[latexpage]
Rotation atau memutar image, fungsi yang digunakan warpAffine dengan menggunakan matrix rotation. Untuk menghitung matrix rotation digunakan fungsi dibawah.
cv2.getRotationMatrix2D((center_x, center_y), angle, scale )
- center_x adalah kordinat x untuk pusat putaran.
- center_y adalah kordinat y untuk pusat putaran.
- angle adalah derajat putaran.
- scale adalah skala faktor.
Fungsi diatas akan mengahasilkan matrix rotation berikut.
$R = \begin{bmatrix}Cos\theta & – Sin\theta \\Sin\theta & Cos\theta \end{bmatrix}$
Berikut contoh code untuk memutar gambar 90 derajat.
import cv2
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
img = cv2.imread('chureito-pagoda.jpg')
height, width = img.shape[:2]
R = cv2.getRotationMatrix2D((width/2, height/2), 90, 1)
img_rotation = cv2.warpAffine(img, R, (width, height))
plt.imshow(img_rotation)

Untuk file google colab dapat diakses di https://colab.research.google.com/drive/1boyRxoxmWQUEBeshn_TcmohxjoGYhKEL?usp=sharing