Hough Line transform is not correctly identifying any lines

时间:2018-02-01 18:38:58

标签: python opencv hough-transform canny-operator

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)

1 个答案:

答案 0 :(得分:0)

您可能正在查看旧的opencv教程页面,该页面可能有错误(或者版本控制发生了变化,没有跟踪opencv-python)。
Here's a new & correct one

您需要更改的是替换

for rho, theta in lines[0]:

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

但无论如何,你需要一些时间来获得所需的输出。 我建议您使用HoughLinesP,这可以轻松地为您提供您可能需要的内容

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