利用Opencv中Houghline方法实现直线检测
技术  /  管理员 发布于 7年前   214
利用Opencv中的Houghline方法进行直线检测―python语言
这是给Python部落翻译的文章,请在这里看原文。
在图像处理中,霍夫变换用来检测任意能够用数学公式表达的形状,即使这个形状被破坏或者有点扭曲。
下面我们将看到利用HoughLine算法来阐述霍夫变化进行直线检测的原理,把此算法应用到特点图像的边缘检测是可取的。边缘检测方法请参考这篇文章C边缘检测。
Houghline算法基础
直线可以表示为y=mx+c,或者以极坐标形式表示为r=xcosθ+ysinθ,其中r是原点到直线的垂直距离,θ是水平轴顺时针方向到垂直线的夹角(这个方向取决于坐标的形式,在OpenCV就是采用这种极坐标形式)。
因此任意的曲线都可以用两个参数(r,θ)表示。
HoughLine算法原理:
例子:
假设一幅100x100的图像,在图像中间有一条水平直线。设直线的第一个点的坐标为(x,y),在直线方程中,令参数θ=0,12,⋯,180,观查参数r变化。对每一个参数对(r,θ),在累加器中将(r,θ)对应的单元格中的值递增1,比如现在在累加器中,某个单元(50,90)的值等于1,其它的值也如此。
对于直线上的第二个点,重复上述操作。将得到的参数对(r,θ)的对应值继续递增,然后(50,90)对应的值等于2。实现上我们是对参数对(r,θ)进行投票,对直线上的每一个点重复上述操作,对每一个点,单元格(50,90)对应的值会递增,或者说投票给参数对(50,90),而会或者不会投票给其它参数对。以这种方式,最后单元格(50,90)的值将会是最大的值。然后搜索累加器的最大值,将会找到参数对(50,90)。也就是说,在图像中找到了到原点距离为50,角度为90的一条直线。
上述算法的过程被封装成OpenCV函数cv2.HoughLines(),函数返回(r,θ)的一个数组,其中r的单位为像素,θ的单位为弧度。
# Python program to illustrate HoughLine# method for line detectionimport cv2import numpy as np# Reading the required image in # which operations are to be done. # Make sure that the image is in the same # directory in which this python program isimg = cv2.imread('xyz.jpg')# Convert the img to grayscalegray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)# Apply edge detection method on the imageedges = cv2.Canny(gray,50,150,apertureSize = 3)# This returns an array of r and theta valueslines = cv2.HoughLines(edges,1,np.pi/180, 200)# The below for loop runs till r and theta values # are in the range of the 2d arrayfor r,theta in lines[0]: # Stores the value of cos(theta) in a a = np.cos(theta) # Stores the value of sin(theta) in b b = np.sin(theta) # x0 stores the value rcos(theta) x0 = a*r # y0 stores the value rsin(theta) y0 = b*r # x1 stores the rounded off value of (rcos(theta)-1000sin(theta)) x1 = int(x0 + 1000*(-b)) # y1 stores the rounded off value of (rsin(theta)+1000cos(theta)) y1 = int(y0 + 1000*(a)) # x2 stores the rounded off value of (rcos(theta)+1000sin(theta)) x2 = int(x0 - 1000*(-b)) # y2 stores the rounded off value of (rsin(theta)-1000cos(theta)) y2 = int(y0 - 1000*(a)) # cv2.line draws a line in img from the point(x1,y1) to (x2,y2). # (0,0,255) denotes the colour of the line to be #drawn. In this case, it is red. cv2.line(img,(x1,y1), (x2,y2), (0,0,255),2)# All the changes made in the input image are finally# written on a new image houghlines.jpgcv2.imwrite('houghlines3.jpg', img)
函数(cv2.HoughLines(edges, 1, np.pi/180,200))
总结
霍夫变换的应用
文章作者是Pratima Upadhyay,如果你喜欢GeeksforGeeks,并且愿意分享,可以利用contribute.geeksforgeeks.org写文章,然后发送到[email protected],在GeeksforGeeks中看到自己的文章,帮助更多的Geeks。
若发现错误欢迎指正,也欢迎评论。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号