Python如何使用带有OpenCV的HoughLines检测图像中的垂直和水平线? [英] Python How to detect vertical and horizontal lines in an image with HoughLines with OpenCV?

查看:479
本文介绍了Python如何使用带有OpenCV的HoughLines检测图像中的垂直和水平线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取校准棋盘的阈值.观察微型棋盘时,由于有些灰尘,我无法直接检测到棋盘角. 我尝试了几种方法,而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)

如下图所示,我无法获得棋盘,线条在很多方向上都被绘制...(原始图片:解决方案

您为rho使用的值太小.

尝试以下代码:-

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)

请注意, rho值,pi值和maxLineGap 的变化可减少异常值.

输入图片

边缘图像

输出图像

其他-初学者提示

  1. 许多计算机视觉算法都对输入的方式进行了某些假设.构建概念验证时,请始终尝试在应用此类算法之前查看生成的中间输入.

  2. 对于快速破解,如果算法接受某些参数,请对这些参数的可能值使用for循环,并查看结果如何变化. 链接有关如何快速生成这些可能值的答案.

  3. 要真正理解算法,请在必要时阅读Wiki或什至是更好的资源.然后再次/仍然进行上述修改(第2点).它将进一步清除您的理解.

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?

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)

解决方案

You are using too small value for 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 maxLineGap to reduce outliers.

Input Image

Edges Image

Output Image

Miscellaneous - Tips for Beginners

  1. 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.

  2. 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. Link to an answer on how to quickly generate these possible values.

  3. 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.

这篇关于Python如何使用带有OpenCV的HoughLines检测图像中的垂直和水平线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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