Python+OpenCV:Hough直线检测(Hough Line Transform)

理论

A line can be represented as  or in a parametric form, as  where ρ is the perpendicular distance(垂直距离) from the origin to the line,

and θ is the angle formed by this perpendicular line and the horizontal axis measured in counter-clockwise (That direction varies on how you represent the coordinate system.

This representation is used in OpenCV). Check the image below:

So if the line is passing below the origin, it will have a positive rho and an angle less than 180.

If it is going above the origin, instead of taking an angle greater than 180, the angle is taken less than 180, and rho is taken negative.

Any vertical line will have 0 degree and horizontal lines will have 90 degree.

Now let's see how the Hough Transform works for lines.

Any line can be represented in these two terms, (ρ,θ).

So first it creates a 2D array or accumulator (to hold the values of the two parameters) and it is set to 0 initially.

Let rows denote the ρ and columns denote the θ.

Size of array depends on the accuracy you need.

Suppose you want the accuracy of angles to be 1 degree, you will need 180 columns.

For ρ, the maximum distance possible is the diagonal length of the image.

So taking one pixel accuracy, the number of rows can be the diagonal length of the image.

Consider a 100x100 image with a horizontal line at the middle.

Take the first point of the line. You know its (x,y) values.

Now in the line equation, put the values θ=0,1,2,....,180 and check the ρ you get.

For every (ρ,θ) pair, you increment value by one in our accumulator in its corresponding (ρ,θ) cells.

So now in accumulator, the cell (50,90) = 1 along with some other cells.

Now take the second point on the line.

Do the same as above. Increment the values in the cells corresponding to (rho, theta) you got.

This time, the cell (50,90) = 2. What you actually do is voting the (ρ,θ) values.

You continue this process for every point on the line.

At each point, the cell (50,90) will be incremented or voted up, while other cells may or may not be voted up.

This way, at the end, the cell (50,90) will have maximum votes.

So if you search the accumulator for maximum votes, you get the value (50,90) which says, there is a line in this image at a distance 50 from the origin and at angle 90 degrees.

It is well shown in the below animation (Image Courtesy: Amos Storkey )

This is how hough transform works for lines. It is simple, and may be you can implement it using Numpy on your own. Below is an image which shows the accumulator. Bright spots at some locations denote they are the parameters of possible lines in the image. (Image courtesy: Wikipedia )

Hough Transform in OpenCV

####################################################################################################
# Hough直线检测(Hough Line Transform)
def lmc_cv_image_hough_line_transform():"""函数功能: Hough直线检测(Hough Line Transform)。"""# 读取图像image = lmc_cv.imread('D:/99-Research/Python/Image/football_field.jpg', flags=lmc_cv.IMREAD_UNCHANGED)rgb_image1 = lmc_cv.cvtColor(image, lmc_cv.COLOR_BGR2RGB)rgb_image2 = rgb_image1.copy()gray_image = lmc_cv.cvtColor(rgb_image1, lmc_cv.COLOR_RGB2GRAY)pyplot.figure('Image Display')pyplot.subplot(2, 2, 1)pyplot.imshow(rgb_image1, cmap='gray')pyplot.title('Original Image')pyplot.xticks([])pyplot.yticks([])# 边缘检测ret, thresh_image = lmc_cv.threshold(gray_image, 160, 255, lmc_cv.THRESH_BINARY)edges_image = lmc_cv.Canny(thresh_image, 50, 250, apertureSize=3)pyplot.subplot(2, 2, 2)pyplot.imshow(edges_image, cmap='gray')pyplot.title('Edges Image')pyplot.xticks([])pyplot.yticks([])# Hough直线检测(Hough Line Transform)lines = lmc_cv.HoughLines(edges_image, rho=3, theta=np.pi / 90, threshold=300)for line in lines:rho, theta = line[0]a = np.cos(theta)b = np.sin(theta)x0 = a * rhoy0 = b * rhox1 = int(x0 + 1000 * (-b))y1 = int(y0 + 1000 * a)x2 = int(x0 - 1000 * (-b))y2 = int(y0 - 1000 * a)lmc_cv.line(rgb_image1, (x1, y1), (x2, y2), (255, 0, 0), 2)pyplot.subplot(2, 2, 3)pyplot.imshow(rgb_image1, cmap='gray')pyplot.title('Hough Transform Image')pyplot.xticks([])pyplot.yticks([])# 概率Hough直线检测(Probabilistic Hough Line Transform)probabilistic_lines = lmc_cv.HoughLinesP(edges_image, rho=3, theta=np.pi / 180, threshold=100, minLineLength=100,maxLineGap=10)for line in probabilistic_lines:x1, y1, x2, y2 = line[0]lmc_cv.line(rgb_image2, (x1, y1), (x2, y2), (0, 0, 255), 2)pyplot.subplot(2, 2, 4)pyplot.imshow(rgb_image2, cmap='gray')pyplot.title('Probabilistic Hough Transform Image')pyplot.xticks([])pyplot.yticks([])# 根据用户输入保存图像if ord("q") == (lmc_cv.waitKey(0) & 0xFF):# 销毁窗口pyplot.close('all')return

