对一个图像进行旋转和位移:
cv2.getRotationMatrix2D
cv2.warpAffine
# -*- coding: utf-8 -*-
import cv2
import numpy as np
img = cv2.imread('1.png')
x,y,c=img.shape
rotMat = cv2.getRotationMatrix2D((x/2, y/2),45,1)
#center – Center of the rotation in the source image.
#angle – Rotation angle in degrees. Positive values
#mean counter-clockwise rotation (the coordinate origin
#is assumed to be the top-left corner).
#scale – Isotropic scale factor.
#map_matrix – The output affine transformation, 2x3 floating-point matrix.
#在opencv中提供了cv2.getRotationMatrix2D函数获得变换矩阵。
#第一参数指定旋转圆点;
#第二个参数指定旋转角度;
#第二个参数指定缩放比例;
rotMat = np.float32([[1,0,100],[0,1,50]])
#平移100 50
afnImg = cv2.warpAffine(img,rotMat,(x,y))
#dst(x,y)=src(a[0][0]*x+a[0][1]*y,a[1][0]*x+a[1][1]*y)
#https://docs.opencv.org/2.4/modules/imgproc/doc/geometric_transformations.html?highlight=warpaffine#cv2.warpAffine
#第三个参数是输出图像的大小。
#第二个参数是变换矩阵;
cv2.imshow('affine image',afnImg)
cv2.imwrite('affine.png',afnImg)
cv2.waitKey(0)
cv2.destroyAllWindows()