Python数学形态学实例分析

yipeiwu_com6年前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 tornado修改log输出方式

sed -i 's/StreamHandler()/StreamHandler(sys.__stdout__)/' /opt/python/python3/lib/python3.6/s...

Python数据结构与算法之图的基本实现及迭代器实例详解

Python数据结构与算法之图的基本实现及迭代器实例详解

本文实例讲述了Python数据结构与算法之图的基本实现及迭代器。分享给大家供大家参考,具体如下: 这篇文章参考自《复杂性思考》一书的第二章,并给出这一章节里我的习题解答。 (这书不到12...

MySQLdb ImportError: libmysqlclient.so.18解决方法

安装MySQLdb后,import MySQLdb出错如下: 复制代码 代码如下: [root@lizhong MySQL-python-1.2.3]# /usr/local/bin/p...

用python登录Dr.com思路以及代码分享

前提:isp得支持web登录的方式。 说明:每个ISP的登录页面不一样,不过我估计算法都是一样的,于是解决方案应该也是相似的,只是表单的key可能不太一样。 首先,分析登录页面。 页面h...

举例讲解如何在Python编程中进行迭代和遍历

迭代 首先理解下什么是迭代,python中所有从左往右扫面对象的方式都是可迭代的 有哪些方式是可迭代的: 1.文件操作    我们读取文件的时候,会用到一个readl...