Opencv Python HoughLinesP 奇怪结果

5

我试图获得与这个 HoughLinesP滤波器教程中相同的结果。我使用了相同的图像和阈值值,如下所示:

import cv2
from line import Line
import numpy as np

img = cv2.imread('building.jpg',1)
cannied = cv2.Canny(img, 50, 200, 3)
lines = cv2.HoughLinesP(cannied, 1, np.pi / 180, 80, 30, 10)


for leftx, boty, rightx, topy in lines[0]:
    line = Line((leftx, boty), (rightx,topy))
    line.draw(img, (255, 255, 0), 2)

cv2.imwrite('lines.png',img)
cv2.imwrite('canniedHouse.png',cannied)
cv2.waitKey(0)
cv2.destroyAllWindows()
Line类是一个自定义类,它并没有做什么有趣的事情,只是计算一些东西并且可以画线。然后我得到了这两个图片:enter image description hereenter image description here 所以,正如您所看到的,我在图片中间只得到了一条小线。
不确定哪里出了问题。我错过了什么吗?
谢谢。
3个回答

9

注意:由于您链接了一个针对OpenCV 2.4.x的教程,我最初认为您也使用OpenCV 2.4.11编写了代码。事实证明,您实际上正在使用OpenCV 3.x。请记住,在2.x和3.x之间的API中存在微妙的变化。


您错误地调用了 HoughLinesP

根据文档,该Python函数的签名为:

cv2.HoughLinesP(image, rho, theta, threshold[, lines[, minLineLength[, maxLineGap]]]) → lines

如果我们给您的调用参数进行标记,那么我们将得到以下结果:
lines = cv2.HoughLinesP(cannied, rho=1, theta=np.pi / 180
    , threshold=80, lines=30, minLineLength=10)

然而,正确移植到Python的C++代码应该是:
lines = cv2.HoughLinesP(cannied, rho=1, theta=np.pi / 180
    , threshold=80, minLineLength=30, maxLineGap=10)

Result


Canny 相似的情况。

cv2.Canny(image, threshold1, threshold2[, edges[, apertureSize[, L2gradient]]]) → edges

再次,让我们标记参数:

cannied = cv2.Canny(img, threshold1=50, threshold2=200, edges=3)

但应该是:
cannied = cv2.Canny(img, threshold1=50, threshold2=200, apertureSize=3)

然而,这对输出结果没有影响,因为apertureSize的默认值是3。

最后,正如我们与Vasanthnamatoj所确认的那样,cv2.HoughLinesP生成的输出格式存在差异:

  • 在2.4中,它看起来像[[[x1, y1, x2, y2], [...], ..., [...]]]
  • 在3.x中,它看起来像[[[x1, y1, x2, y2]], [[...]], ..., [[...]]]

我添加了一个简短的get_lines函数,将两个版本中的线路转换为一致的布局([[x1, y1, x2, y2], [...], ..., [...]])。


适用于OpenCV两个版本的完整脚本:

import cv2
import numpy as np


def get_lines(lines_in):
    if cv2.__version__ < '3.0':
        return lines_in[0]
    return [l[0] for l in lines]


img = cv2.imread('building.jpg')
img_gray = gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

cannied = cv2.Canny(img_gray, threshold1=50, threshold2=200, apertureSize=3)
lines = cv2.HoughLinesP(cannied, rho=1, theta=np.pi / 180, threshold=80, minLineLength=30, maxLineGap=10)

for line in get_lines(lines):
    leftx, boty, rightx, topy = line
    cv2.line(img, (leftx, boty), (rightx,topy), (255, 255, 0), 2)

cv2.imwrite('lines.png',img)
cv2.imwrite('canniedHouse.png',cannied)
cv2.waitKey(0)
cv2.destroyAllWindows()

4
作为Dan的回答所提到的,Canny和HoughLinesP中的参数没有正确指定。
修改后的代码:
import cv2
from line import Line
import numpy as np

img = cv2.imread('building.jpg',1)
cannied = cv2.Canny(img, 50, 200, apertureSize=3)
lines = cv2.HoughLinesP(cannied, 1, np.pi / 180, 80, minLineLength=30, maxLineGap=10)

for leftx, boty, rightx, topy in lines[0]:
    line = Line((leftx, boty), (rightx,topy))
    line.draw(img, (255, 255, 0), 2)

cv2.imwrite('lines.png',img)
cv2.imwrite('canniedHouse.png',cannied)
cv2.waitKey(0)
cv2.destroyAllWindows()

输出:

detected lines

如果您使用的是OpenCV-3+,请使用以下for循环,因为HoughLinesP返回不同的输出格式:[[[x1,y1,x2,y2]],[[...]] ... [[...]]]
for l in lines:  #Modified to loop across all the lines
    leftx, boty, rightx, topy = l[0] #assign each line's values to variables
    line = Line((leftx, boty), (rightx,topy))
    line.draw(img, (255, 255, 0), 2)

这可能与版本有关。当我查看OpenCV 2.4.11中的“lines”时,我发现它的格式如下:[[[x1,y1,x2,y2],[....]]]。这使得Anton的代码正确。 - Dan Mašek
是的,我认为你是正确的。2.4.11有不同的格式。我会更新我的答案。 - Vasanth

1
你的代码问题在于返回的行排列方式。 这段代码对我有效:
import cv2
import numpy as np

img = cv2.imread('building.jpg',1)
cannied = cv2.Canny(img, 50, 200, 3)
lines = cv2.HoughLinesP(cannied, 1, np.pi / 180, 80, 30, 10)

for line in lines:
    leftx, boty, rightx, topy = line[0]
    cv2.line(img, (leftx, boty), (rightx,topy), (255, 255, 0), 2)

cv2.imwrite('lines.png',img)
cv2.imwrite('canniedHouse.png',cannied)
cv2.imshow('', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

我还做了一些其他小的修改,以便在我的电脑上运行代码。

enter image description here

我认为你需要更改一些参数,才能获得与文档完全相同的结果。

1
由于你的代码几乎与Vasanth的代码相同,因此它包含了我在评论中指出的相同问题。 - Dan Mašek
1
我不知道这个,谢谢你指出来!看起来 OP 也在版本 3 上做了这个。 - namatoj
关于 OP 的 OpenCV 版本,你提出了一个很好的观点。我因为过于专注于错误的参数而产生了一些狭隘的视野。 - Dan Mašek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接