可采用Scipy自带图片,亦可读入本地图片。misc.imread()方式读入失败,故这里采用cv2.imread()读入。
#图片处理
import numpy as np
import scipy.misc as misc
import scipy.ndimage as ndimage
#黑白图
#face =misc.face(gray =True)
#face = misc.imread('handsome.jpg')
import cv2
face = cv2.imread('handsome.jpg',cv2.IMREAD_GRAYSCALE) #读入灰度图
import matplotlib.pyplot as plt
#misc.imshow(face)
#plt.imshow(face, cmp = 'gray') # 'AxesImage' object has no property 'cmp'
plt.imshow(face,cmap =plt.cm.gray)
#图片移动
face2 = ndimage.shift(face,shift = [300,150])# mode = {'reflect', 'constant', 'nearest', 'mirror', 'wrap'}
#plt.imshow(face2,cmap='gray') #invalid character in identifier
plt.imshow(face2,cmap =plt.cm.gray)
#图片移动
face2 = ndimage.shift(face,shift = [100,100],mode = 'mirror')# mode = {'reflect', 'constant', 'nearest', 'mirror', 'wrap'}
#plt.imshow(face2,cmap='gray') #invalid character in identifier
plt.imshow(face2,cmap =plt.cm.gray)
#图片旋转
face3 = ndimage.rotate(face,angle = 180)
#plt.imshow(face3)
#plt.imshow(face3,cmap ='gray')
plt.imshow(face3,cmap =plt.cm.gray)
#图片缩放
face4 = ndimage.zoom(face,zoom = 0.5)
#plt.imshow(face4,cmap =plt.cm.gray)
plt.imshow(face4,cmap ='gray')
face4 = ndimage.zoom(face,zoom = 0.2)
plt.imshow(face4,cmap =plt.cm.gray)
#图片切割
face5 = face[:512,-512:]
plt.imshow(face5, cmap= 'gray')
步骤和上面的类似,就是看着需求操作罢了
import numpy as np
import scipy.misc as misc
import scipy.ndimage as ndimage
import matplotlib.pyplot as plt
import cv2
face = cv2.imread('handsome.jpg') #读入彩图
#face =misc.face(gray =False)
#plt.imshow(face,cmap ='gray')
plt.imshow(face)
#图片移动(三维)
face2 = ndimage.shift(face,shift = [0,500,0],mode = 'mirror')# mode = {'reflect', 'constant', 'nearest', 'mirror', 'wrap'}
#plt.imshow(face2,cmap='gray') #invalid character in identifier
plt.imshow(face2)
#图片移动(三维)
face2 = ndimage.shift(face,shift = [400,-625,2],mode = 'mirror')# mode = {'reflect', 'constant', 'nearest', 'mirror', 'wrap'}
#plt.imshow(face2,cmap='gray') #invalid character in identifier
#plt.imshow(face2,cmap =plt.cm.gray)
cv2.imwrite('mirror.png',face2)
#图片移动(三维)
face_wrap = ndimage.shift(face,shift = [400,-625,1],mode = 'wrap')# mode = {'reflect', 'constant', 'nearest', 'mirror', 'wrap'}
#plt.imshow(face2,cmap='gray') #invalid character in identifier
cv2.imwrite('wrap.png',face_wrap)
#图片旋转,三维二维都一样
face3 = ndimage.rotate(face,angle = 180)
plt.imshow(face3)
#cv2.imwrite('rotate.png',face3)
#图片缩放(三维)
face4 = ndimage.zoom(face,zoom = [0.1,0.1,1])
plt.imshow(face4,cmap =plt.cm.gray)
#图片缩放(三维)
face4 = ndimage.zoom(face,zoom = [2,2,1])
plt.imshow(face4,cmap ='gray')
#plt.imsave('zoom_face.jpg',face4)
#plt.imsave('face.jpg',face)
plt.imshow(face+36,cmap = 'gray')
face = misc.face(gray = True)
face = face[:512,-512:]
plt.imshow(face,cmap ='gray')
import numpy as np
import scipy.misc as misc
import scipy.ndimage as ndimage
import matplotlib.pyplot as plt
#添加噪声
face = misc.face(gray = True)
face = face+face.std()*0.8*np.random.random(face.shape)
#高斯滤波
face_g = ndimage.gaussian_filter(face,sigma = 1)#sigma越大越糊
plt.imshow(face_g,cmap ='gray')
moon = plt.imread(r'E:\Document transfer\Tencent Files\1005634200\FileRecv\数据分析方法 周一56\第15周\moonlanding.png')
moon_g = ndimage.gaussian_filter(moon,sigma = 5)
plt.figure(figsize =(9,8))
plt.imshow(moon_g,cmap ='gray')
#中值滤波
face_m = ndimage.median_filter(face,size=8)
#plt.figure(figsize=(10,6))
plt.imshow(face_m,cmap = 'gray')
#维纳滤波(可处理老照片)
import scipy.signal as signal
face_w = signal.wiener(face,mysize =2)
plt.imshow(face_w,cmap = 'gray')
python scipy库基础学习(图片消噪, 求解圆周率, Scipy文件输入/输出, scipy矩阵)
千锋教育