M=cv2.getRotationMatrix2D(center, angle, scale)
函数有三个输入参数:
center:图片的旋转中心
angle:旋转角度
scale:旋转后图像相比原来的缩放比例
M:计算得到的旋转矩阵
from matplotlib import pyplot as plt
import cv2
import numpy as np
img = cv2.imread('aier.jpg')
rows,cols = img.shape[:2]
# 第一个参数旋转中心,第二个参数旋转角度,第三个参数:缩放比例, 生成一2*3的矩阵
M = cv2.getRotationMatrix2D((cols/2,rows/2),90,1)
M1 = cv2.getRotationMatrix2D((cols/2,rows/2),180,1)
M2 = cv2.getRotationMatrix2D((cols/2,rows/2),60,1)
print(M)
'''
[[ 6.123234e-17 1.000000e+00 1.500000e+02]
[-1.000000e+00 6.123234e-17 6.500000e+02]]
'''
# 第三个参数:变换后的图像大小
img_tra = cv2.warpAffine(img,M,(cols,rows))
img_tra1 = cv2.warpAffine(img,M1,(cols,rows))
img_tra2 = cv2.warpAffine(img,M2,(cols,rows), borderValue=(155, 100, 155))
plt.figure(figsize=(8,8))
plt.subplot(221)
plt.imshow(img[:,:,::-1])
plt.subplot(222)
plt.imshow(img_tra[:,:,::-1])
plt.subplot(223)
plt.imshow(img_tra1[:,:,::-1])
plt.subplot(224)
plt.imshow(img_tra2[:,:,::-1])