Opencv python HoughLinesP 奇怪的结果 [英] Opencv python HoughLinesP strange results

查看:132
本文介绍了Opencv python HoughLinesP 奇怪的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图得到他们在

如你所见,我在图像的中间只有一条小线.

不知道出了什么问题.我错过了什么?

谢谢.

解决方案

注意:由于您链接了 OpenCV 2.4.x 教程,我最初假设您也使用 OpenCV 2.4.11 编写代码.事实证明,您实际上使用的是 OpenCV 3.x.请记住,2.x 和 3.x 之间的 API 有细微的变化.

<小时>

您调用

<小时>

类似情况与 Canny

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

再次标记参数:

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

但应该是:

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

但是这对输出没有影响,因为apertureSize 的默认值为3.

<小时>

最后,正如我们通过 Vasanthnamatojcv2.HoughLinesP生成的输出格式有区别:

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

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

<小时>

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

导入 cv2将 numpy 导入为 npdef get_lines(lines_in):如果 cv2.__version__ <'3.0':返回 lines_in[0]返回 [l[0] for l in lines]img = cv2.imread('建筑.jpg')img_gray = 灰色 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)cannied = cv2.Canny(img_gray,阈值1=50,阈值2=200,孔径大小=3)线= cv2.HoughLinesP(罐头,rho=1,theta=np.pi/180,阈值=80,minLineLength=30,maxLineGap=10)对于 get_lines(lines) 中的行:leftx,boty,rightx,topy = 线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()

I'm trying to get the same result they got in this tutorial for HoughLinesP filter. I took same images and same threshold values like this :

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 class is a custom class that does not do anything interesting just calculates some stuff and can draw the line. And then I get these two images :

So as you can see I get only one litle line in the midle of the image.

Not sure what's going wrong. Did I miss some thing?

Thanks.

解决方案

NOTE: Since you linked a tutorial for OpenCV 2.4.x, I initially assumed you also wrote your code with OpenCV 2.4.11. As it turns out, you're actually using OpenCV 3.x. Keep in mind that there are subtle changes in the API between 2.x and 3.x.


You call HoughLinesP incorrectly.

According to the documentation, the signature of the Python function is:

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

If we label the parameters in your call, we get the following:

lines = cv2.HoughLinesP(cannied, rho=1, theta=np.pi / 180
    , threshold=80, lines=30, minLineLength=10)

However, the C++ code correctly ported to Python would be

lines = cv2.HoughLinesP(cannied, rho=1, theta=np.pi / 180
    , threshold=80, minLineLength=30, maxLineGap=10)


Similar situation with Canny

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

Again, let's label the parameters:

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

But it should be:

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

However this makes no difference in the output, since the default value for apertureSize is 3.


Finally, as we identified with Vasanth and namatoj, there is a difference in the format of the output generated by cv2.HoughLinesP:

  • In 2.4 it looks like [[[x1, y1, x2, y2], [...], ..., [...]]]
  • In 3.x it looks like [[[x1, y1, x2, y2]], [[...]], ..., [[...]]]

I added a short get_lines function to transform the lines into consistent layout ([[x1, y1, x2, y2], [...], ..., [...]]) in both versions.


Full script that works in both OpenCV versions:

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

这篇关于Opencv python HoughLinesP 奇怪的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