画像の回転 cv2.rotate, np.rot90, cv2.getRotationMatrix2D

pythonで画像を回転させる方法を紹介します。 特に今回は affine変換 による回転 cv2.rotate() による回転 np.rot90() による回転 を紹介します。 開発環境はjupyter notebook、言語はpythonです。 今回使用した画像・コード一式は google drive にも保存しておりますので、必要に応じてお使いください。 作成したコード #モジュールのインポート import cv2 import glob import numpy as np import matplotlib.pyplot as plt import os %matplotlib inline #affine変換用def def rotate_affine(path,angle): img = cv2.imread(path) # 変換後の画像高さを定義 height = img.shape[0] # 変換後の画像幅を定義 width = img.shape[1] # 回転の軸を指定:今回は中心 center = (int(width/2), int(height/2)) # scaleを指定 scale = 1 trans = cv2.getRotationMatrix2D(center, angle,scale) img_rotate_affine = cv2.warpAffine(img, trans, (width,height),flags=cv2.INTER_CUBIC) cv2.imwrite(os.path.split(path)[1][:-4]+"_rotate_affine_"+str(angle)+".bmp",img_rotate_affine) return #画像の読み込み filename = glob.glob("*.jpg") for n in range(len(filename)): img = cv2.imread(filename[n]) angle_degree = 0 #1....