Python 如何使用 OpenCV 使用 HoughLines 检测图像中的垂直线和水平线?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39752235/
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
Python How to detect vertical and horizontal lines in an image with HoughLines with OpenCV?
提问by user3601754
I m trying to obtain a threshold of the calibration chessboard. I cant detect directly the chessboard corners as there is some dust as i observe a micro chessboard. I try several methods and HoughLinesP seems to be the easiest approach. But the results are not good, how to improve my results?
我正在尝试获得校准棋盘的阈值。我无法直接检测棋盘角,因为我观察微型棋盘时有一些灰尘。我尝试了几种方法,HoughLinesP 似乎是最简单的方法。但结果并不好,如何提高我的结果?
import numpy as np
import cv2
img = cv2.imread('lines.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
print img.shape[1]
print img.shape
minLineLength=100
lines = cv2.HoughLinesP(image=edges,rho=0.02,theta=np.pi/500, threshold=10,lines=np.array([]), minLineLength=minLineLength,maxLineGap=100)
a,b,c = lines.shape
for i in range(a):
cv2.line(img, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (0, 0, 255), 3, cv2.LINE_AA)
cv2.imwrite('houghlines5.jpg',img)
As you can see on figure below, i cant obtain my chessboard, the lines are plotted in a lot of directions... (the original picture : https://s22.postimg.org/iq2b91xq9/droite_Image_00000.jpg)
正如你在下图中看到的,我无法获得我的棋盘,线被绘制在很多方向......(原始图片:https: //s22.postimg.org/iq2b91xq9/droite_Image_00000.jpg)
回答by saurabheights
You are using too small value for rho.
您使用的 rho 值太小。
Try the below code:-
试试下面的代码:-
import numpy as np
import cv2
gray = cv2.imread('lines.jpg')
edges = cv2.Canny(gray,50,150,apertureSize = 3)
cv2.imwrite('edges-50-150.jpg',edges)
minLineLength=100
lines = cv2.HoughLinesP(image=edges,rho=1,theta=np.pi/180, threshold=100,lines=np.array([]), minLineLength=minLineLength,maxLineGap=80)
a,b,c = lines.shape
for i in range(a):
cv2.line(gray, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (0, 0, 255), 3, cv2.LINE_AA)
cv2.imwrite('houghlines5.jpg',gray)
Note, the change in rho value, pi value and maxLineGapto reduce outliers.
注意,rho 值、pi 值和 maxLineGap 的变化以减少异常值。
Miscellaneous - Tips for Beginners
杂项 - 初学者提示
A lot of Computer Vision algorithms assume certain assumptions, well, in how the input should be. When building Proof-of-Concept, always try to view intermediate inputs you generate before applying such algorithms.
For quick hack, if an algorithm accepts some parameters, use a for loop on possible values of these parameters and see how the results varies. Linkto an answer on how to quickly generate these possible values.
To really understand the algorithm, read on wiki or even better sources where if necessary. And then again/still do the above hack(point 2). It will further clear your understanding.
许多计算机视觉算法都假设输入应该如何。在构建概念验证时,请始终尝试在应用此类算法之前查看您生成的中间输入。
为了快速破解,如果算法接受某些参数,请对这些参数的可能值使用 for 循环,并查看结果如何变化。链接到有关如何快速生成这些可能值的答案。
要真正理解算法,请在必要时阅读 wiki 或更好的资源。然后再次/仍然执行上述 hack(第 2 点)。它会进一步明确你的理解。
回答by Rick M.
I would rather write this as a comment but unfortunately I can't. You should change the minLineLength and minLineGap. Or what if its just sqaures that you have to find, I would get all the lines and check the angles between them to get lines only along squares. I have worked with HoughLineP before and it is pretty much based on the above two arguments. Additionally, try using Bilateral filtering. I really helps when the sharpening using median filter doesn't help.
我宁愿把它写成评论,但不幸的是我不能。您应该更改 minLineLength 和 minLineGap。或者,如果它只是您必须找到的正方形,我会获取所有线条并检查它们之间的角度以仅沿着正方形获得线条。我之前曾与 HoughLineP 合作过,它几乎基于上述两个论点。此外,尝试使用双边过滤。当使用中值滤波器的锐化没有帮助时,我真的很有帮助。
回答by shadow
in images processing they are some roles you have to go through such as filters before you go for edges detection, in your condition the dust is just a noise that you have to remove by filter, use gausse or blure after that use thresholding and then use canny for edges and in opencv they are cornere detection you can use, or you can just go for key point after threshholding if i'm not wrong.. try to do those steps and see the resulte
在图像处理中,它们是您在进行边缘检测之前必须经历的一些角色,例如过滤器,在您的条件下,灰尘只是您必须通过过滤器去除的噪声,然后使用高斯或模糊,然后使用阈值,然后使用精明的边缘和在 opencv 中,它们是您可以使用的角点检测,或者如果我没有错,您可以在阈值后寻找关键点..尝试执行这些步骤并查看结果