问题描述
我在尝试对使用霍夫变换获得的线进行分组时遇到问题,其中我有以下信息:每条线的 rho(距离)和 theta(角度)。
问题是同一个位置,生成多条线,hough的参数不能改。挑战来了,我需要对附近的线进行分组并只提取一条。我试图仅按角度或仅按距离使用限制进行分组,但我意识到一个取决于另一个(可能有几条线具有相同的角度,但距离不同,反之亦然),我需要同时使用这两个信息,但我无法完成此任务。
我的python代码:
import cv2
import numpy as np
import matplotlib.pyplot as plt
def drawLines(thetas,rhos):
img_black = np.zeros((240,320,3),np.uint8) #empty image 240x320 just for draw the lines
origin = np.array((0,img_black.shape[1]))
for theta,rho in zip(thetas,rhos):
degree = abs(theta*(180/np.pi)) #radian to degree
if(not (degree >= 45 and degree <= 120)): #limiting horizontal lines
y0,y1 = (rho - origin * np.cos(theta)) / np.sin(theta)
xx1,yy1 = (0,int(y0))
xx2,yy2 = (img_black.shape[1],int(y1))
cv2.line(img_black,(xx1,yy1),(xx2,yy2),(0,255,255),2)
return img_black
#array containing angles at [:,0] and distances at [:,1].
lines = np.array([[ 0.240651248,143.679599499],[-0.538183700,150.688360451],[-0.179394567,157.697121402],[ 0.599440381,110.638297872],[ 0.021877386,172.715894869],[ 0.415670337,143.679599499]])
thetas = lines[:,0]
rhos = lines[:,1]
img_lines = drawLines(thetas,rhos)
#showing the original result:
plt.imshow(img_lines)
输出:
当我使用下面的代码手动删除第 0 行和第 2 行时,我得到了我想要的结果,但我需要自动执行此操作。
deleted = [0,2] #indexs deleted lines
thetas2 = np.delete(thetas,deleted)
rhos2 = np.delete(rhos,deleted)
img_lines2 = drawLines(thetas2,rhos2)
plt.imshow(img_lines2)
输出:
除了上面几行中的数据之外,这个问题还有一些变体(我希望每组中只找到一行),例如:
lines2 = np.array([[ 0.354413656,112.640801001],[-0.520681791,131.664580726],[-0.450674155,149.687108886],[ 0.039379295,164.705882353],[-0.083134067,148.685857322],[ 0.573187518,125.657071339],[-0.126888840,138.673341677],[-0.739455652,137.672090113],[-0.205647430,121.652065081],[-0.363164610,166.708385482],[-0.774459470,126.658322904],[-0.223149339,183.729662078]])
thetas3 = lines2[:,0]
rhos3 = lines2[:,1]
img_lines3 = drawLines(thetas3,rhos3)
plt.imshow(img_lines3)
输出:
我希望你能帮助我,我已经在尝试为此创建一个解决方案,但到目前为止我还没有得到任何结果。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)