【python学习笔记】27:scipy中ndimage模块作图像滤波

来源:互联网 发布:c语言中调用文本文件 编辑:程序博客网 时间:2024/05/26 22:06

scipy的ndimage可以用于做n维图像的处理。
*高斯滤波

>>> from scipy import misc>>> from scipy import ndimage>>> import matplotlib.pyplot as plt>>> test=misc.ascent() #用于测试的图像>>> plt.figure() #创建画布<matplotlib.figure.Figure object at 0x000000000687D860>>>> plt.imshow(test) #绘制测试图像<matplotlib.image.AxesImage object at 0x0000000006F4AB00>>>> plt.show() #显示原始图像

这里写图片描述

>>> gaus_test=ndimage.gaussian_filter(test,sigma=7) #高斯滤波>>> plt.imshow(gaus_test) #绘制新产生的图像(数组)<matplotlib.image.AxesImage object at 0x0000000007492588>>>> plt.show() #显示更新后的画布

这里写图片描述

*图像边缘化
因为图像是用数组表示的,可以先作高斯滤波让它们一定程度上变模糊,然后相减,那些相似的值就变小了,再进行一定倍数的放大(让小的值和大的值相差更多),然后加到某个基准值上,就可以实现边缘锐化了。

>>> gaus1=ndimage.gaussian_filter(test,sigma=1)>>> gaus3=ndimage.gaussian_filter(test,sigma=3)>>> sharp=gaus3+6*(gaus3-gaus1) #作差放大并加到基准值上>>> plt.imshow(sharp)<matplotlib.image.AxesImage object at 0x0000000007709CF8>>>> plt.show()

这里写图片描述

*用ndimage中值滤波

>>> mid_test=ndimage.median_filter(test,7) #直接作中值滤波>>> plt.imshow(mid_test)<matplotlib.image.AxesImage object at 0x0000000007884EB8>>>> plt.show()

这里用ndimage.median_filter()可以直接作二维图像的中值滤波,在参数中指定邻域(滤波窗口的像素长)。
这里写图片描述
该模块的更多方法,可以在导入后用dir查看。

原创粉丝点击