【youcans 的 OpenCV 例程200篇】157. 霍夫变换直线检测

欢迎关注 『youcans 的 OpenCV 例程 200 篇』 系列,持续更新中
欢迎关注 『youcans 的 OpenCV学习课』 系列,持续更新中


【youcans 的 OpenCV 例程200篇】157. 霍夫变换直线检测


2.9 霍夫变换

霍夫变换(Hough Transform)是图像处理中重要的特征检测技术,经常用来检测直线、曲线、圆和椭圆等特征形状。

对于给定的特征形状,算法在参数空间中通过计算累计空间的局部最大值,来获取符合特征形状的像素集合。也就是说,霍夫变换通过坐标空间变换,将图像空间中的特征形状映射到参数空间的点上形成峰值,从而把特征形状检测问题转化成了统计峰值问题。

以直线检测为例。直线在直角坐标系和极坐标系下的表达式分别为:
y = k   x + b x   c o s θ + y   s i n θ = ρ y = k \ x + b \\ x \ cos \theta + y \ sin \theta = \rho y=k x+bx cosθ+y sinθ=ρ
直角坐标系中通过点 ( x i , y i ) (x_i, y_i) (xi,yi) 的一条直线,在极坐标系中对应着一个点 ( ρ θ , θ ) (\rho_{\theta}, \theta) (ρθ,θ)。通过点 ( x i , y i ) (x_i,y_i) (xi,yi) 的直线有无数条,在极坐标中系中对应为一条正弦曲线。

将图像中所有的点从直角坐标系映射到极坐标系中。如果两个像素点在极坐标系中对应的两条曲线相交与某点,就表明这两个像素点在直角坐标系中通过一条直线。极坐标系中越多曲线相交于某点,则表明该点所表示的直角坐标系中的直线包括越多的像素点。因此可以通过计算极坐标系中经过某点的曲线数量,来检测直角坐标系中对应直线所包括的像素点的数量。

霍夫变换检测直线的原理,就是在极坐标系中追踪图像中每个点所对应曲线的交点。如果经过某点 ( ρ θ , θ ) (\rho_{\theta}, \theta) (ρθ,θ) 的曲线数量大于设定的阈值,就认为检测到了图像中的一条直线。

霍夫变换的具体实现步骤为:
(1)在参数空间 ( ρ , θ ) (\rho, \theta) (ρ,θ) 建立一个二维数组作为累加器(计数器);
(2)遍历图像中所有目标(黑色)像素,将每一个目标像素映射到参数空间的对应点,对应点的累加器加 1;
(3)求出参数空间中累加器的最大值,得到最大值点的位置 ( ρ θ , θ ) (\rho_{\theta}, \theta) (ρθ,θ)
(4)将参数空间中的最大值点位置 ( ρ θ , θ ) (\rho_{\theta}, \theta) (ρθ,θ) 映射回图像空间,得到对应的直线。

基于霍夫变换的边缘连接的具体实现步骤为:
(1)通过边缘检测获得一幅二值边缘图像;
(2)将参数空间 ( ρ , θ ) (\rho, \theta) (ρ,θ) 划分为若干个细分网格,作为累加器的单元;
(3)检查像素高度集中的累加器单元的数量;
(4)检查选中的累加器单元中像素间的关系。

OpenCV 支持三种不同的霍夫线变换:标准霍夫变换(Standard Hough Transform,SHT)、多尺度霍夫变换(Multi-Scale Hough Transform,MSHT)和累计概率霍夫变换(Progressive Probabilistic Hough Transform ,PPHT)。相关内容将在特征提取章节详细介绍,这里只简单介绍标准霍夫变换。

OpenCV 提供了函数 cv.HoughLines 实现标准霍夫变换查找二值图像中的线条。

函数说明:

cv.HoughLines(image, rho, theta, threshold[, lines[, srn[, stn[, min_theta[, max_theta]]]]]) → lines

参数说明:

  • image:输入二值图像,8-bit 单通道图像
  • lines:直线检测结果, 形状为 (n,1,2) 的Numpy 数组,每行两个元素 (rho, theta)
  • rho:距离分辨率(以像素为单位)
  • theta:角度分辨率(弧度)
  • threshold:累加器(Accumulator)阈值,大于阈值的直线才会被检测输出
  • srn: 可选项,多尺度霍夫变换变换中距离分辨率 rho 的除数
  • stn: 可选项,多尺度霍夫变换变换中角度分辨率 theta 的除数
  • min_theta:可选项,检查线条的最小角度,取值范围 (0,max_theta)
  • max_theta:可选项,检查线条的最大角度,取值范围 (min_theta,pi)

注意事项:

  • 函数参数中的 rho, theta 分别表示霍夫变换的距离分辨率、角度分辨率;而输出结果 lines 中的 rho 是距图像左上角 (0,0) 的距离,theta 为弧度表示的线旋转角度。

例程 11.12:霍夫变换直线检测

    # 11.12 霍夫变换直线检测
    img = cv2.imread("../images/Fig1034a.tif", flags=1)  # flags=1 读取为彩色图像
    imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    hImg, wImg = imgGray.shape

    # Canny 边缘检测, kSize 为高斯核大小,t1, t2为阈值大小
    ratio, low = 3, 120
    imgGauss = cv2.GaussianBlur(imgGray, (5, 5), 0)
    imgCanny = cv2.Canny(imgGauss, low, low*ratio)
    pEdge = np.where(imgCanny==255)  # 所有的边缘像素
    print(hImg, wImg, imgCanny.max(), imgCanny.min(), len(pEdge[0]))

    # # 霍夫线变换
    # # cv.HoughLines(image, rho, theta, threshold[, lines[, srn[, stn[, min_theta[, max_theta]]]]])
    # # rho 距离分辨率(以像素为单位), theta 角度分辨率(弧度)
    # # threshold 累加器阈值参数
    # # 返回:列表,形状为 (n,1,2) 的 Numpy 数组,每个元素 (n,1,:) 表示直线参数 rho, theta
    lines = cv2.HoughLines(imgCanny, 1, np.pi/180, threshold=120)  # lines: (n, 1, 2)
    print(lines.shape, type(lines))  # (11,1,2)
    imgEdge = img.copy()
    for i in range(8):
        rho, theta = lines[i,0,:]  # lines: (n,1,2)
        if (theta < (np.pi/4)) or (theta > (3*np.pi/4)):  # 直线与图像上下相交
            pt1 = (int(rho/np.cos(theta)), 0)  # (x,0), 直线与顶侧的交点
            pt2 = (int((rho - hImg * np.sin(theta))/np.cos(theta)), hImg)  # (x,h), 直线与底侧的交点
            cv2.line(imgEdge, pt1, pt2, (0, 0, 255))  # 绘制直线
        else:  # 直线与图像左右相交
            pt1 = (0, int(rho/np.sin(theta)))  # (0,y), 直线与左侧的交点
            pt2 = (wImg, int((rho - wImg * np.cos(theta))/np.sin(theta)))  # (w,y), 直线与右侧的交点
            cv2.line(imgEdge, pt1, pt2, (255, 0, 255), 1)  # 绘制直线
        print(rho, theta, pt1, pt2)

    # 累积概率霍夫变换
    # # cv.HoughLinesP(image, rho, theta, threshold[, lines[, minLineLength[, maxLineGap]]])
    # # rho 距离分辨率(以像素为单位), theta 角度分辨率(弧度)
    # # threshold 累加器阈值参数, minLineLength 最小直线长度, maxLineGap 最大允许间隔
    # # 返回:列表,每个元素是一个 4 元组,表示直线端点坐标 (x1, y1, x2, y2)
    minLineLength = 100  # 直线的最短长度
    maxLineGap = 20  # 线段之间最大间隔
    lines = cv2.HoughLinesP(imgCanny, 1, np.pi/180, 100, minLineLength, maxLineGap)  # lines: (n,1,4)
    for line in lines:
        x1, y1, x2, y2 = line[0]
        cv2.line(imgEdge, (x1,y1), (x2,y2), (255, 215, 0), 2)  # 绘制直线

    plt.figure(figsize=(9, 5))
    plt.subplot(131), plt.title("Origin"), plt.imshow(imgGray, cmap='gray'), plt.axis('off')
    plt.subplot(132), plt.title("Canny"), plt.imshow(imgCanny, cmap='gray'), plt.axis('off')
    plt.subplot(133), plt.title("Hough"), plt.imshow(cv2.cvtColor(imgEdge, cv2.COLOR_RGB2BGR)), plt.axis('off')
    plt.tight_layout()
    plt.show()

在这里插入图片描述


(本节完)


版权声明:

