首先整體歸納一下Scipy的用處數組
>>> #Scipy依賴於numpy
>>> #Scipy提供了真正的矩陣
>>> #Scipy包含的功能:最優化,線性代數,積分,插值,擬合,特殊函數,快速傅里葉變換,信號處理,圖形處理,常微分方程求解器等
>>> #Scipy是高端科學計算工具包
>>> #Scipy由一些特殊功能的子模塊組成
>>> #圖片消噪dom
下面介紹一些具體的應用函數
1:求圓周率工具
從圖片易知道,圓周率爲半徑爲一的半圓的面積的兩倍,因此只須要求解半圓的面積便可,可經過積分的形式求解優化
具體過程以下ui
>>>x=np.linspace(-1,1,1000)#-1到1分紅1000份來進行積分spa
>>> f=lambda x:(1-x**2)**0.53d
>>>plt.plot(x,f(x))#畫出該圖形blog
>>> plt.figure(figsize=(4,2))#設置圖形大小圖片
>>> plt.show()
>>> #使用scipy.integrate進行積分,調用quad()方法
>>> import scipy.integrate as integrate
>>> integrate.quad (f,-1,1)#求積分
(1.5707963267948983, 1.0002354500215915e-09, 1.5707963267948983, 1.0002354500215915e-09)
>>> sq,err=integrate.quad (f,-1,1)#sq是半圓的面積,err是偏差
>>> pi=sq*2#圓的面積是圓周率
>>> pi
3.1415926535897967
2:文件處理
>>> #Scipy文件輸入輸出
>>> #隨機生成數組,使用Scipy中的io.savement()保存
>>> #文件格式是.mat,標準的二進制文件
>>> import scipy.io as spio
>>> nd=np.random.randint(0,150,size=10)
>>> spio.savemat('nd',{'data':nd})#保存文件,文件名爲nd
>>> spio.loadmat('nd')['data']#讀取文件
array([[ 92, 67, 50, 145, 81, 101, 144, 101, 92, 106]])
>>> #讀取scipy中的misc.imread()/imsave()
>>> import scipy.misc as misc
>>> cat_data=misc.imread ('C:/a/a.jpg')#對圖片進行操做
>>> misc.imshow(cat_data)
>>> misc.imshow(misc.imrotate(cat_data,angle=90))#旋轉90度
>>> a=misc.imresize(cat_data,size=0.5)
>>> misc.imshow(a)#縮小一倍
>>> q=misc.imfilter(cat_data,'blur')#給圖片添加一種模糊效果,smooth是平滑效果,固然還有許多其餘的效果
>>> misc.show(q)
>>> misc.imshow(q)
3:操做圖片
>>> #使用scipy.misc.face(gray=True)獲取圖片,使用ndimage移動座標,旋轉圖片,切割圖片縮放圖片
>>> import numpy as np
>>> import scipy.misc as misc
>>> import scipy.ndimage as ndimage
>>> face=misc.face(gray=True)#圖片設置爲黑白色了
>>> misc.imshow(face)
>>> import matplotlib.pyplot as plt
>>> ndimage.shift(face,[200,0])#圖片向下移動200個單位
array([[ 0, 0, 0, ..., 0, 0, 0],
[ 0, 0, 0, ..., 0, 0, 0],
[ 0, 0, 0, ..., 0, 0, 0],
...,
[203, 207, 210, ..., 102, 100, 100],
[205, 208, 210, ..., 111, 109, 108],
[206, 210, 211, ..., 119, 117, 116]], dtype=uint8)
>>> ss=ndimage.shift(face,[200,0])#圖片向下移動200個單位
>>> plt.imshow(ss)
<matplotlib.image.AxesImage object at 0x00000000110F8A58>
>>> plt.show()
>>> ss1=ndimage.shift(face,[350,0],mode='mirror')#圖片向下移動350個單位,併產生鏡像效果
>>> plt.imshow(ss)
<matplotlib.image.AxesImage object at 0x000000001161C9B0>
>>> plt.show()
>>> plt.imshow(ss1)
<matplotlib.image.AxesImage object at 0x000000001180EFD0>
>>> plt.show()
>>> #mode 還能夠指定爲near和wrap等
>>> r=ndimage.rotate(face,angle=180,axes=(0,1))
>>> plt.imshow(r)
<matplotlib.image.AxesImage object at 0x000000001D7A3470>
>>> plt.show()
>>> #旋轉
>>> #下面是縮放
>>> z=ndimage.zoom(face,zoom=0.5)
>>> plt.imshow(z)
<matplotlib.image.AxesImage object at 0x00000000117BE7B8>
>>> plt.show()
>>> #縮小一半
>>> face2=face[:512,-512:]
>>> plt.imshow(face2)
<matplotlib.image.AxesImage object at 0x000000001DA75B38>
>>> plt.show()
#
>>> face_g =ndimage.gaussian_filter(face,sigma=1)
>>> plt.imshow(face_g)
<matplotlib.image.AxesImage object at 0x0000000010D15DA0>
>>> plt.show()
>>> #高斯濾波能夠使圖片變得清晰些
>>> plt_m=ndimage.median_filter(face,size=2)
>>> plt.imshow(plt_m)
<matplotlib.image.AxesImage object at 0x000000001E2E58D0>
>>> plt.show()
>>> #中值濾波能夠使圖片變得清晰些
>>> #signal維納濾波,濾鏡尺寸的標量
>>> import scipy.signal as signal
>>> sw=signal.wiener(face,mysize=10)
>>> plt.imshow(sw)
<matplotlib.image.AxesImage object at 0x000000001DC1D278>
>>> plt.show()