【youcans 的 OpenCV 例程200篇】114. 形态学操作之膨胀
歡迎關注 『youcans 的 OpenCV 例程 200 篇』 系列,持續更新中
歡迎關注 『youcans 的 OpenCV學習課』 系列,持續更新中
【youcans 的 OpenCV 例程 200 篇】114. 形態學操作之膨脹
形態學的基本思想是利用結構元素測量或提取輸入圖像中的形狀或特征,以便進行圖像分析和目標識別。形態學操作都是基于各種形狀的結構元,結構元對輸入圖像進行操作得到輸出圖像。
腐蝕和膨脹是圖像處理中最基本的形態學操作,是很多高級處理方法的基礎。腐蝕和膨脹是對白色部分(高亮部分)而言的,膨脹就是圖像中的高亮部分進行膨脹,腐蝕就是原圖中的高亮部分被腐蝕。
2. 形態學基本操作
2.2 膨脹
膨脹使圖像中的白色高亮部分進行膨脹,“鄰域擴張”,膨脹效果擁有比原圖更大的高亮區域,可以填補圖像缺陷,用來擴充邊緣或填充小的孔洞,也可以用來連接兩個分開的物體。
膨脹的原理是求局部最大值的操作,將 1 值擴充到鄰近像素,從而擴大白色值范圍、壓縮黑色值范圍。
結構元 B 對集合 A 的膨脹定義為:
A⊕B={z∣(B^)z∩A≠?}A \oplus B = \{ z | (\hat B)_z \cap A \neq \varnothing \} A⊕B={z∣(B^)z?∩A?=?}
用卷積來描述膨脹操作,中心為 1、其它為 0 的卷積核沿著圖像滑動卷積,與卷積核對應的原圖像的像素值中只要有一個為 1, 則圖像的中心元素的像素值為 1,否則(全 0)為 0。
用卷積來描述膨脹操作,結構元素 B 是中心為 1、其它為 0 的卷積模板(核):
(1)卷積核 B 沿著圖像滑動,掃描圖像 A 的每一個像素;
(2)用結構元素與其覆蓋的二值圖像進行 “與操作”;
(3)如果圖像與卷積核對應區域的所有像素值都是 0,則圖像的該像素值仍為 0;否則為 1。
在去噪聲時通常先進行腐蝕,在去掉白噪聲的同時,使前景對象變小;然后再對進行膨脹,此時噪聲已經被去除,可以增加前景。
OpenCV 提供了函數 cv.dilate 可以實現圖像的膨脹。
函數說明:
cv.dilate(src, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]]) → dst函數 cv.dilate 使用指定的結構元(卷積核)膨脹源圖像,結構元確定像素鄰域的形狀,在該鄰域上取最大值:
dst(x,y)=max?(x′,y′):element(x′,y′≠0)src(x+x′,y+y′)dst(x,y)= \max_{\enspace (x',y'):element(x',y' \neq 0)} \enspace src(x+x',y+y') dst(x,y)=(x′,y′):element(x′,y′?=0)max?src(x+x′,y+y′)
參數說明:
- src:輸入圖像,可以為單通道或多通道,圖像深度必須為 CV_8U, CV_16U, CV_16S, CV_32F 或 CV_64F
- dst:輸出圖像,大小和類型與 src 相同
- kernel:結構元(卷積核),null 時使用 3*3 矩形卷積核
- anchor:卷積核的錨點位置,默認值 (-1, -1) 表示以卷積核的中心為錨點
- iterations:應用膨脹的次數,可選項,默認值為 1
- borderType:邊界擴充的類型
- borderValue:當 borderType=BORDER_CONSTANT 時以常量 value 填充擴充邊界,默認值為 (0,0,0)
注意事項:
例程 10.2:圖像的膨脹 (cv.dilate)
# 10.2 圖像的膨脹 (cv.dilate)# cv.dilate(src, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]]) → dst# 讀取原始圖像imgGray = cv2.imread("../images/handwriting01.png", flags=0) # flags=0 讀取為灰度圖像ret, imgBin = cv2.threshold(imgGray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) # 二值化處理# 圖像膨脹kSize = (3, 3) # 卷積核的尺寸kernel = np.ones(kSize, dtype=np.uint8) # 生成盒式卷積核imgDilate1 = cv2.dilate(imgBin, kernel=kernel) # 圖像膨脹kSize = (5, 5)kernel = np.ones(kSize, dtype=np.uint8)imgDilate2 = cv2.dilate(imgBin, kernel=kernel) # 圖像膨脹kSize = (7, 7)kernel = np.ones(kSize, dtype=np.uint8)imgDilate3 = cv2.dilate(imgBin, kernel=kernel) # 圖像膨脹plt.figure(figsize=(10, 5))plt.subplot(141), plt.axis('off'), plt.title("Origin")plt.imshow(imgBin, cmap='gray', vmin=0, vmax=255)plt.subplot(142), plt.title("dilate kSize=(3,3)"), plt.axis('off')plt.imshow(imgDilate1, cmap='gray', vmin=0, vmax=255)plt.subplot(143), plt.title("dilate kSize=(5,5)"), plt.axis('off')plt.imshow(imgDilate2, cmap='gray', vmin=0, vmax=255)plt.subplot(144), plt.title("dilate kSize=(7,7)"), plt.axis('off')plt.imshow(imgDilate3, cmap='gray', vmin=0, vmax=255)plt.tight_layout()plt.show()(本節完)
版權聲明:
youcans@xupt 原創作品,轉載必須標注原文鏈接:(https://blog.csdn.net/youcans/article/details/123251108)
Copyright 2022 youcans, XUPT
Crated:2022-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. 形態學操作之腐蝕
【youcans 的 OpenCV 例程200篇】114. 形態學操作之膨脹
總結
以上是生活随笔為你收集整理的【youcans 的 OpenCV 例程200篇】114. 形态学操作之膨胀的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机硬盘能达到5g的速度,5G号称可以
- 下一篇: Python小白的数学建模课-B2. 新