Python OpenCV houghLinesP 参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35609719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenCV houghLinesP parameters
提问by Jane Courtney
I am having difficulty finding the lines on a chessboard in this image using HoughLinesP with OpenCV in Python.
我在使用 HoughLinesP 和 Python 中的 OpenCV 时难以在此图像中找到棋盘上的线条。
In an attempt to understand the parameters of HoughLinesP, I have come up with the following code:
为了理解 HoughLinesP 的参数,我想出了以下代码:
import numpy as np
import cv2
from matplotlib import pyplot as plt
from matplotlib import image as image
I = image.imread('chess.jpg')
G = cv2.cvtColor(I, cv2.COLOR_BGR2GRAY)
# Canny Edge Detection:
Threshold1 = 150;
Threshold2 = 350;
FilterSize = 5
E = cv2.Canny(G, Threshold1, Threshold2, FilterSize)
Rres = 1
Thetares = 1*np.pi/180
Threshold = 1
minLineLength = 1
maxLineGap = 100
lines = cv2.HoughLinesP(E,Rres,Thetares,Threshold,minLineLength,maxLineGap)
N = lines.shape[0]
for i in range(N):
x1 = lines[i][0][0]
y1 = lines[i][0][1]
x2 = lines[i][0][2]
y2 = lines[i][0][3]
cv2.line(I,(x1,y1),(x2,y2),(255,0,0),2)
plt.figure(),plt.imshow(I),plt.title('Hough Lines'),plt.axis('off')
plt.show()
The problem I am having is that this picks up only one line. If I reduce the maxLineGap to 1, it picks up thousands.
我遇到的问题是这只能拾取一行。如果我将 maxLineGap 减少到 1,它会增加数千。
I understand why this might be but how do I pick a suitable set of parameters to get all these co-linear lines to merge? Am I missing something?
我明白为什么会这样,但是我如何选择一组合适的参数来合并所有这些共线线?我错过了什么吗?
I would like to keep the code simple as I am using it as an example of this function in action.
我想保持代码简单,因为我将它用作此函数的示例。
Thanks in advance for any help!
在此先感谢您的帮助!
Update: This works perfectly with HoughLines.
更新:这与 HoughLines 完美配合。
And there doesn't seem to be edge detection issues as Canny is working just fine.
而且似乎没有边缘检测问题,因为 Canny 工作得很好。
However, I still need to get HoughLinesP to work. Any ideas??
但是,我仍然需要让 HoughLinesP 工作。有任何想法吗??
Images here: Results
这里的图片:结果
采纳答案by Jane Courtney
Ok, I finally found the problem and thought I would share the solution for anyone else driven nuts by this. The issue is that in the HoughLinesP function, there is an extra parameter, "lines" which is redundant because the output of the function is the same:
好的,我终于找到了这个问题,并认为我会为其他为此而发疯的人分享解决方案。问题是在 HoughLinesP 函数中,有一个额外的参数“lines”,它是多余的,因为函数的输出是相同的:
cv2.HoughLinesP(image, rho, theta, threshold[, lines[, minLineLength[, maxLineGap]]])
cv2.HoughLinesP(图像,rho,theta,阈值[,行[,minLineLength[,maxLineGap]]])
This is causing issues with the parameters as they are read in the wrong order. To avoid confusion with the order of the parameters, the simplest solution is to specify them inside the function like so:
这会导致参数出现问题,因为它们以错误的顺序读取。为避免与参数顺序混淆,最简单的解决方案是在函数内指定它们,如下所示:
lines = cv2.HoughLinesP(E,rho = 1,theta = 1*np.pi/180,threshold = 100,minLineLength = 100,maxLineGap = 50)
This totally fixed my problem and I hope it will help others.
这完全解决了我的问题,我希望它会帮助其他人。
回答by Meaniegy
It is not HoughLinesP
issue, using that method will only get all the lines detected in the picture and return to you.
这不是HoughLinesP
问题,使用该方法只会让所有的照片并返回给你检测线。
To be able to get the lines you want,you will need to smooth the image before you use the method. However if you smooth too much, there won't be any edges for HoughLinesP to detect.
为了能够获得您想要的线条,您需要在使用该方法之前平滑图像。但是,如果平滑太多,则 HoughLinesP 将无法检测到任何边缘。
You can know more about Smoothing Effects of OpenCV here.
您可以在此处了解有关 OpenCV 平滑效果的更多信息。
回答by Sangha
cv2.HoughLinesP(image,rho, theta, threshold, np.array ([ ]), minLineLength=xx, maxLineGap=xx)
cv2.HoughLinesP(image,rho, theta, threshold, np.array ([ ]), minLineLength=xx, maxLineGap=xx)
This will also work.
这也将起作用。