【youcans 的 OpenCV 例程200篇】152. 边缘检测之 LoG 算子
歡迎關(guān)注 『youcans 的 OpenCV 例程 200 篇』 系列,持續(xù)更新中
歡迎關(guān)注 『youcans 的 OpenCV學(xué)習(xí)課』 系列,持續(xù)更新中
【youcans 的 OpenCV 例程200篇】152. 邊緣檢測(cè)之 LoG 算子(Marr-Hildreth 算法)
2.5 LoG 邊緣檢測(cè)算子(Marr-Hildreth 算法)
Marr-Hildreth 算法是改進(jìn)的邊緣檢測(cè)算子,是平滑算子與 Laplace 算子的結(jié)合,因而兼具平滑和二階微分的作用,其定位精度高,邊緣連續(xù)性好,計(jì)算速度快。
Marr-Hildreth 算子的原理是,灰度變化與圖像尺度是相關(guān)的,因此需要使用不同尺度的微分算子:大算子可以檢測(cè)模糊的邊緣,而小算子可以檢測(cè)清晰的細(xì)節(jié)。
濾波器 ?2G\nabla ^2 G?2G 可以很好地滿(mǎn)足該條件, ?2\nabla ^2?2 是拉普拉斯算子,G 是標(biāo)準(zhǔn)差為 σ\sigmaσ 的二維高斯函數(shù)。
?2G(x,y)=(x2+y2?2σ2σ4)e?(x2+y2)/2σ2\nabla ^2 G(x,y) = \Big( \frac{x^2 + y^2 - {2 \sigma ^2}}{\sigma ^4} \Big) e^{- {(x^2 + y^2)} / {2 \sigma ^2}} ?2G(x,y)=(σ4x2+y2?2σ2?)e?(x2+y2)/2σ2
上式就是高斯拉普拉斯函數(shù)(Laplacian of the Gaussina ),又稱(chēng)為 LoG 濾波器、墨西哥草帽算子。
具體地,可以先對(duì)圖像做 Gauss 平滑,再做 Laplace 變換;也可以預(yù)先算出高斯拉普拉斯卷積核 LoG,直接與圖像卷積來(lái)實(shí)現(xiàn),提高計(jì)算速度。
當(dāng) LoG 卷積核的標(biāo)準(zhǔn)差 σ\sigmaσ 取不同值時(shí),可以檢測(cè)不同尺度下圖像的強(qiáng)度變化 。減小標(biāo)準(zhǔn)差有利于檢測(cè)影像細(xì)節(jié),增大標(biāo)準(zhǔn)差有利于檢測(cè)輪廓。參考高斯分布的 3σ3 \sigma3σ 原則(概率 99.7%),可以選擇大于 6σ6\sigma6σ 的最小奇數(shù)作為模板大小,過(guò)大的標(biāo)準(zhǔn)差并不會(huì)提高效果反而會(huì)增加計(jì)算量。
標(biāo)準(zhǔn)差為 1 的 5*5 的近似的 LoG 卷積核為:
KMH,5=[00?1000?1?2?10?1?216?2?10?1?2?1000?100]K_{MH,5} = \begin{bmatrix} 0 & 0 & -1 & 0 & 0\\ 0 & -1 & -2 & -1 & 0\\ -1 & -2 & 16 & -2 & -1\\ 0 & -1 & -2 & -1 & 0\\ 0 & 0 & -1 & 0 & 0 \end{bmatrix} KMH,5?=???????00?100?0?1?2?10??1?216?2?1?0?1?2?10?00?100????????
該卷積核是示例性的,對(duì)應(yīng)的標(biāo)準(zhǔn)差約為 0.5,由于卷積核寬度太小檢測(cè)邊緣的效果并不好。
Marr-Hildreth 算法讓 LoG 核與原圖像卷積,然后尋找過(guò)零點(diǎn)來(lái)確定邊緣的位置。
例程 11.6:LoG 邊緣檢測(cè)算子(Marr-Hildreth 算法)
# 11.6 LoG 邊緣檢測(cè)算子 (Marr-Hildreth 算法)def ZeroDetect(img): # 判斷零交叉點(diǎn)h, w = img.shape[0], img.shape[1]zeroCrossing = np.zeros_like(img, np.uint8)for x in range(0, w-1):for y in range(0, h-1):if img[y][x] < 0:if (img[y][x-1] > 0) or (img[y][x+1] > 0)\or (img[y-1][x] > 0) or (img[y+1][x] > 0):zeroCrossing[y][x] = 255return zeroCrossingfrom scipy import signalimg = cv2.imread("../images/Fig1016a.tif", flags=0) # flags=0 讀取為灰度圖像imgBlur = cv2.blur(img, (3,3)) # Blur 平滑后再做 Laplacian 變換# 近似的 Marr-Hildreth 卷積核 (5*5)kernel_MH5 = np.array([[0, 0, -1, 0, 0],[0, -1, -2, -1, 0],[-1, -2, 16, -2, -1],[0, -1, -2, -1, 0],[0, 0, -1, 0, 0]])imgMH5 = signal.convolve2d(imgBlur, kernel_MH5, boundary='symm', mode='same') # 卷積計(jì)算zeroMH5 = ZeroDetect(imgMH5) # 判斷零交叉點(diǎn)# 由 Gauss 標(biāo)準(zhǔn)差計(jì)算 Marr-Hildreth 卷積核sigma = 3 # Gauss 標(biāo)準(zhǔn)差,輸入?yún)?shù)size = int(2 * round(3 * sigma)) + 1 # 根據(jù)標(biāo)準(zhǔn)差確定窗口大小,3*sigma 占比 99.7%print("sigma={:d}, size={}".format(sigma, size))x, y = np.meshgrid(np.arange(-size/2+1, size/2+1), np.arange(-size/2+1, size/2+1)) # 生成網(wǎng)格norm2 = np.power(x, 2) + np.power(y, 2)sigma2, sigma4 = np.power(sigma, 2), np.power(sigma, 4)kernelLoG = ((norm2 - (2.0 * sigma2)) / sigma4) * np.exp(- norm2 / (2.0 * sigma2)) # 計(jì)算 LoG 卷積核# Marr-Hildreth 卷積運(yùn)算imgLoG = signal.convolve2d(imgBlur, kernelLoG, boundary='symm', mode='same') # 卷積計(jì)算# 判斷零交叉點(diǎn)zeroCrossing = ZeroDetect(imgLoG)plt.figure(figsize=(10, 7))plt.subplot(221), plt.title("Marr-Hildreth (sigma=0.5)"), plt.imshow(imgMH5, cmap='gray'), plt.axis('off')plt.subplot(222), plt.title("Marr-Hildreth (sigma=3)"), plt.imshow(imgLoG, cmap='gray'), plt.axis('off')plt.subplot(223), plt.title("Zero crossing (size=5)"), plt.imshow(zeroMH5, cmap='gray'), plt.axis('off')plt.subplot(224), plt.title("Zero crossing (size=19)"), plt.imshow(zeroCrossing, cmap='gray'), plt.axis('off')plt.tight_layout()plt.show()(本節(jié)完)
版權(quán)聲明:
youcans@xupt 原創(chuàng)作品,轉(zhuǎn)載必須標(biāo)注原文鏈接:(https://blog.csdn.net/youcans/article/details/124091970)
Copyright 2022 youcans, XUPT
Crated:2022-4-10
歡迎關(guān)注 『youcans 的 OpenCV 例程 200 篇』 系列,持續(xù)更新中
歡迎關(guān)注 『youcans 的 OpenCV學(xué)習(xí)課』 系列,持續(xù)更新中
【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. 圖像的創(chuàng)建(np.zeros)
【youcans 的 OpenCV 例程200篇】08. 圖像的復(fù)制(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. 圖像的加法運(yùn)算(cv2.add)
【youcans 的 OpenCV 例程200篇】14. 圖像與標(biāo)量相加(cv2.add)
【youcans 的 OpenCV 例程200篇】15. 圖像的加權(quán)加法(cv2.addWeight)
【youcans 的 OpenCV 例程200篇】16. 不同尺寸的圖像加法
【youcans 的 OpenCV 例程200篇】17. 兩張圖像的漸變切換
【youcans 的 OpenCV 例程200篇】18. 圖像的掩模加法
【youcans 的 OpenCV 例程200篇】19. 圖像的圓形遮罩
【youcans 的 OpenCV 例程200篇】20. 圖像的按位運(yùn)算
【youcans 的 OpenCV 例程200篇】21. 圖像的疊加
【youcans 的 OpenCV 例程200篇】22. 圖像添加非中文文字
【youcans 的 OpenCV 例程200篇】23. 圖像添加中文文字
【youcans 的 OpenCV 例程200篇】24. 圖像的仿射變換
【youcans 的 OpenCV 例程200篇】25. 圖像的平移
【youcans 的 OpenCV 例程200篇】26. 圖像的旋轉(zhuǎn)(以原點(diǎn)為中心)
【youcans 的 OpenCV 例程200篇】27. 圖像的旋轉(zhuǎn)(以任意點(diǎn)為中心)
【youcans 的 OpenCV 例程200篇】28. 圖像的旋轉(zhuǎn)(直角旋轉(zhuǎn))
【youcans 的 OpenCV 例程200篇】29. 圖像的翻轉(zhuǎn)(cv2.flip)
【youcans 的 OpenCV 例程200篇】30. 圖像的縮放(cv2.resize)
【youcans 的 OpenCV 例程200篇】31. 圖像金字塔(cv2.pyrDown)
【youcans 的 OpenCV 例程200篇】32. 圖像的扭變(錯(cuò)切)
【youcans 的 OpenCV 例程200篇】33. 圖像的復(fù)合變換
【youcans 的 OpenCV 例程200篇】34. 圖像的投影變換
【youcans 的 OpenCV 例程200篇】35. 圖像的投影變換(邊界填充)
【youcans 的 OpenCV 例程200篇】36. 直角坐標(biāo)與極坐標(biāo)的轉(zhuǎn)換
【youcans 的 OpenCV 例程200篇】37. 圖像的灰度化處理和二值化處理
【youcans 的 OpenCV 例程200篇】38. 圖像的反色變換(圖像反轉(zhuǎn))
【youcans 的 OpenCV 例程200篇】39. 圖像灰度的線(xiàn)性變換
【youcans 的 OpenCV 例程200篇】40. 圖像分段線(xiàn)性灰度變換
【youcans 的 OpenCV 例程200篇】41. 圖像的灰度變換(灰度級(jí)分層)
【youcans 的 OpenCV 例程200篇】42. 圖像的灰度變換(比特平面分層)
【youcans 的 OpenCV 例程200篇】43. 圖像的灰度變換(對(duì)數(shù)變換)
【youcans 的 OpenCV 例程200篇】44. 圖像的灰度變換(伽馬變換)
【youcans 的 OpenCV 例程200篇】45. 圖像的灰度直方圖
【youcans 的 OpenCV 例程200篇】46. 直方圖均衡化
【youcans 的 OpenCV 例程200篇】47. 圖像增強(qiáng)—直方圖匹配
【youcans 的 OpenCV 例程200篇】48. 圖像增強(qiáng)—彩色直方圖匹配
【youcans 的 OpenCV 例程200篇】49. 圖像增強(qiáng)—局部直方圖處理
【youcans 的 OpenCV 例程200篇】50. 圖像增強(qiáng)—直方圖統(tǒng)計(jì)量圖像增強(qiáng)
【youcans 的 OpenCV 例程200篇】51. 圖像增強(qiáng)—直方圖反向追蹤
【youcans 的 OpenCV 例程200篇】52. 圖像的相關(guān)與卷積運(yùn)算
【youcans 的 OpenCV 例程200篇】53. Scipy 實(shí)現(xiàn)圖像二維卷積
【youcans 的 OpenCV 例程200篇】54. OpenCV 實(shí)現(xiàn)圖像二維卷積
【youcans 的 OpenCV 例程200篇】55. 可分離卷積核
【youcans 的 OpenCV 例程200篇】56. 低通盒式濾波器
【youcans 的 OpenCV 例程200篇】57. 低通高斯濾波器
【youcans 的 OpenCV 例程200篇】58. 非線(xiàn)性濾波—中值濾波
【youcans 的 OpenCV 例程200篇】59. 非線(xiàn)性濾波—雙邊濾波
【youcans 的 OpenCV 例程200篇】60. 非線(xiàn)性濾波—聯(lián)合雙邊濾波
【youcans 的 OpenCV 例程200篇】61. 導(dǎo)向?yàn)V波(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. 空間域圖像增強(qiáng)的綜合應(yīng)用
【youcans 的 OpenCV 例程200篇】68. 空間域圖像增強(qiáng)的綜合應(yīng)用
【youcans 的 OpenCV 例程200篇】69. 連續(xù)非周期信號(hào)的傅立葉系數(shù)
【youcans 的 OpenCV 例程200篇】70. 一維連續(xù)函數(shù)的傅里葉變換
【youcans 的 OpenCV 例程200篇】71. 連續(xù)函數(shù)的取樣
【youcans 的 OpenCV 例程200篇】72. 一維離散傅里葉變換
【youcans 的 OpenCV 例程200篇】73. 二維連續(xù)傅里葉變換
【youcans 的 OpenCV 例程200篇】74. 圖像的抗混疊
【youcans 的 OpenCV 例程200篇】75. Numpy 實(shí)現(xiàn)圖像傅里葉變換
【youcans 的 OpenCV 例程200篇】76. OpenCV 實(shí)現(xiàn)圖像傅里葉變換
【youcans 的 OpenCV 例程200篇】77. OpenCV 實(shí)現(xiàn)快速傅里葉變換
【youcans 的 OpenCV 例程200篇】78. 頻率域圖像濾波基礎(chǔ)
【youcans 的 OpenCV 例程200篇】79. 頻率域圖像濾波的基本步驟
【youcans 的 OpenCV 例程200篇】80. 頻率域圖像濾波詳細(xì)步驟
【youcans 的 OpenCV 例程200篇】81. 頻率域高斯低通濾波器
【youcans 的 OpenCV 例程200篇】82. 頻率域巴特沃斯低通濾波器
【youcans 的 OpenCV 例程200篇】83. 頻率域低通濾波:印刷文本字符修復(fù)
【youcans 的 OpenCV 例程200篇】84. 由低通濾波器得到高通濾波器
【youcans 的 OpenCV 例程200篇】85. 頻率域高通濾波器的應(yīng)用
【youcans 的 OpenCV 例程200篇】86. 頻率域?yàn)V波應(yīng)用:指紋圖像處理
【youcans 的 OpenCV 例程200篇】87. 頻率域鈍化掩蔽
【youcans 的 OpenCV 例程200篇】88. 頻率域拉普拉斯高通濾波
【youcans 的 OpenCV 例程200篇】89. 帶阻濾波器的傳遞函數(shù)
【youcans 的 OpenCV 例程200篇】90. 頻率域陷波濾波器
【youcans 的 OpenCV 例程200篇】91. 高斯噪聲、瑞利噪聲、愛(ài)爾蘭噪聲
【youcans 的 OpenCV 例程200篇】92. 指數(shù)噪聲、均勻噪聲、椒鹽噪聲
【youcans 的 OpenCV 例程200篇】93. 噪聲模型的直方圖
【youcans 的 OpenCV 例程200篇】94. 算術(shù)平均濾波器
【youcans 的 OpenCV 例程200篇】95. 幾何均值濾波器
【youcans 的 OpenCV 例程200篇】96. 諧波平均濾波器
【youcans 的 OpenCV 例程200篇】97. 反諧波平均濾波器
【youcans 的 OpenCV 例程200篇】98. 統(tǒng)計(jì)排序?yàn)V波器
【youcans 的 OpenCV 例程200篇】99. 修正阿爾法均值濾波器
【youcans 的 OpenCV 例程200篇】100. 自適應(yīng)局部降噪濾波器
【youcans 的 OpenCV 例程200篇】101. 自適應(yīng)中值濾波器
【youcans 的 OpenCV 例程200篇】102. 陷波帶阻濾波器的傳遞函數(shù)
【youcans 的 OpenCV 例程200篇】103. 陷波帶阻濾波器消除周期噪聲干擾
【youcans 的 OpenCV 例程200篇】104. 運(yùn)動(dòng)模糊退化模型
【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. 形態(tài)學(xué)操作之腐蝕
【youcans 的 OpenCV 例程200篇】114. 形態(tài)學(xué)操作之膨脹
【youcans 的 OpenCV 例程200篇】115. 形態(tài)學(xué)操作之開(kāi)運(yùn)算
【youcans 的 OpenCV 例程200篇】116. 形態(tài)學(xué)操作之閉運(yùn)算
【youcans 的 OpenCV 例程200篇】117. 形態(tài)學(xué)操作之頂帽運(yùn)算
【youcans 的 OpenCV 例程200篇】118. 形態(tài)學(xué)操作之底帽運(yùn)算
【youcans 的 OpenCV 例程200篇】119. 圖像的形態(tài)學(xué)梯度
【youcans 的 OpenCV 例程200篇】120. 擊中-擊不中變換
【youcans 的 OpenCV 例程200篇】121. 擊中-擊不中用于特征識(shí)別
【youcans 的 OpenCV 例程200篇】122. 形態(tài)算法之邊界提取
【youcans 的 OpenCV 例程200篇】123. 形態(tài)算法之孔洞填充
【youcans 的 OpenCV 例程200篇】124. 孔洞填充的泛洪算法
【youcans 的 OpenCV 例程200篇】125. 形態(tài)算法之提取連通分量
【youcans 的 OpenCV 例程200篇】126. 形態(tài)算法之凸殼
【youcans 的 OpenCV 例程200篇】127. 形態(tài)算法之細(xì)化
【youcans 的 OpenCV 例程200篇】128. 形態(tài)算法之骨架 (skimage)
【youcans 的 OpenCV 例程200篇】129. 形態(tài)算法之骨架 (重建開(kāi)運(yùn)算)
【youcans 的 OpenCV 例程200篇】130. 形態(tài)學(xué)之提取水平和垂直線(xiàn)
【youcans 的 OpenCV 例程200篇】131. 形態(tài)學(xué)重建之豎線(xiàn)字符提取
【youcans 的 OpenCV 例程200篇】132. 形態(tài)學(xué)重建之孔洞填充算法
【youcans 的 OpenCV 例程200篇】133. 形態(tài)學(xué)重建之邊界清除
【youcans 的 OpenCV 例程200篇】134. 形態(tài)學(xué)重建之細(xì)胞計(jì)數(shù)
【youcans 的 OpenCV 例程200篇】135. 形態(tài)學(xué)重建之粒度測(cè)定
【youcans 的 OpenCV 例程200篇】136. 灰度腐蝕和灰度膨脹
【youcans 的 OpenCV 例程200篇】137. 灰度開(kāi)運(yùn)算和灰度閉運(yùn)算原理
【youcans 的 OpenCV 例程200篇】138. 灰度開(kāi)運(yùn)算和灰度閉運(yùn)算
【youcans 的 OpenCV 例程200篇】139. 灰度頂帽變換校正陰影
【youcans 的 OpenCV 例程200篇】140. 灰度底帽變換校正光照
【youcans 的 OpenCV 例程200篇】141. 灰度底帽變換的三維地形圖
【youcans 的 OpenCV 例程200篇】142. 基于灰度形態(tài)學(xué)的圖像平滑
【youcans 的 OpenCV 例程200篇】143. 基于灰度形態(tài)學(xué)的粒度測(cè)定
【youcans 的 OpenCV 例程200篇】144. 基于灰度形態(tài)學(xué)的紋理分割
【youcans 的 OpenCV 例程200篇】145. 形態(tài)學(xué)之邊緣和角點(diǎn)檢測(cè)
【youcans 的 OpenCV 例程200篇】146. 基于灰度形態(tài)學(xué)的復(fù)雜背景圖像重建
【youcans 的 OpenCV 例程200篇】147. 圖像分割之孤立點(diǎn)檢測(cè)
【youcans 的 OpenCV 例程200篇】148. 圖像分割之線(xiàn)檢測(cè)
【youcans 的 OpenCV 例程200篇】149. 圖像分割之邊緣模型
【youcans 的 OpenCV 例程200篇】150. 邊緣檢測(cè)梯度算子
【youcans 的 OpenCV 例程200篇】151. 邊緣檢測(cè)中的平滑處理
【youcans 的 OpenCV 例程200篇】152. 邊緣檢測(cè)之 LoG 算子
總結(jié)
以上是生活随笔為你收集整理的【youcans 的 OpenCV 例程200篇】152. 边缘检测之 LoG 算子的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java常用的缓存_java 常用缓存
- 下一篇: java gradle 资源访问_jav