图像的几何变换包括移位,旋转,仿射变换(affine transformation :不是特别懂这个是什么意思,根据例子的效果,自己也不知道该用怎样的语言来表述这个变换的具体含义),放大缩小等。
1)translation:改变object的位置
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('dog.png',0)
rows,cols=img.shape
# translation:改变object的位置
M=np.float32([[1,0,100],[0,1,50]])
dst=cv2.warpAffine(img,M,(cols,rows))
2)scale:放大或缩小
# scale:放大
res = cv2.resize(img,None,fx=1.5, fy=1.5, interpolation = cv2.INTER_CUBIC)
# or
# height, width = img.shape[:2]
# res = cv2.resize(img,(2*width, 2*height), interpolation = cv2.INTER_CUBIC)
3)旋转:rotation
#rotation:旋转
M = cv2.getRotationMatrix2D((cols/2,rows/2),180,1)
rota = cv2.warpAffine(img,M,(cols,rows))
旋转180
旋转90
4) Affine Transformation:仿射变换# Affine Transformation:仿射变换,保持直线性以及一条直线上点间距离的比值。
# 不一定保留角度或者长度,但的确有平行线集合变换后彼此之间仍然平行的属性。
img1=cv2.imread('dog.png')
rows,cols,ch=img1.shape
pts1 = np.float32([[50,50],[200,50],[50,200]])
pts2 = np.float32([[10,100],[200,50],[100,250]])
M1 = cv2.getAffineTransform(pts1,pts2)
dst1 = cv2.warpAffine(img1,M1,(cols,rows))
plt.subplot(121),plt.imshow(img1),plt.title('Input')
plt.subplot(122),plt.imshow(dst1),plt.title('Output')
plt.show()
# Perspective Transformation:透视变换,我觉得就跟那放大镜看东西一样,尺寸不变,里面的内容变大,就好像离我们更近了。
img2 = cv2.imread('dog.png')
rows,cols,ch=img2.shape
pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]])
pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])
M2 = cv2.getPerspectiveTransform(pts1,pts2)
dst2 = cv2.warpPerspective(img2,M2,(300,500))
plt.subplot(121),plt.imshow(img2),plt.title('Input')
plt.subplot(122),plt.imshow(dst2),plt.title('Output')
plt.show()
加油,继续学习。