2021-04-06 13:34:18 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
2019-10-22 19:19:38 +00:00
|
|
|
import cv2
|
2020-07-06 07:44:19 +00:00
|
|
|
import numpy as np
|
|
|
|
from matplotlib import pyplot as plt
|
2019-10-22 19:19:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_rotation(
|
2021-04-06 13:34:18 +00:00
|
|
|
img: np.ndarray, pt1: np.ndarray, pt2: np.ndarray, rows: int, cols: int
|
|
|
|
) -> np.ndarray:
|
2019-10-22 19:19:38 +00:00
|
|
|
"""
|
|
|
|
Get image rotation
|
2023-08-15 21:27:41 +00:00
|
|
|
:param img: np.ndarray
|
2019-10-22 19:19:38 +00:00
|
|
|
:param pt1: 3x2 list
|
|
|
|
:param pt2: 3x2 list
|
|
|
|
:param rows: columns image shape
|
|
|
|
:param cols: rows image shape
|
2023-08-15 21:27:41 +00:00
|
|
|
:return: np.ndarray
|
2019-10-22 19:19:38 +00:00
|
|
|
"""
|
|
|
|
matrix = cv2.getAffineTransform(pt1, pt2)
|
|
|
|
return cv2.warpAffine(img, matrix, (rows, cols))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
# read original image
|
2021-04-06 13:34:18 +00:00
|
|
|
image = cv2.imread(
|
|
|
|
str(Path(__file__).resolve().parent.parent / "image_data" / "lena.jpg")
|
|
|
|
)
|
2019-10-22 19:19:38 +00:00
|
|
|
# turn image in gray scale value
|
|
|
|
gray_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
|
|
# get image shape
|
|
|
|
img_rows, img_cols = gray_img.shape
|
|
|
|
|
|
|
|
# set different points to rotate image
|
2021-04-06 13:34:18 +00:00
|
|
|
pts1 = np.array([[50, 50], [200, 50], [50, 200]], np.float32)
|
|
|
|
pts2 = np.array([[10, 100], [200, 50], [100, 250]], np.float32)
|
|
|
|
pts3 = np.array([[50, 50], [150, 50], [120, 200]], np.float32)
|
|
|
|
pts4 = np.array([[10, 100], [80, 50], [180, 250]], np.float32)
|
2019-10-22 19:19:38 +00:00
|
|
|
|
|
|
|
# add all rotated images in a list
|
|
|
|
images = [
|
|
|
|
gray_img,
|
|
|
|
get_rotation(gray_img, pts1, pts2, img_rows, img_cols),
|
|
|
|
get_rotation(gray_img, pts2, pts3, img_rows, img_cols),
|
|
|
|
get_rotation(gray_img, pts2, pts4, img_rows, img_cols),
|
|
|
|
]
|
|
|
|
|
|
|
|
# plot different image rotations
|
|
|
|
fig = plt.figure(1)
|
|
|
|
titles = ["Original", "Rotation 1", "Rotation 2", "Rotation 3"]
|
|
|
|
for i, image in enumerate(images):
|
|
|
|
plt.subplot(2, 2, i + 1), plt.imshow(image, "gray")
|
|
|
|
plt.title(titles[i])
|
|
|
|
plt.axis("off")
|
|
|
|
plt.subplots_adjust(left=0.0, bottom=0.05, right=1.0, top=0.95)
|
|
|
|
plt.show()
|