MJ31
MJ31

Reputation: 31

Hough Line transform is not correctly identifying any lines

I am having trouble with Hough Line transformation. I am trying to identify the major lines in a kitchen. I first just used Canny, but it was picking up more noise than I wanted and wasn't picking up the meeting of the wall and ceiling. However, the Hough Line transformation is only identifying one line that it should not be identifying at all. Any help would be appreciated.

My input:

kitchen_sample.jpg

My output:

kitchen_lines.jpg

And here is my code:

import cv2
import numpy as np

image = cv2.imread('kitchen_sample.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150, apertureSize=3)

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

cv2.imwrite('kitchen_lines.jpg', image)

Upvotes: 1

Views: 1627

Answers (1)

Dmitrii Z.
Dmitrii Z.

Reputation: 2357

You were probably looking at the old opencv tutorial page which probably has a mistake in it (or something changed with versioning, didn't track opencv-python).
Here's a new & correct one

All you need to change is replace

for rho, theta in lines[0]:

with

for line in lines:
rho,theta = line[0]

But anyway it would take you some time to get desired output. What I would recommend you is using HoughLinesP which would easily give you what you likely need

lines = cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength=100,maxLineGap=10)
for line in lines:
x1,y1,x2,y2 = line[0]
cv2.line(image,(x1,y1),(x2,y2),(0,255,0),2)

enter image description here

Upvotes: 3

Related Questions