アフィン変換の特殊な例。任意の回転変換と平行移動を組合せた変換。
Link API Reference .
# -*- coding: utf-8 -*-
import cv2
import numpy as np
# Ipythonで表示用の設定
import matplotlib.pyplot as plt
%matplotlib inline
# 画像読込
in_img = cv2.imread("lena.jpg")
# 画像のサイズ(縦、横)、チャンネル数)を取得
h, w, ch = in_img.shape
#ユークリッド変換行列の生成
#回転成分
a = np.cos(30.0 * np.pi/180.0)
b = -np.sin(30.0 * np.pi/180.0)
c = np.sin(30.0 * np.pi/180.0)
d = np.cos(30.0 * np.pi/180.0)
#平行移動成分
tx = 300
ty = 50
matrix = [[a, b, tx],
[c, d, ty]]
euclidean_matrix = np.float32(matrix)
# アフィン変換実行
out_img = cv2.warpAffine(in_img, euclidean_matrix, (w*2,h*2))
#OpenCVがBGRなのでRGBに変換
disp_in_img = cv2.cvtColor(in_img, cv2.COLOR_BGR2RGB)
disp_out_img = cv2.cvtColor(out_img, cv2.COLOR_BGR2RGB)
#画像表示
plt.figure(figsize=(5,5))
plt.subplot(211)
plt.imshow(disp_in_img)
plt.figure(figsize=(10,10))
plt.subplot(212)
plt.imshow(disp_out_img)