数学形态学

一 原始随机图像

1、代码
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. square = np.zeros((32,32))#全0数组
  4. square[10:20,10:20]=1#把其中一部分设置为1
  5. x, y =(32*np.random.random((2,15))).astype(np.int)#随机位置
  6. square[x,y]=1#把随机位置设置为1
  7. plt.imshow(square)#原始随机图像
  8. plt.show()
2、运行结果


 
 
二 开运算
1、代码
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from scipy import ndimage
  4. square = np.zeros((32,32))#全0数组
  5. square[10:20,10:20]=1#把其中一部分设置为1
  6. x, y =(32*np.random.random((2,15))).astype(np.int)#随机位置
  7. square[x,y]=1#把随机位置设置为1
  8. open_square = ndimage.binary_opening(square)#开运算
  9. plt.imshow(open_square)
  10. plt.show()
2、运行结果


 
 
三 膨胀运算
1、代码
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from scipy import ndimage
  4. square = np.zeros((32,32))#全0数组
  5. square[10:20,10:20]=1#把其中一部分设置为1
  6. x, y =(32*np.random.random((2,15))).astype(np.int)#随机位置
  7. square[x,y]=1#把随机位置设置为1
  8. eroded_square = ndimage.binary_erosion(square)#膨胀运算
  9. plt.imshow(eroded_square)
  10. plt.show()
2、运行结果


 
 
四 闭运算
1、代码
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from scipy import ndimage
  4. square = np.zeros((32,32))#全0数组
  5. square[10:20,10:20]=1#把其中一部分设置为1
  6. x, y =(32*np.random.random((2,15))).astype(np.int)#随机位置
  7. square[x,y]=1#把随机位置设置为1
  8. closed_square = ndimage.binary_closing(square)#闭运算
  9. plt.imshow(closed_square)
  10. plt.show()
2、运行结果


 

猜你喜欢

转载自cakin24.iteye.com/blog/2388395