python实现图像膨胀和腐蚀算法

如果您觉得本文不错!记得点赞哦!


一. 图像形态学简介:


 

    经验之谈:形态学操作一般作用于二值图像,来连接相邻的元素(膨胀)或分离成独立的元素(侵蚀)。腐蚀和膨胀是针对图片中的白色(即前景)部分!


二. 图像形态学操作 膨胀和腐蚀的算法:

    膨胀算法:

        对于待操作的像素 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


六. 版权声明:

    未经作者允许,请勿随意转载抄袭,抄袭情节严重者,作者将考虑追究其法律责任,创作不易,感谢您的理解和配合!

posted on 2020-03-21 20:40  我坚信阳光灿烂  阅读(11420)  评论(1编辑  收藏  举报

导航