Python数学形态学实例分析

yipeiwu_com5年前Python基础

本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《Python数学运算技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程

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

相关文章

浅析Python 引号、注释、字符串

Python3.6.3    json==2.0.9    win10.0.17134 字符串 1.不可变类型,可迭代对...

对python捕获ctrl+c手工中断程序的两种方法详解

日常编写调试运行程序过程中,难免需要手动停止,以下两种方法可以捕获ctrl+c立即停止程序 1、使用python的异常KeyboardInterrupt try: whi...

在pycharm中设置显示行数的方法

下面是具体的步骤,试用于pycharm2016(亲测) 1.  File-->settings-->editor-->general-->appeara...

Python中unittest模块做UT(单元测试)使用实例

待测试的类(Widget.py) # Widget.py # Python 2.7.6 class Widget: def __init__(self, size =...

python数据结构之链表详解

python数据结构之链表详解

数据结构是计算机科学必须掌握的一门学问,之前很多的教材都是用C语言实现链表,因为c有指针,可以很方便的控制内存,很方便就实现链表,其他的语言,则没那么方便,有很多都是用模拟链表,不过这次...