Gui
Gui

Reputation: 45

Houghlines no detect all lines opencv+python

I really need help, I'm trying for many days know, how to use the houghlines function of OpenCV with python .

The function does not detect all lines, even when the result of threshold if it's showing. I have some images and the portion of code that I'm using. thank you very much for any help. i'm sorry for my english.

    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)

Resulting imagen here

Threshold image here

Upvotes: 1

Views: 3477

Answers (1)

Julio Cabrera
Julio Cabrera

Reputation: 93

Is because the for bucle is around lines[0] -> [0] you must try this:

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)

Upvotes: 3

Related Questions