门户网站备案,公司网站首页制作教程,app网站及其特色,杭州制作网页欢迎关注 『youcans 的 OpenCV 例程 200 篇』 系列#xff0c;持续更新中 欢迎关注 『youcans 的 OpenCV学习课』 系列#xff0c;持续更新中 【youcans 的 OpenCV 例程 200 篇】113. 形态学操作之腐蚀 ## 1. 形态学图像处理简介
形态学是生物学的概念#xff0c;主要研究动… 欢迎关注 『youcans 的 OpenCV 例程 200 篇』 系列持续更新中 欢迎关注 『youcans 的 OpenCV学习课』 系列持续更新中 【youcans 的 OpenCV 例程 200 篇】113. 形态学操作之腐蚀 ## 1. 形态学图像处理简介
形态学是生物学的概念主要研究动植物的形态和结构。数学形态学Mathematical morphology是建立在集合论和拓扑学基础之上的图像分析学科。
图像处理中的形态学是指基于形状的图像处理操作以数学形态学为工具从图像中提取表达和描绘区域形状的图像结构信息如边界、骨架、凸壳等还包括用于预处理或后处理的形态学过滤、细化和修剪等。形态学图像处理最初用于处理二值图进而推广到灰度级图像处理其运算简单、效果良好。
形态学图像处理的运算是用集合定义的基本运算包括二值腐蚀和膨胀二值开闭运算骨架抽取极限腐蚀击中击不中变换形态学梯度顶帽变换颗粒分析流域变换灰值腐蚀和膨胀灰值开闭运算灰值形态学梯度等。
形态学的基本思想是利用结构元素测量或提取输入图像中的形状或特征以便进行图像分析和目标识别。形态学操作都是基于各种形状的结构元结构元对输入图像进行操作得到输出图像。
在二值图像中所有黑色像素的集合是图像完整的形态学描述。假定二值图像 A 和形态学处理的结构元素 B 是定义在笛卡儿网格上的集合结构元素Structuring ElementsSE可以是任意形状如矩形、十字形。结构元有一个锚点 O定义为结构元的中心。 2. 形态学基本操作
2.1 腐蚀
腐蚀和膨胀是图像处理中最基本的形态学操作是很多高级处理方法的基础。
腐蚀和膨胀是对白色部分高亮部分而言的膨胀就是图像中的高亮部分进行膨胀腐蚀就是原图中的高亮部分被腐蚀。
腐蚀使图像中白色高亮部分被腐蚀“邻域被蚕食”腐蚀的效果拥有比原图更小的高亮区域可以去掉毛刺去掉孤立的像素提取骨干信息。
腐蚀的原理是求局部最小值的操作将 0 值扩充到邻近像素从而扩大黑色值范围、压缩白色值范围。
结构元 B 对集合 A 的腐蚀定义为 A⊖B{z∣(B)z⊆A}A \ominus B \{ z | (B)_z \subseteq A\} A⊖B{z∣(B)z⊆A}
用卷积来描述腐蚀操作结构元素 B 是中心为 1、其它为 0 的卷积模板核 1卷积核 B 沿着图像滑动扫描图像 A 的每一个像素 2用结构元素与其覆盖的二值图像进行 “或操作” 3如果图像与卷积核对应区域的所有像素值都是 1则图像的该像素值仍为 1否则为 0。
OpenCV 提供了函数 cv.erode 可以实现图像的腐蚀。
函数说明
cv.erode(src, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]]) → dst函数 cv.erode 使用指定的结构元卷积核侵蚀源图像结构元确定像素邻域的形状在该邻域上取最小值 dst(x,y)min(x′,y′):element(x′,y′≠0)src(xx′,yy′)dst(x,y) \min_{\enspace (x,y):element(x,y \neq 0)} \enspace src(xx,yy) dst(x,y)(x′,y′):element(x′,y′0)minsrc(xx′,yy′)
参数说明
src输入图像可以为单通道或多通道图像深度必须为 CV_8U, CV_16U, CV_16S, CV_32F 或 CV_64Fdst输出图像大小和类型与 src 相同kernel结构元卷积核null 时使用 3*3 矩形结构元素anchor卷积核的锚点位置默认值 (-1, -1) 表示以卷积核的中心为锚点iterations应用腐蚀操作的次数可选项默认值为 1borderType边界扩充的类型borderValue当 borderTypeBORDER_CONSTANT 时以常量 value 填充扩充边界默认值为 (0,0,0)
注意事项 函数支持就地模式腐蚀操作可以迭加使用多次。 在多通道图像的情况下每个通道独立处理 。 例程 10.1图像的腐蚀 (cv.erode) # 10.1 图像的腐蚀 (cv.erode)# 读取原始图像imgGray cv2.imread(../images/Fig0905a.tif, flags0) # flags0 读取为灰度图像ret, imgBin cv2.threshold(imgGray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) # 二值化处理# 图像腐蚀kSize (3, 3) # 卷积核的尺寸kernel np.ones(kSize, dtypenp.uint8) # 生成盒式卷积核imgErode1 cv2.erode(imgBin, kernelkernel) # 图像腐蚀kSize (9, 9)kernel np.ones(kSize, dtypenp.uint8)imgErode2 cv2.erode(imgBin, kernelkernel)kSize (25, 25)kernel np.ones(kSize, dtypenp.uint8)imgErode3 cv2.erode(imgBin, kernelkernel)plt.figure(figsize(10, 5))plt.subplot(141), plt.axis(off), plt.title(Origin)plt.imshow(imgBin, cmapgray, vmin0, vmax255)plt.subplot(142), plt.title(eroded kSize(3,3)), plt.axis(off)plt.imshow(imgErode1, cmapgray, vmin0, vmax255)plt.subplot(143), plt.title(eroded kSize(9,9)), plt.axis(off)plt.imshow(imgErode2, cmapgray, vmin0, vmax255)plt.subplot(144), plt.title(eroded kSize(25,25)), plt.axis(off)plt.imshow(imgErode3, cmapgray, vmin0, vmax255)plt.tight_layout()plt.show()本节完 版权声明
youcansxupt 原创作品转载必须标注原文链接(https://blog.csdn.net/youcans/article/details/123225087)
Copyright 2022 youcans, XUPT Crated2022-3-2 欢迎关注 『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. 形态学操作之腐蚀