[英]How can I improve the results of HoughLinesP() in my OpenCV Python script
[英]Opencv python HoughLinesP strange results
我试图获得他们在本教程中为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
类是一个自定义类,它不会做任何有趣的事情,只需计算一些东西并绘制线条。 然后我得到这两个图像:
所以你可以看到我在图像的中间只有一条litle线。
不确定出了什么问题。 我错过了什么?
谢谢。
注意:由于您链接了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)
与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。
最后,正如我们在Vasanth和namatoj中所确定的那样 , cv2.HoughLinesP
生成的输出格式有所不同:
[[[x1, y1, x2, y2], [...], ..., [...]]]
[[[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()
正如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()
输出:
如果您使用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)
代码中的问题是如何排列返回的行。 这段代码对我有用:
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()
我还做了一些其他的小改动,以便在我的机器上运行代码。
我认为您需要更改一些参数才能获得与文档中完全相同的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.