Opencv houghLines not detecting lines

Viewed 2472

I am trying to filter an image using Hough lines but something just isn't working.

I created the following subroutine, which was copy pasted without modification from the documentation:

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

I then give it the following test image: enter image description here

But the function enter image description hereoutputs:

So it kinda detected a couple of lines at the bottom and nothing else.

Why can;t the function detect lines in such a simple image?

2 Answers

The problem is this linefor rho,theta in lines[0]:. You are using only one line, the first one line[0].

You need to change the for loop like this:

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

The whole code then looks like this (with extra debug statements commented out, after I put them in for testing):

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")

This is how the output looks on my PC:

enter image description here

Use this code. There is a conflict in openCV versions which doesn't let us perform the above operation in that way.


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)

Related