scipy图片处理(2)
图片处理
使用scipy.misc.face(gray=True)获取图片使用ndimage移动坐标,旋转图片,切割图片,缩放图片。
导包,读取显示图片
In [13]:
#scipy自带的一张图
face=misc.face()
plt.imshow(face)
plt.show()
In [14]:
from scipy import ndimage
shift移动坐标
In [15]:
face1=ndimage.shift(face,shift=[200,0,0],cval=255)#高,宽,颜色,cval(常量)=255白色,0(黑色)
plt.imshow(face1)
plt.show()
In [16]:
'''
mode : str, optional
Points outside the boundaries of the input are filled according
to the given mode ('constant', 'nearest', 'reflect', 'mirror' or 'wrap').
Default is 'constant'.'''
face1=ndimage.shift(face,shift=[0,-500,1],mode='mirror')#mode对无图像的地方操作,可变像看作补全
plt.imshow(face1)
plt.show()
In [17]:
face2=ndimage.shift(face,shift=[0,-500,0],mode='wrap')
plt.imshow(face2)
plt.show()
rotate旋转图片
In [18]:
#mode对无图像的地方操作,可变像看作补全,这里的旋转相较于前面的不会有图片的边角消失
face3=ndimage.rotate(face,angle=60,mode='mirror')
plt.imshow(face3)
plt.show()
zoom缩放
In [49]:
face4=ndimage.zoom(face,zoom=[0.5,0.5,0.9]) #高,宽,颜色
plt.imshow(face4)
plt.show()
# face4 #当颜色小于0.9,arrag怎么会没东西?? (384, 512, 0) TypeError: Invalid dimensions for image data
In [61]:
face5=ndimage.zoom(face,zoom=[0.5,0.5,1/3]) #高,宽,颜色
# 形状出现问题TypeError: Invalid dimensions for image data (384, 512, 9),要reshape来解决
#cmap=plt.cm.gray灰度显示黑白,要求是二维(降一下维度)
plt.imshow(face5.reshape(384, 512),cmap=plt.cm.gray)
plt.show()
In [64]:
face5.shape
Out[64]:
In [66]:
face6=ndimage.zoom(face,zoom=[0.5,0.5,4/3]) #高,宽,颜色, 4/3透明效果 四维,rgb+1
plt.imshow(face6)
plt.show()
In [71]:
face6[:,:,2]=190
plt.imshow(face6)
plt.show()
In [80]:
face6[:,:,3]
Out[80]:
切片操作
图片进行过滤,
添加噪声,对噪声图片使用ndimage中的高斯滤波,中值滤波,sigg1al中维纳滤波处理 使图片变得更清楚
加载图片,使灰色图片misc.face()添加噪声
In [ ]:
ndimaged.
高斯滤波sigma:高斯喝的标准偏差
In [83]:
import pandas as pd
import matplotlib.pyplot as plt
In [85]:
moon=plt.imread('./moonlanding.png')
moon.shape
Out[85]:
In [91]:
plt.figure(figsize=(12,9))
# plt.imshow(moon)
# plt.show()
In [97]:
#高斯分布,正态分布,概率不同
#misc,imfilter(moon,'smooth')原理一样
'''
input : array_like
Input array to filter.
sigma : scalar or sequence of scalars
Standard deviation for Gaussian kernel. The standard
deviations of the Gaussian filter are given for each axis as a
sequence, or as a single number, in which case it is equal for
all axes.'''
moon2= ndimage.gaussian_filter(moon,sigma=2)#卷积操作,(概率),而不是数据提取成波
plt.figure(figsize=(12,9))
plt.imshow(moon2)
plt.show()
中值滤波参数size:给出差在每个原=元素上的从输入数组中提取出来的形状位置,定义过滤器功能的输入
In [100]:
moon2=ndimage.median_filter(moon,size=5)
plt.figure(figsize=(12.,9))
plt.imshow(moon2)
plt.show()