python + opencv で画像を回転
python + opencvのアフィン変換を使って画像を回転
import numpy as np
import cv2
# 画像読み込み
img_src = cv2.imread("001.png")
# 画像サイズの取得(横, 縦)
size = tuple([img_src.shape[1], img_src.shape[0]])
# 画像の中心位置(x, y)
center = tuple([int(size[0]/2), int(size[1]/2)])
# 回転させたい角度(正の値は反時計回り)
angle = -45.0
# 拡大比率
scale = 1.0
# 回転変換行列の算出
rotation_matrix = cv2.getRotationMatrix2D(center, angle, scale)
# アフィン変換
img_rot = cv2.warpAffine(img_src, rotation_matrix, size, flags=cv2.INTER_CUBIC)
# 表示
cv2.imshow("Show ROTATION Image", img_rot)
cv2.waitKey(0)
cv2.destroyAllWindows()
src
dst
参考にしたサイト:
Python OpenCV3で画像のアフィン変換(中心を指定した回転) | from umentu import stupid