【问题标题】:Opencv houghLines not detecting linesOpencv houghLines 未检测到线条
【发布时间】:2019-01-14 20:10:30
【问题描述】:

我正在尝试使用霍夫线过滤图像,但有些东西不起作用。

我创建了以下子例程,它是从文档中复制粘贴而没有修改:

def FilterLines(img):
    lines = cv2.HoughLines(img,1,np.pi/180,200)
    blank_image = np.zeros((img.shape[0], img.shape[1]), np.uint8)
    if (lines is None):
        return blank_image
    for rho,theta in lines[0]:
        a = np.cos(theta)
        b = np.sin(theta)
        x0 = a*rho
        y0 = b*rho
        x1 = int(x0 + 1000*(-b))
        y1 = int(y0 + 1000*(a))
        x2 = int(x0 - 1000*(-b))
        y2 = int(y0 - 1000*(a))

        cv2.line(blank_image,(x1,y1),(x2,y2),255,1)

    return blank_image

然后我给它下面的测试图像:

但是函数outputs:

所以它在底部检测到几行,没有别的。

为什么函数不能在如此简单的图像中检测线条?

【问题讨论】:

    标签: python opencv image-processing hough-transform


    【解决方案1】:

    问题出在这一行for rho,theta in lines[0]:。你只使用了一行,第一行line[0]。

    你需要像这样改变for循环:

        for x in range(0, len(lines)):    
            for rho, theta in lines[x]:
    

    然后整个代码看起来像这样(在我将它们放入测试后,额外的调试语句被注释掉了):

    import cv2
    import os
    import numpy as np
    
    #path = os.path("D:\")
    img = cv2.imread("D:\\oV1PG.png")
    
    #cv2.imshow('image',img)
    #cv2.waitKey(0)
    
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    #print(img)
    
    '''height, width, channel = img.shape[:3]
    size = img.size
    
    print(height, width, channel, size)'''
    
    
    if img is not None:
        lines = cv2.HoughLines(img, 1, np.pi / 180, 100)
    
    
        blank_image = np.zeros((img.shape[0], img.shape[1]), np.uint8)
    
        for x in range(0, len(lines)):    
            for rho, theta in lines[x]:
                a = np.cos(theta)
                b = np.sin(theta)
                x0 = a * rho
                y0 = b * rho
                x1 = int(x0 + 1000 * (-b))
                y1 = int(y0 + 1000 * (a))
                x2 = int(x0 - 1000 * (-b))
                y2 = int(y0 - 1000 * (a))
    
                cv2.line(blank_image, (x1, y1), (x2, y2), 255, 1)
                print(x1, y1, x2, y2)
    
            print("\n")
    
        cv2.imshow("out", blank_image)
        cv2.waitKey(0)
    else:
        print("empty img. Cannot read file")
    

    这是输出在我的 PC 上的样子:

    【讨论】:

    • 所以 hough 会倾向于将 ines 一直延伸到原点 (0,0)?
    • 这个函数需要两个参数,cv2.HoughLines(img, 1, np.pi / 180, 100),根据documentation是cv2.HoughLines(img, 1, np.pi / 180, 100,minLineLength,maxLineGap)。也许和他们一起玩会产生不同的结果。但老实说,我没有对此进行调查,因为这不是这里最初的问题。 p.s.在任何情况下,这些线都不会延伸到原点 (0,0)。看垂直线。他们不倾向于去原点。也没有任何水平线。
    • 同样按照hough transform docs,你也可以玩阈值。它是您的代码中的100。增加它会减少行数。但这也可能会删除合法行。必须稍微调整一下参数才能获得最佳结果。
    【解决方案2】:

    使用此代码。 openCV 版本存在冲突,不允许我们以这种方式执行上述操作。

    
    import cv2
    import numpy as np
    
    img = cv2.imread('image.jpg')
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    edges = cv2.Canny(gray,50,150,apertureSize = 3)
    
    lines = cv2.HoughLines(edges,1,np.pi/180,200)
    for line in lines:
        rho = line[0][0]
        theta = line[0][1]
        a = np.cos(theta)
        b = np.sin(theta)
        x0 = a*rho
        y0 = b*rho
        x1 = int(x0 + 1000*(-b))
        y1 = int(y0 + 1000*(a))
        x2 = int(x0 - 1000*(-b))
        y2 = int(y0 - 1000*(a))
    
        cv2.line(img,(x1,y1),(x2,y2),(0,0,255),2)
    
    cv2.imwrite('houghlines3.jpg',img)
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-30
      • 2019-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-12
      • 2020-01-20
      • 1970-01-01
      相关资源
      最近更新 更多