如何在HoughlineP露天电视台合并附近的线路



我正在尝试使用Segmentation和HoughLines检测裁剪行,我有一个来自github的脚本,我正在尝试修改它。

HoughLines之后应用了合并功能,根据距离合并彼此接近的线路

但我似乎不明白为什么。

即使在改变HoughLine参数后,我也可以告诉多条线在哪里检测到单个作物行。因此,合并线路是优化HoughLine过程结果的一种方式。

def draw_lines(image,mask):
mask = mask*255
mask = cv2.GaussianBlur(mask,(5,5),1)
mask = cv2.Canny(mask.astype(np.uint8),80,255)
lines = cv2.HoughLinesP(mask,1,np.pi / 180,threshold=50,
minLineLength=50,maxLineGap=250)
lines = np.squeeze(lines, axis=1)

for line in lines:
x1, y1, x2, y2 = line.astype(int)
cv2.line(image, (x1, y1), (x2, y2), (255, 0, 0), 2)

return image

Houghline方法测试1、测试2 的图像结果

这是合并功能

##merge lines that are near to each other based on distance
from numpy.polynomial import polynomial as P
def merge_lines(lines):
clusters =  []
idx = []
total_lines = len(lines)
if total_lines < 30:
distance_threshold = 20
elif total_lines <75:
distance_threshold = 15
elif total_lines<120:
distance_threshold = 10
else:
distance_threshold = 7
for i,line in enumerate(lines):
x1,y1,x2,y2 = line
if [x1,y1,x2,y2] in idx:
continue
parameters = P.polyfit((x1, x2),(y1, y2), 1)
slope = parameters[0]#(y2-y1)/(x2-x1+0.001)
intercept = parameters[1]#((y2+y1) - slope *(x2+x1))/2
a = -slope
b = 1
c = -intercept
d = np.sqrt(a**2+b**2)
cluster = [line]
for d_line in lines[i+1:]:
x,y,xo,yo= d_line
mid_x = (x+xo)/2
mid_y = (y+yo)/2
distance = np.abs(a*mid_x+b*mid_y+c)/d
if distance < distance_threshold:
cluster.append(d_line)
idx.append(d_line.tolist())
clusters.append(np.array(cluster))
merged_lines = [np.mean(cluster, axis=0) for cluster in clusters]
return merged_lines

我首先按角度对hough线列表进行排序。然后,我将遍历排序列表,并将rho计算为给定的预设rho容差。当角度变化超过预设的给定θ公差时,我会重新开始ρ公差评估。在执行函数时,请注意公差分析中大于90的角度是否会产生负rho。该函数最终以线列表、rho公差和theta公差作为输入。

最新更新