Python数学形态学实例分析

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Python数学形态学实例分析

  2021-04-02 我要评论

本文实例讲述了Python数学形态学。分享给大家供大家参考,具体如下:

一 原始随机图像

1、代码

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

2、运行结果

二 开运算

1、代码

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

2、运行结果

三 膨胀运算

1、代码

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

2、运行结果

四 闭运算

1、代码

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

2、运行结果

希望本文所述对大家Python程序设计有所帮助。

您可能感兴趣的文章:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们