youcans@xupt 原创作品,转载必须标注原文链接:(https://blog.csdn.net/youcans/article/details/124181211)

Copyright 2022 youcans, XUPT
Crated:2022-4-10


欢迎关注 『youcans 的 OpenCV 例程 200 篇』 系列,持续更新中
欢迎关注 『youcans 的 OpenCV学习课』 系列,持续更新中

【youcans 的 OpenCV 例程200篇】01. 图像的读取(cv2.imread)
【youcans 的 OpenCV 例程200篇】02. 图像的保存(cv2.imwrite)
【youcans 的 OpenCV 例程200篇】03. 图像的显示(cv2.imshow)
【youcans 的 OpenCV 例程200篇】04. 用 matplotlib 显示图像(plt.imshow)
【youcans 的 OpenCV 例程200篇】05. 图像的属性(np.shape)
【youcans 的 OpenCV 例程200篇】06. 像素的编辑(img.itemset)
【youcans 的 OpenCV 例程200篇】07. 图像的创建(np.zeros)
【youcans 的 OpenCV 例程200篇】08. 图像的复制(np.copy)
【youcans 的 OpenCV 例程200篇】09. 图像的裁剪(cv2.selectROI)
【youcans 的 OpenCV 例程200篇】10. 图像的拼接(np.hstack)
【youcans 的 OpenCV 例程200篇】11. 图像通道的拆分(cv2.split)
【youcans 的 OpenCV 例程200篇】12. 图像通道的合并(cv2.merge)
【youcans 的 OpenCV 例程200篇】13. 图像的加法运算(cv2.add)
【youcans 的 OpenCV 例程200篇】14. 图像与标量相加(cv2.add)
【youcans 的 OpenCV 例程200篇】15. 图像的加权加法(cv2.addWeight)
【youcans 的 OpenCV 例程200篇】16. 不同尺寸的图像加法
【youcans 的 OpenCV 例程200篇】17. 两张图像的渐变切换
【youcans 的 OpenCV 例程200篇】18. 图像的掩模加法
【youcans 的 OpenCV 例程200篇】19. 图像的圆形遮罩
【youcans 的 OpenCV 例程200篇】20. 图像的按位运算
【youcans 的 OpenCV 例程200篇】21. 图像的叠加
【youcans 的 OpenCV 例程200篇】22. 图像添加非中文文字
【youcans 的 OpenCV 例程200篇】23. 图像添加中文文字
【youcans 的 OpenCV 例程200篇】24. 图像的仿射变换
【youcans 的 OpenCV 例程200篇】25. 图像的平移
【youcans 的 OpenCV 例程200篇】26. 图像的旋转(以原点为中心)
【youcans 的 OpenCV 例程200篇】27. 图像的旋转(以任意点为中心)
【youcans 的 OpenCV 例程200篇】28. 图像的旋转(直角旋转)
【youcans 的 OpenCV 例程200篇】29. 图像的翻转(cv2.flip)
【youcans 的 OpenCV 例程200篇】30. 图像的缩放(cv2.resize)
【youcans 的 OpenCV 例程200篇】31. 图像金字塔(cv2.pyrDown)
【youcans 的 OpenCV 例程200篇】32. 图像的扭变(错切)
【youcans 的 OpenCV 例程200篇】33. 图像的复合变换
【youcans 的 OpenCV 例程200篇】34. 图像的投影变换
【youcans 的 OpenCV 例程200篇】35. 图像的投影变换(边界填充)
【youcans 的 OpenCV 例程200篇】36. 直角坐标与极坐标的转换
【youcans 的 OpenCV 例程200篇】37. 图像的灰度化处理和二值化处理
【youcans 的 OpenCV 例程200篇】38. 图像的反色变换(图像反转)
【youcans 的 OpenCV 例程200篇】39. 图像灰度的线性变换
【youcans 的 OpenCV 例程200篇】40. 图像分段线性灰度变换
【youcans 的 OpenCV 例程200篇】41. 图像的灰度变换(灰度级分层)
【youcans 的 OpenCV 例程200篇】42. 图像的灰度变换(比特平面分层)
【youcans 的 OpenCV 例程200篇】43. 图像的灰度变换(对数变换)
【youcans 的 OpenCV 例程200篇】44. 图像的灰度变换(伽马变换)
【youcans 的 OpenCV 例程200篇】45. 图像的灰度直方图
【youcans 的 OpenCV 例程200篇】46. 直方图均衡化
【youcans 的 OpenCV 例程200篇】47. 图像增强—直方图匹配
【youcans 的 OpenCV 例程200篇】48. 图像增强—彩色直方图匹配
【youcans 的 OpenCV 例程200篇】49. 图像增强—局部直方图处理
【youcans 的 OpenCV 例程200篇】50. 图像增强—直方图统计量图像增强
【youcans 的 OpenCV 例程200篇】51. 图像增强—直方图反向追踪
【youcans 的 OpenCV 例程200篇】52. 图像的相关与卷积运算
【youcans 的 OpenCV 例程200篇】53. Scipy 实现图像二维卷积
【youcans 的 OpenCV 例程200篇】54. OpenCV 实现图像二维卷积
【youcans 的 OpenCV 例程200篇】55. 可分离卷积核
【youcans 的 OpenCV 例程200篇】56. 低通盒式滤波器
【youcans 的 OpenCV 例程200篇】57. 低通高斯滤波器
【youcans 的 OpenCV 例程200篇】58. 非线性滤波—中值滤波
【youcans 的 OpenCV 例程200篇】59. 非线性滤波—双边滤波
【youcans 的 OpenCV 例程200篇】60. 非线性滤波—联合双边滤波
【youcans 的 OpenCV 例程200篇】61. 导向滤波(Guided filter)
【youcans 的 OpenCV 例程200篇】62. 图像锐化——钝化掩蔽
【youcans 的 OpenCV 例程200篇】63. 图像锐化——Laplacian 算子
【youcans 的 OpenCV 例程200篇】64. 图像锐化——Sobel 算子
【youcans 的 OpenCV 例程200篇】65. 图像锐化——Scharr 算子
【youcans 的 OpenCV 例程200篇】66. 图像滤波之低通/高通/带阻/带通
【youcans 的 OpenCV 例程200篇】67. 空间域图像增强的综合应用
【youcans 的 OpenCV 例程200篇】68. 空间域图像增强的综合应用
【youcans 的 OpenCV 例程200篇】69. 连续非周期信号的傅立叶系数
【youcans 的 OpenCV 例程200篇】70. 一维连续函数的傅里叶变换
【youcans 的 OpenCV 例程200篇】71. 连续函数的取样
【youcans 的 OpenCV 例程200篇】72. 一维离散傅里叶变换
【youcans 的 OpenCV 例程200篇】73. 二维连续傅里叶变换
【youcans 的 OpenCV 例程200篇】74. 图像的抗混叠
【youcans 的 OpenCV 例程200篇】75. Numpy 实现图像傅里叶变换
【youcans 的 OpenCV 例程200篇】76. OpenCV 实现图像傅里叶变换
【youcans 的 OpenCV 例程200篇】77. OpenCV 实现快速傅里叶变换
【youcans 的 OpenCV 例程200篇】78. 频率域图像滤波基础
【youcans 的 OpenCV 例程200篇】79. 频率域图像滤波的基本步骤
【youcans 的 OpenCV 例程200篇】80. 频率域图像滤波详细步骤
【youcans 的 OpenCV 例程200篇】81. 频率域高斯低通滤波器
【youcans 的 OpenCV 例程200篇】82. 频率域巴特沃斯低通滤波器
【youcans 的 OpenCV 例程200篇】83. 频率域低通滤波:印刷文本字符修复
【youcans 的 OpenCV 例程200篇】84. 由低通滤波器得到高通滤波器
【youcans 的 OpenCV 例程200篇】85. 频率域高通滤波器的应用
【youcans 的 OpenCV 例程200篇】86. 频率域滤波应用:指纹图像处理
【youcans 的 OpenCV 例程200篇】87. 频率域钝化掩蔽
【youcans 的 OpenCV 例程200篇】88. 频率域拉普拉斯高通滤波
【youcans 的 OpenCV 例程200篇】89. 带阻滤波器的传递函数
【youcans 的 OpenCV 例程200篇】90. 频率域陷波滤波器
【youcans 的 OpenCV 例程200篇】91. 高斯噪声、瑞利噪声、爱尔兰噪声
【youcans 的 OpenCV 例程200篇】92. 指数噪声、均匀噪声、椒盐噪声
【youcans 的 OpenCV 例程200篇】93. 噪声模型的直方图
【youcans 的 OpenCV 例程200篇】94. 算术平均滤波器
【youcans 的 OpenCV 例程200篇】95. 几何均值滤波器
【youcans 的 OpenCV 例程200篇】96. 谐波平均滤波器
【youcans 的 OpenCV 例程200篇】97. 反谐波平均滤波器
【youcans 的 OpenCV 例程200篇】98. 统计排序滤波器
【youcans 的 OpenCV 例程200篇】99. 修正阿尔法均值滤波器
【youcans 的 OpenCV 例程200篇】100. 自适应局部降噪滤波器
【youcans 的 OpenCV 例程200篇】101. 自适应中值滤波器
【youcans 的 OpenCV 例程200篇】102. 陷波带阻滤波器的传递函数
【youcans 的 OpenCV 例程200篇】103. 陷波带阻滤波器消除周期噪声干扰
【youcans 的 OpenCV 例程200篇】104. 运动模糊退化模型
【youcans 的 OpenCV 例程200篇】105. 湍流模糊退化模型
【youcans 的 OpenCV 例程200篇】106. 退化图像的逆滤波
【youcans 的 OpenCV 例程200篇】107. 退化图像的维纳滤波
【youcans 的 OpenCV 例程200篇】108. 约束最小二乘方滤波
【youcans 的 OpenCV 例程200篇】109. 几何均值滤波
【youcans 的 OpenCV 例程200篇】110. 投影和雷登变换
【youcans 的 OpenCV 例程200篇】111. 雷登变换反投影重建图像
【youcans 的 OpenCV 例程200篇】112. 滤波反投影重建图像
【youcans 的 OpenCV 例程200篇】113. 形态学操作之腐蚀
【youcans 的 OpenCV 例程200篇】114. 形态学操作之膨胀
【youcans 的 OpenCV 例程200篇】115. 形态学操作之开运算
【youcans 的 OpenCV 例程200篇】116. 形态学操作之闭运算
【youcans 的 OpenCV 例程200篇】117. 形态学操作之顶帽运算
【youcans 的 OpenCV 例程200篇】118. 形态学操作之底帽运算
【youcans 的 OpenCV 例程200篇】119. 图像的形态学梯度
【youcans 的 OpenCV 例程200篇】120. 击中-击不中变换
【youcans 的 OpenCV 例程200篇】121. 击中-击不中用于特征识别
【youcans 的 OpenCV 例程200篇】122. 形态算法之边界提取
【youcans 的 OpenCV 例程200篇】123. 形态算法之孔洞填充
【youcans 的 OpenCV 例程200篇】124. 孔洞填充的泛洪算法
【youcans 的 OpenCV 例程200篇】125. 形态算法之提取连通分量
【youcans 的 OpenCV 例程200篇】126. 形态算法之凸壳
【youcans 的 OpenCV 例程200篇】127. 形态算法之细化
【youcans 的 OpenCV 例程200篇】128. 形态算法之骨架 (skimage)
【youcans 的 OpenCV 例程200篇】129. 形态算法之骨架 (重建开运算)
【youcans 的 OpenCV 例程200篇】130. 形态学之提取水平和垂直线
【youcans 的 OpenCV 例程200篇】131. 形态学重建之竖线字符提取
【youcans 的 OpenCV 例程200篇】132. 形态学重建之孔洞填充算法
【youcans 的 OpenCV 例程200篇】133. 形态学重建之边界清除
【youcans 的 OpenCV 例程200篇】134. 形态学重建之细胞计数
【youcans 的 OpenCV 例程200篇】135. 形态学重建之粒度测定
【youcans 的 OpenCV 例程200篇】136. 灰度腐蚀和灰度膨胀
【youcans 的 OpenCV 例程200篇】137. 灰度开运算和灰度闭运算原理
【youcans 的 OpenCV 例程200篇】138. 灰度开运算和灰度闭运算
【youcans 的 OpenCV 例程200篇】139. 灰度顶帽变换校正阴影
【youcans 的 OpenCV 例程200篇】140. 灰度底帽变换校正光照
【youcans 的 OpenCV 例程200篇】141. 灰度底帽变换的三维地形图
【youcans 的 OpenCV 例程200篇】142. 基于灰度形态学的图像平滑
【youcans 的 OpenCV 例程200篇】143. 基于灰度形态学的粒度测定
【youcans 的 OpenCV 例程200篇】144. 基于灰度形态学的纹理分割
【youcans 的 OpenCV 例程200篇】145. 形态学之边缘和角点检测
【youcans 的 OpenCV 例程200篇】146. 基于灰度形态学的复杂背景图像重建
【youcans 的 OpenCV 例程200篇】147. 图像分割之孤立点检测
【youcans 的 OpenCV 例程200篇】148. 图像分割之线检测
【youcans 的 OpenCV 例程200篇】149. 图像分割之边缘模型
【youcans 的 OpenCV 例程200篇】150. 边缘检测梯度算子
【youcans 的 OpenCV 例程200篇】151. 边缘检测中的平滑处理
【youcans 的 OpenCV 例程200篇】152. 边缘检测之 LoG 算子
【youcans 的 OpenCV 例程200篇】153. 边缘检测之 DoG 算子
【youcans 的 OpenCV 例程200篇】154. 边缘检测之 Canny 算子
【youcans 的 OpenCV 例程200篇】155. 边缘连接的局部处理方法
【youcans 的 OpenCV 例程200篇】156. 边缘连接局部处理的简化算法
【youcans 的 OpenCV 例程200篇】157. 霍夫变换直线检测

猜你喜欢

转载自blog.csdn.net/youcans/article/details/124181211