【问题标题】:Hough space for cv2 houghlinescv2 houghlines 的霍夫空间
【发布时间】:2016-09-26 09:46:46
【问题描述】:

当我认为真正的拟合应该提供水平线时,我在 cv2.Houghlines() 显示垂直线时遇到了一些问题。 这是我正在使用的代码片段:

        rho_resoultion = 1
        theta_resolution = np.pi/180
        threshold = 200

        lines = cv2.HoughLines(image, rho_resoultion, theta_resolution, threshold)

        # print(lines)
        for line in lines:
            rho, theta = line[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(image,(x1,y1),(x2,y2),(255,255,255),1)

        cv2.namedWindow('thing', cv2.WINDOW_NORMAL)
        cv2.imshow("thing", image)
        cv2.waitKey(0)

这是输入和输出:

我认为如果可以查看霍夫空间图像,提取正在发生的事情会更容易。 但是,该文档没有提供有关如何显示完整霍夫空间的信息。 如何显示整个霍夫变换空间? 我尝试将阈值降低到 1,但它没有提供图像。

【问题讨论】:

    标签: opencv computer-vision transform hough-transform


    【解决方案1】:

    也许你在计算角度时出错了。随意展示一些代码。

    这是一个如何在图像中显示所有霍夫线的示例:

    import cv2
    import numpy as np
    
    img = cv2.imread('sudoku.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:
        for rho,theta in line:
            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.imshow('Houghlines',img)
    if cv2.waitKey(0) & 0xff == 27:
        cv2.destroyAllWindows()
    

    原图:

    结果:

    【讨论】:

    • 这实际上和我使用的代码非常相似,但是结果很糟糕。你可以看到我在问题中添加的结果。
    猜你喜欢
    • 2011-12-25
    • 1970-01-01
    • 2012-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    相关资源
    最近更新 更多