【youcans 的 OpenCV 例程200篇】157. 霍夫变换直线检测
歡迎關注 『youcans 的 OpenCV 例程 200 篇』 系列,持續更新中
歡迎關注 『youcans 的 OpenCV學習課』 系列,持續更新中
【youcans 的 OpenCV 例程200篇】157. 霍夫變換直線檢測
2.9 霍夫變換
霍夫變換(Hough Transform)是圖像處理中重要的特征檢測技術,經常用來檢測直線、曲線、圓和橢圓等特征形狀。
對于給定的特征形狀,算法在參數空間中通過計算累計空間的局部最大值,來獲取符合特征形狀的像素集合。也就是說,霍夫變換通過坐標空間變換,將圖像空間中的特征形狀映射到參數空間的點上形成峰值,從而把特征形狀檢測問題轉化成了統計峰值問題。
以直線檢測為例。直線在直角坐標系和極坐標系下的表達式分別為:
y=kx+bxcosθ+ysinθ=ρy = k \ x + b \\ x \ cos \theta + y \ sin \theta = \rho y=k?x+bx?cosθ+y?sinθ=ρ
直角坐標系中通過點 (xi,yi)(x_i, y_i)(xi?,yi?) 的一條直線,在極坐標系中對應著一個點 (ρθ,θ)(\rho_{\theta}, \theta)(ρθ?,θ)。通過點 (xi,yi)(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, 120imgGauss = 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, thetalines = 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. 霍夫變換直線檢測
總結
以上是生活随笔為你收集整理的【youcans 的 OpenCV 例程200篇】157. 霍夫变换直线检测的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 山东大学 2020级数据库系统 实验七
- 下一篇: 时间序列的预处理