利用scipy实现图像处理

from scipy import misc
f = misc.face()
fgrey = misc.face(gray=True)
import matplotlib.pyplot as plt
plt.imshow(f)
plt.show()

plt.subplot(1,2,1)
plt.imshow(f)
plt.subplot(1,2,2)
plt.imshow(fgrey, cmap=plt.cm.gray)
plt.show()

import numpy as np
from scipy import ndimage
lx, ly = fgrey.shape
crop_fgrey = fgrey[(int)(lx/4):-(int)(lx/4), (int)(ly/4):-(int)(ly/4)]
plt.imshow(crop_fgrey, cmap=plt.cm.gray)

flip_ud_fgrey = np.flipud(fgrey) # np.flipud() 矩阵旋转180度
plt.imshow(flip_ud_fgrey, cmap=plt.cm.gray)
plt.show()

flip_lr_fgrey = np.fliplr(fgrey) # np.fliplr() 矩阵左右翻转
plt.imshow(flip_lr_fgrey, cmap=plt.cm.gray)

# 逆时针旋转45度
rotate_fgrey = ndimage.rotate(fgrey, 45)
plt.imshow(rotate_fgrey, cmap=plt.cm.gray)
plt.show()
# 不改变原图大小旋转45度
rotate_fgrey_noreshape = ndimage.rotate(fgrey, 45, reshape=False)
plt.imshow(rotate_fgrey_noreshape, cmap=plt.cm.gray)
plt.show()

# 放大2倍
zoomed_fgrey = ndimage.zoom(fgrey, 2)
# 缩小一半
zoomed_fgrey2 = ndimage.zoom(fgrey, 0.5)
plt.subplot(1,2,1)
plt.imshow(zoomed_fgrey, cmap=plt.cm.gray)
plt.subplot(1,2,2)
plt.imshow(zoomed_fgrey2, cmap=plt.cm.gray)
plt.show()

总结

  • (1)旋转的方法为——ndimage.rotate(图像, 角度)

  • (2)翻转的方法为——np.flipud(图像)及np.fliplr(图像)

  • (3)缩放的方法为——ndimage.zoom(图像,缩放比例)

猜你喜欢

转载自www.cnblogs.com/LiJinrun/p/13364785.html