盐城快速建设网站公司,如何预览做好的网站,网站备案 域名证书,河北省建设注册中心网站首页欢迎关注 『youcans 的 OpenCV 例程 200 篇』 系列#xff0c;持续更新中 欢迎关注 『youcans 的 OpenCV学习课』 系列#xff0c;持续更新中 【youcans 的 OpenCV 例程200篇】154. 边缘检测之 Canny 算子 2.7 Canny 边缘检测算法
Canny 算法希望在提高边缘的敏感性的同时抑… 欢迎关注 『youcans 的 OpenCV 例程 200 篇』 系列持续更新中 欢迎关注 『youcans 的 OpenCV学习课』 系列持续更新中 【youcans 的 OpenCV 例程200篇】154. 边缘检测之 Canny 算子 2.7 Canny 边缘检测算法
Canny 算法希望在提高边缘的敏感性的同时抑制噪声 具体而言包括三个基本目标
错误率低对边缘的错判率、漏判率低定位性能好检测的边缘点尽可能接近实际边缘的中心单一边缘有且应当只有一个准确的响应 并尽可能抑制虚假边缘。
Canny 算法的本质是从数学上表达了这三个准则并试图得到最优解。Canny 算法使用四个指数函数的线性组合形成的最佳边缘算子可以由高斯函数的一阶导数来近似。
Canny 边缘检测算法是目前最优秀和最流行的边缘检测算法之一。算法不容易受噪声影响能够识别图像中的弱边缘和强边缘并能结合强弱边缘的位置关系给出图像整体的边缘信息。但是 Canny 算法编程复杂、运算较慢。
Canny 算法的基本步骤为
1使用高斯滤波对图像进行平滑
用二维高斯核与灰度图像进行卷积实现平滑滤波 G(x,y)e−(x2y2)/2σ2fs(x,y)G(x,y)⋆f(x,y)G(x,y) e^{- {(x^2 y^2)} / {2 \sigma ^2}} \\ f_s(x,y) G(x,y) \star f(x,y) G(x,y)e−(x2y2)/2σ2fs(x,y)G(x,y)⋆f(x,y) 2用一阶有限差分计算梯度幅值和方向
梯度向量的幅度 M 和角度 α\alphaα 为 M(x,y)∣∣∇fs∣∣gx2gy2α(x,y)arctan[gy/gx]M(x,y) ||\nabla f_s|| \sqrt {g_x^2 g_y^2} \\ \alpha (x,y) arctan[g_y / g_x] M(x,y)∣∣∇fs∣∣gx2gy2α(x,y)arctan[gy/gx] 具体应用中可以采用 Sobel 算子、Prewitt算子、Roberts算子等进行梯度幅值的计算。
3对梯度幅值进行非极大值抑制NMS
为了实现单一边缘仅有唯一响应沿着梯度方向寻找像素点的局部最大值 将局部最大值之外的所有梯度值抑制为 0剔除非边缘的像素点。
4用双阈值处理和连通性分析来检测和连接边缘。
应用双阈值 TH、TL 划分强边缘和弱边缘。将边缘处的梯度幅值与阈值比较如果大于 TH则标记为强边缘如果在 TL 与 TH 之间则标记为弱边缘如果小于 TL 则被抛弃。
OpenCV 也提供了函数 cv.Canny 实现 Canny 边缘检测算子。
函数说明
cv.Canny(image, threshold1, threshold2[, edges[, apertureSize[, L2gradient]]]) → edges参数说明
image输入图像8-bit 灰度图像不适用彩色图像edges输出边缘图像8-bit 单通道图像大小与输入图像相同threshold1第一阈值 TLthreshold2第二阈值 THapertureSizeSobel 卷积核的孔径可选项默认值 3L2gradient 计算图像梯度幅值 标志符默认值为 True 表示 L2 法False 表示 L1 法
注意事项
阈值 TL 用于边缘连接阈值 TH 用于控制强边元的初值推荐选择阈值比为 1:2~1:3。 例程 11.8Canny 边缘检测算法 # 11.8 Canny 边缘检测算子img cv2.imread(../images/imgLena.tif, flags0) # flags0 读取为灰度图像# 高斯核低通滤波器sigmaY 缺省时 sigmaYsigmaXkSize (5, 5)imgGauss1 cv2.GaussianBlur(img, kSize, sigmaX1.0) # sigma1.0imgGauss2 cv2.GaussianBlur(img, kSize, sigmaX10.0) # sigma2.0# 高斯差分算子 (Difference of Gaussian)imgDoG imgGauss2 - imgGauss1 # sigma1.0, 10.0# Canny 边缘检测 kSize 为高斯核大小t1,t2为阈值大小t1, t2 50, 150imgCanny cv2.Canny(imgGauss1, t1, t2)plt.figure(figsize(10, 6))plt.subplot(131), plt.title(Origin), plt.imshow(img, cmapgray), plt.axis(off)plt.subplot(132), plt.title(DoG), plt.imshow(imgDoG, cmapgray), plt.axis(off)plt.subplot(133), plt.title(Canny), plt.imshow(imgCanny, cmapgray), plt.axis(off)plt.tight_layout()plt.show()本节完 版权声明
youcansxupt 原创作品转载必须标注原文链接(https://blog.csdn.net/youcans/article/details/124092258)
Copyright 2022 youcans, XUPT Crated2022-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 算子