【问题标题】:How do i optimize line detection for different images?如何优化不同图像的线检测?
【发布时间】:2021-06-08 15:37:24
【问题描述】:

一点上下文:
我正在尝试检测立方体的消失点,然后将具有相同消失点的线组合在一起
我在边缘/线检测阶段遇到问题。


例如:

Original Image

Desired Output


  1. 问题是通过对参数 [thresholding,canny,Hough_Line] 稍作修改,我得到了所需的霍夫线输出。
  2. 例如,我优化了这个特定图像的参数Input1,我得到了所需的输出。
  3. 但如果我对Different Image 应用相同的值,那么会为同一行检测到很多重复行。
  4. 我已经编写了一个过滤算法来加入类似的行。但是这里检测到的线条有很大的不同 [rho,theta],我必须将过滤算法的阈值提高很多,这最终会影响其他线条。
  5. 我认为问题是因为在边缘检测过程中线断裂,因此被解释为不同的线,我不确定。
  6. 我尝试在预处理中使用膨胀和腐蚀,但我遇到了同样的问题,即不同图像的内核大小不同,因此我将这部分注释掉。
  7. 我可以调整不同图像的参数,但这里的差异非常大,所以我觉得这里的问题出在我的预处理功能上。
  8. 我已经粘贴了我认为与这个问题相关的函数以避免混乱,如果有人需要完整的我可以分享 github 链接。

代码:

def pre_processing(path):

'''
    pre_processing:

            Param  : 
                path : Path for the image to processed.

            Return :
                edge_image : NumpyArray that stores the grayscale values of 
                             Input image.
'''

image = cv2.imread(path)

image = cv2.bitwise_not(image)

gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

# print("Gray:",gray,"\n")
# plt.imshow(gray,cmap='gray')
# plt.show()

blur = cv2.GaussianBlur(gray,(5,5),0)

# print("blur:",blur,"\n")
# plt.imshow(blur,cmap='gray')
# plt.show()

ret,thresh = cv2.threshold(blur,50,255,cv2.THRESH_BINARY)



return thresh    

def filter_threshold(indexes,filter_indexes,filt_rho = 20,filt_theta= 0.2):

'''
    Filter_threshold:

        Param:
            indexes :  The raw [rho,theta] values with duplicates for same line.
            filter_indexes : A list to store indexes of unique lines.
            filt_rho : acceptable rho ranges to group lines.
            filt_theta  : acceptable theta ranges to group lines.

        Return :  
            filter_indexes : Dataset that stores filtered(duplicate values) 
                             (rho,theta) values.
'''
a = 0

rho = indexes[a][0]
theta = indexes[a][1]
unfilt_indexes = []

for j in range(len(indexes)):

    
    if rho<0:
        rho *= -1
        theta -= np.pi

    curr_rho = indexes[j][0]
    curr_theta = indexes[j][1]

    if curr_rho<0:
        curr_rho *= -1
        curr_theta -= np.pi

    if a == j:
        pass
    elif (0 <= abs(curr_rho-rho) <filt_rho) and (0 <= abs(curr_theta-theta)<filt_theta):

        pass

    else:
        unfilt_indexes.append([curr_rho,curr_theta])

filter_indexes.append([rho,theta])

if len(unfilt_indexes)>0:
    x = filter_threshold(unfilt_indexes,filter_indexes)

return filter_indexes

path = "/work/cubes/4.jpg"
thresh = pre_processing(path)

edge_image = cv2.Canny(thresh,100,200)

hough_threshold = 40
lines = cv2.HoughLines(edge_image,1,(np.pi/180),hough_threshold)

filtered_indexes = []
filtered_indexes = filter_threshold(lines,filtered_indexes,20,0.2)

【问题讨论】:

  • 到底是什么问题?阈值参数?精明的参数?霍夫参数?我建议使用 LSD 或 EDLines 而不是 threshold/canny。
  • @Micka 我认为问题出现在预处理和精明之间。我无法正确地提出问题,但我遇到的问题是在霍夫变换期间检测到的重复行可以通过使用我的过滤算法来删除,但不能使用我的过滤算法来删除image。最初我认为这是由于过滤算法,但线条有很大差异。所以我猜这是由于精明或预处理的错误。我正在研究你提到的算法。感谢您的回复。

标签: python opencv computer-vision edge-detection hough-transform


【解决方案1】:

有点难过,当您拥有如此清晰的图像且线段如此容易遵循时,使用 Hough 或 LSD 之类的大锤并尝试从如此获得的混乱中检索解决方案。

还要注意,消失点可以单独从六边形轮廓中获得。

下面是通过简单的轮廓检测得到的六边形(在不同的图像上)。从轮廓中,您可以通过查找对齐像素的长段来分割边。 (Douglas-Peucker 是一个选项。)

更一般地说,您可以分割出所有白色区域并将多边形拟合到它们以获取边缘片段并将它们扩展到消失点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-30
    • 1970-01-01
    • 2013-01-03
    • 2015-09-05
    相关资源
    最近更新 更多