Python+OpenCV:Hough直线检测(Hough Line Transform)相关推荐

  1. opencv判断 线夹角_python opencv实现直线检测并测出倾斜角度(附源码+注释)

    由于学习需要,我想要检测出图片中的直线,并且得到这些直线的角度.于是我在网上搜了好多直线检测的代码,但是没有搜到附有计算直线倾斜角度的代码,所以我花了一点时间,自己写了一份直线检测并测出倾斜角度的代码 ...

  2. Python+OpenCV:Hough圆检测(Hough Circle Transform)

    Python+OpenCV:Hough圆检测(Hough Circle Transform) ##################################################### ...

  3. 史上最详细的Hough直线检测

    之前写过检测车道线的文章:https://blog.csdn.net/u010712012/article/details/84780943 最后可以检测出两条车道线,但是,本课题的目的是通过提供一张 ...

  4. matlab实现彩色图像的hough直线检测

    matlab实现彩色图像的hough直线检测 原理描述 代码 实验结果 原理描述 霍夫变换是用来检测图像中的直线或者圆等几何图形的.一条直线的表示方法有好多种,最常见的是 y=mx+b 的形式. 假设 ...

  5. python+opencv车道线检测(简易实现)

    python+opencv车道线检测(简易实现) 技术栈:python+opencv 实现思路: canny边缘检测获取图中的边缘信息: 霍夫变换寻找图中直线: 绘制梯形感兴趣区域获得车前范围: 得到 ...

  6. 基于Python+OpenCV车道线检测(直道和弯道)

    基于Python+OpenCV车道线检测(直道和弯道) 基于Python+OpenCV车道线检测(直道和弯道)

  7. opencv+hough直线检测+fitline直线拟合

    #include <iostream> #include <opencv2/highgui/highgui.hpp> #include <opencv2/core/cor ...

  8. 在Python中使用OpenCV进行直线检测

    1. 引言 在图像处理中,直线检测是一种常见的算法,它通常获取n个边缘点的集合,并找到通过这些边缘点的直线.其中用于直线检测,最为流行的检测器是基于霍夫变换的直线检测技术. 2. 霍夫变换 霍夫变换是 ...

  9. Hough直线检测的原理与实现

     霍夫变换就是通过图形的一种表示模式,加上一种转换方法,把图形的点集投射到一个点上以便检测. 标准直线Hough变换采用如下参数化直线方程: x*cosθ+y*sinθ=ρ             ...

最新文章

  1. oracle数据库性能awr,常见问题:如何使用AWR报告来诊断数据库性能问题
  2. 各种机器学习方法的优缺点
  3. 复盘无人业态的三点心得:起于共享单车,止于何?
  4. oracle登录账号和密码,oracle 登录账号与密码oracle按照中文排序
  5. 深圳大学计算机科学专业排名,深圳大学专业排名及介绍 哪些专业最好
  6. 使用 ipmitool 实现远程管理Dell 系列服务器
  7. leetcode 442. Find All Duplicates in an Array | 442. 数组中重复的数据(位运算)
  8. linux location root访问文件夹404_如何使网站支持https访问?nginx配置https证书
  9. 爬山算法和模拟退火算法简介(转)
  10. Docker 下载 JDK 镜像(docker search 、docker pull)
  11. PHP使用CURL抓取页面
  12. 运维Python大全
  13. VUE 自定义取色器组件
  14. 烽火mysql数据库安装_sql/mysql3.md · zack烽火/knowledge - Gitee.com
  15. Vision Transformer模型/论文详解
  16. Echarts数据可视化总结
  17. Node-RED使用指南:7:配置与设定总结:其他配置
  18. Maven整合阿里云云效制品仓库 Packages(私服)
  19. 337个人写了一封信,然后北京地铁有了无障碍地图
  20. Nodejs实现给手机发送短信验证码用于登录功能(免费短信)

热门文章

  1. User Interaction Design
  2. 学习GRPC(一) 简单实现
  3. java Calendar的学习分享
  4. 2017 西安网络赛A Tree(树上静态查询,带权并查集,矩阵乘法压位,好题)
  5. How to setup a DL4J project with eclipse
  6. Hibernate中saveOrUpdate()和merge()的区别
  7. FLEX APIs、Libs、Components
  8. 【C/C++】通过无类型指针实现泛型拷贝(内存拷贝)
  9. linux之同时监控多个日志文件变化
  10. display环境变量如何配置_JDK 安装 Java环境变量配置