Houghlines没有检测到所有行opencv + python

时间:2015-01-14 14:26:54

标签: python opencv lines hough-transform

我真的需要帮助,我已经尝试了很多天知道,如何使用OpenCV的houghlines函数和python。

该功能不检测所有行,即使是阈值结果显示也是如此。我有一些图像和我正在使用的代码部分。非常感谢你的帮助。对不起我的英语。

    blur = cv2.GaussianBlur(mask,(5,5),0)
    ret,edges = cv2.threshold(mask,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
    kernel = np.ones((3,3),np.uint8)
    image = cv2.erode(edges,kernel,iterations = 1)

    lines = cv2.HoughLines(image,1,np.pi/180,105)
    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(img,(x1,y1),(x2,y2),(0,0,255),1)

产生的图像here

阈值图片here

1 个答案:

答案 0 :(得分:3)

是因为for bucle在line [0]附近 - > [0]你必须试试这个:

for i in  range(0,lines.shape[0]):
    rho,thetha = lines[i,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(img,(x1,y1),(x2,y2),(0,0,255),1)