如果您覺得本文不錯!記得點贊哦!
一. 圖像形態學簡介:

經驗之談:形態學操作一般作用於二值圖像,來連接相鄰的元素(膨脹)或分離成獨立的元素(侵蝕)。腐蝕和膨脹是針對圖片中的白色(即前景)部分!
二. 圖像形態學操作 膨脹和腐蝕的算法:
膨脹算法:
對於待操作的像素 f(x,y),不論 f(x,y-1) 、f(x,y+1) 、f(x-1,y) 、f(x+1,y) 哪一個為255,則 f(x,y)=255。

膨脹操作 ↑
換句話說:將待操作的圖像像素與以下 4-近鄰矩陣 相乘,結果大於255的話,將中心像素設為255。

膨脹:待操作像素 * 上面矩陣 > =255,f(x,y) = 255。 ↑
腐蝕算法:
對於待操作的像素 f(x,y),只有 f(x,y-1) 、f(x,y+1) 、f(x-1,y) 、f(x+1,y) 都為255,則 f(x,y)=255。
換句話說:將待操作的圖像像素與以下 4-近鄰矩陣 相乘,結果小於255*4的話,將中心像素設為0。

腐蝕:待操作像素 * 上面矩陣 < 255*4,f(x,y) = 0 。↑
三. python實現圖像膨脹和腐蝕
1 # Writer : wojianxinygcl@163.com 2 # Date : 2020.3.21 3 import cv2 4 import numpy as np 5 import matplotlib.pyplot as plt 6 7 # Gray scale 8 def BGR2GRAY(img): 9 b = img[:, :, 0].copy() 10 g = img[:, :, 1].copy() 11 r = img[:, :, 2].copy() 12 13 # Gray scale 14 out = 0.2126 * r + 0.7152 * g + 0.0722 * b 15 out = out.astype(np.uint8) 16 17 return out 18 19 # Otsu Binalization 20 def otsu_binarization(img, th=128): 21 H, W = img.shape 22 out = img.copy() 23 24 max_sigma = 0 25 max_t = 0 26 27 # determine threshold 28 for _t in range(1, 255): 29 v0 = out[np.where(out < _t)] 30 m0 = np.mean(v0) if len(v0) > 0 else 0. 31 w0 = len(v0) / (H * W) 32 v1 = out[np.where(out >= _t)] 33 m1 = np.mean(v1) if len(v1) > 0 else 0. 34 w1 = len(v1) / (H * W) 35 sigma = w0 * w1 * ((m0 - m1) ** 2) 36 if sigma > max_sigma: 37 max_sigma = sigma 38 max_t = _t 39 40 # Binarization 41 print("threshold >>", max_t) 42 th = max_t 43 out[out < th] = 0 44 out[out >= th] = 255 45 46 return out 47 48 49 # Morphology Dilate 50 def Morphology_Dilate(img, Dil_time=1): 51 H, W = img.shape 52 53 # kernel 54 MF = np.array(((0, 1, 0), 55 (1, 0, 1), 56 (0, 1, 0)), dtype=np.int) 57 58 # each dilate time 59 out = img.copy() 60 for i in range(Dil_time): 61 tmp = np.pad(out, (1, 1), 'edge') 62 for y in range(1, H): 63 for x in range(1, W): 64 if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) >= 255: 65 out[y, x] = 255 66 67 return out 68 69 70 # Morphology Erode 71 def Morphology_Erode(img, Erode_time=1): 72 H, W = img.shape 73 out = img.copy() 74 75 # kernel 76 MF = np.array(((0, 1, 0), 77 (1, 0, 1), 78 (0, 1, 0)), dtype=np.int) 79 80 # each erode 81 for i in range(Erode_time): 82 tmp = np.pad(out, (1, 1), 'edge') 83 # erode 84 for y in range(1, H): 85 for x in range(1, W): 86 if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) < 255*4: 87 out[y, x] = 0 88 89 return out 90 91 92 # Read image 93 img = cv2.imread("../paojie.jpg").astype(np.float32) 94 95 # Grayscale 96 gray = BGR2GRAY(img) 97 98 # Otsu's binarization 99 otsu = otsu_binarization(gray) 100 101 # Morphology - dilate 102 erode_result = Morphology_Erode(otsu, Erode_time=2) 103 dilate_result = Morphology_Dilate(otsu,Dil_time=2) 104 105 # Save result 106 cv2.imwrite("Black_and_white.jpg",otsu) 107 cv2.imshow("Black_and_white",otsu) 108 cv2.imwrite("erode_result.jpg", erode_result) 109 cv2.imshow("erode_result", erode_result) 110 cv2.imwrite("dilate_result.jpg", dilate_result) 111 cv2.imshow("dilate_result",dilate_result) 112 cv2.waitKey(0) 113 cv2.destroyAllWindows()
四. 實驗結果:

二值圖像(左),膨脹圖像(中),侵蝕圖像(右) ↑
五. 參考內容:
① https://www.jianshu.com/p/ba2cec49c981
② https://www.cnblogs.com/yibeimingyue/p/10856439.html
六. 版權聲明:
未經作者允許,請勿隨意轉載抄襲,抄襲情節嚴重者,作者將考慮追究其法律責任,創作不易,感謝您的理解和配合!