`
cakin24
  • 浏览: 1333901 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

数学形态学

阅读更多

一 原始随机图像

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、运行结果


 
  • 大小: 14 KB
  • 大小: 14.1 KB
  • 大小: 14.2 KB
  • 大小: 13.9 KB
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics