opencvはcv2.getRotationMatrix2D()を使用して画像の回転を実現します



Opencv Uses Cv2 Getrotationmatrix2d Achieve Image Rotation



M=cv2.getRotationMatrix2D(center, angle, scale)

この関数には、次の3つの入力パラメーターがあります。

中央:画像の回転中心
角度:回転角
スケール:元の画像と比較した回転画像のスケール
M:計算された回転行列



from matplotlib import pyplot as plt import cv2 import numpy as np img = cv2.imread('aier.jpg') rows,cols = img.shape[:2] # The first parameter rotates the center, the second parameter rotates the angle, the third parameter: the scaling ratio, generates a 2*3 matrix 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]] ''' # The third parameter: the size of the transformed image 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])

画像