【youcans 的 OpenCV 例程200篇】124. 孔洞填充的泛洪算法

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


【youcans 的 OpenCV 例程 200 篇】124. 孔洞填充的泛洪算法

3. 形态学算法

形态学处理的主要应用是提取图像中用来表示和描述形状的元素和成分,例如提取边界、连通分量、凸壳和区域骨架。


3.2 孔洞填充

孔洞是被前景像素连成的边框包围的背景区域。书法作品图像中存在孔洞,在图像分割后也经常会有一些孔洞。

泛洪算法孔洞填充:

OpenCV 中提供了一种孔洞填充方法“泛洪填充法”,也成为“漫水填充法“。其原理是将像素点的灰度值视为高度,整个图像就像一张高低起伏的地形图,向洼地注水将会淹没低洼区域,从而实现孔洞填充。

漫水填充经常被用来标记或分离图像的一部分以便对其进行进一步处理或分析,也可以用来从输入图像获取掩码区域,掩码会加速处理过程,或只处理掩码指定的像素点,操作的结果总是某个连续的区域。

OpenCV 中的函数 cv.floodFill 可以实现漫水填充方法 。

函数说明:

cv.floodFill(image, mask, seedPoint, newVal[, loDiff[, upDiff[, flags]]]) -> retval, image, mask, rect

参数说明:

  • image:输入图像,可以为单通道或多通道,图像深度必须为8bit 或浮点类型。
  • dst:输出图像,大小和类型与 src 相同
  • mask:掩模图像,必须为单通道、8bit,且比 image 宽 2个像素、高 2个像素
  • setPoint:起始像素点
  • newVal:重绘像素区域的新的填充值(颜色)
  • rect:可选项,返回重绘区域的最小绑定矩形
  • loDiff:可选项,当前选定像素与其连通区中相邻像素中的一个像素,或者与加入该连通区的一个 seedPoint像素,二者之间的最大下行差异值。
  • upDiff:可选项,当前选定像素与其连通区中相邻像素中的一个像素,或者与加入该连通区的一个 seedPoint像素,二者之间的最大上行差异值。
  • flags:标志位,可选项,32bit 整型数据,由3部分组成: 0-7bit 表示邻接性(4邻接、8邻接);8-15bit 表示 mask 的填充颜色;16-31bit 表示填充模式:
    • cv.FLOODFILL_FIXED_RANGE:如果设置则考虑当前像素和种子像素之间的差异,否则将考虑相邻像素之间的差异
    • cv.FLOODFILL_MASK_ONLY:如果设置则不改变原始图像,并忽略 newVal,只使用上述标志位 8-16 中指定的值填充掩码。本选项仅在具有掩模图像时适用。

注意事项:

  • 原始图像 image 仅当 flags 设置为 FLOODFILL_MASK_ONLY 时不会被修改,否则原始图像会被修改。
  • 由于掩模比原始图像大,所以图像中的像素 (x,y) 对应于掩模中的像素 (x+1,y+1)。
  • Flood-filling 不能跨越掩模图像中的非 0 像素,因此边缘检测的结果可以作为mask来阻止边缘填充。
  • 泛洪填充法可以用特定的颜色填充联通区域(newVal),参见例程 10.10。

例程 10.12:泛洪算法实现孔洞填充

    # 10.12 泛洪算法实现孔洞填充 (cv2.floodFill)
    # 图像为二值化图像,255 白色为目标物,0 黑色为背景,要填充白色目标物中的黑色空洞
    imgGray = cv2.imread("../images/imgBloodCell.png", flags=0)  # flags=0 读取为灰度图像
    ret, imgBin = cv2.threshold(imgGray, 127, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)  # 二值化处理

    h, w = imgBin.shape[:2]
    mask = np.zeros((h+2, w+2), np.uint8)  # 掩模图像比原始图像宽 2 个像素、高 2 个像素

    imgFloodfill = imgBin.copy()
    cv2.floodFill(imgFloodfill, mask, (0, 0), newVal=225)  # 算法从背景像素原点 (0, 0) 开始
    imgFloodfillInv = cv2.bitwise_not(imgFloodfill)  # 计算补集
    imgHoleFilled = imgBin | imgFloodfillInv  # 计算交集
    imgRebuild = cv2.bitwise_not(imgHoleFilled)  # 计算补集

    plt.figure(figsize=(9, 5))
    plt.subplot(131), plt.axis('off'), plt.title("Origin")
    plt.imshow(imgGray, cmap='gray', vmin=0, vmax=255)
    plt.subplot(132), plt.title("Flood filled"), plt.axis('off')
    plt.imshow(imgFloodfill, cmap='gray', vmin=0, vmax=255)
    plt.subplot(133), plt.title("Hole filled image"), plt.axis('off')
    plt.imshow(imgRebuild, cmap='gray', vmin=0, vmax=255)
    plt.tight_layout()
    plt.show()

【youcans 的 OpenCV 例程200篇】124. 孔洞填充的泛洪算法_第1张图片

(本节完)


版权声明:

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

Copyright 2022 youcans, XUPT
Crated:2022-3-6


欢迎关注 『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,篇,opencv,python,图像处理,计算机视觉,算法)