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

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

问题描述

我正在尝试获取校准棋盘的阈值.我无法直接检测棋盘角,因为我观察微型棋盘时有一些灰尘.我尝试了几种方法,HoughLinesP 似乎是最简单的方法.但结果并不好,如何改进我的结果?

将 numpy 导入为 np导入 cv2img = cv2.imread('lines.jpg')灰色 = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)边缘 = cv2.Canny(灰色,50,150,apertureSize = 3)打印 img.shape[1]打印 img.shape最小线长度=100线= cv2.HoughLinesP(图像=边缘,rho=0.02,theta=np.pi/500,阈值=10,线=np.array([]),minLineLength=minLineLength,maxLineGap=100)a,b,c =lines.shape对于范围(a)中的 i: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 值太小.

试试下面的代码:-

将 numpy 导入为 np导入 cv2灰色 = cv2.imread('lines.jpg')边缘 = cv2.Canny(灰色,50,150,apertureSize = 3)cv2.imwrite('edges-50-150.jpg',edges)最小线长度=100线= cv2.HoughLinesP(图像=边缘,rho=1,theta=np.pi/180,阈值=100,线=np.array([]),minLineLength=minLineLength,maxLineGap=80)a,b,c =lines.shape对于范围(a)中的 i: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',灰色)

注意,改变 rho 值、pi 值和 maxLineGap 以减少异常值.

输入图片

边缘图像

输出图像

杂项 - 给初学者的提示

  1. 许多计算机视觉算法都假设输入应该如何.在构建概念验证时,请始终尝试在应用此类算法之前查看您生成的中间输入.

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

  3. 要真正理解算法,请在必要时阅读 wiki 或更好的资源.然后再次/仍然执行上述 hack(第 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天全站免登陆