如何在HoughlineP opencv中合并附近的线

问题描述

我正在尝试使用 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),2)
    
    return image

来自 Houghline 方法的图像结果 test_1,test_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,y2 = line
        if [x1,y2] in idx:
            continue
        parameters = P.polyfit((x1,x2),(y1,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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...