From 5b7f891d30cbe40819e3147d96551ec243bdfef5 Mon Sep 17 00:00:00 2001 From: Matheus Guimaraes Date: Tue, 22 Oct 2019 13:33:45 -0300 Subject: [PATCH 1/3] Add algorithm to rotate image --- digital_image_processing/rotation/__init__.py | 0 digital_image_processing/rotation/rotation.py | 53 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 digital_image_processing/rotation/__init__.py create mode 100644 digital_image_processing/rotation/rotation.py diff --git a/digital_image_processing/rotation/__init__.py b/digital_image_processing/rotation/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/digital_image_processing/rotation/rotation.py b/digital_image_processing/rotation/rotation.py new file mode 100644 index 000000000000..ef3a68918e36 --- /dev/null +++ b/digital_image_processing/rotation/rotation.py @@ -0,0 +1,53 @@ +from cv2 import getAffineTransform, warpAffine, imread, cvtColor, COLOR_BGR2GRAY +from matplotlib import pyplot as plt +from numpy import float32 + + +def get_rotation(img, pt1, pt2, rows, cols): + """ + Get image rotation + :param img: np.array + :param pt1: 3x2 list + :param pt2: 3x2 list + :param rows: columns image shape + :param cols: rows image shape + :return: np.array + """ + matrix = getAffineTransform(pt1, pt2) + return warpAffine(img, matrix, (rows, cols)) + + +if __name__ == '__main__': + # read original image + image = imread('lena.jpg') + # turn image in gray scale value + gray_img = cvtColor(image, COLOR_BGR2GRAY) + # get image shape + img_rows, img_cols = gray_img.shape + + # set different points to rotate image + pts1 = float32([[50, 50], [200, 50], [50, 200]]) + pts2 = float32([[10, 100], [200, 50], [100, 250]]) + pts3 = float32([[50, 50], [150, 50], [120, 200]]) + pts4 = float32([[10, 100], [80, 50], [180, 250]]) + + # 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 in range(0, len(images)): + plt.subplot(2, 2, i + 1), plt.imshow(images[i], 'gray') + plt.title(titles[i]) + plt.axis('off') + plt.subplots_adjust(left=0.0, + bottom=0.05, + right=1.0, + top=0.95, + wspace=0.0) + plt.show() From 505d1364ae6ce1637fe9f127ce26fc4d3166cba9 Mon Sep 17 00:00:00 2001 From: Matheus Guimaraes Date: Tue, 22 Oct 2019 13:49:32 -0300 Subject: [PATCH 2/3] Edit function to be compliant in Black and Flake8 formats --- digital_image_processing/rotation/rotation.py | 35 +++++++++---------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/digital_image_processing/rotation/rotation.py b/digital_image_processing/rotation/rotation.py index ef3a68918e36..5fae754fec4b 100644 --- a/digital_image_processing/rotation/rotation.py +++ b/digital_image_processing/rotation/rotation.py @@ -1,6 +1,6 @@ -from cv2 import getAffineTransform, warpAffine, imread, cvtColor, COLOR_BGR2GRAY from matplotlib import pyplot as plt from numpy import float32 +import cv2 def get_rotation(img, pt1, pt2, rows, cols): @@ -13,15 +13,15 @@ def get_rotation(img, pt1, pt2, rows, cols): :param cols: rows image shape :return: np.array """ - matrix = getAffineTransform(pt1, pt2) - return warpAffine(img, matrix, (rows, cols)) + matrix = cv2.getAffineTransform(pt1, pt2) + return cv2.warpAffine(img, matrix, (rows, cols)) -if __name__ == '__main__': +if __name__ == "__main__": # read original image - image = imread('lena.jpg') + image = cv2.imread("lena.jpg") # turn image in gray scale value - gray_img = cvtColor(image, COLOR_BGR2GRAY) + gray_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # get image shape img_rows, img_cols = gray_img.shape @@ -32,22 +32,19 @@ def get_rotation(img, pt1, pt2, rows, cols): pts4 = float32([[10, 100], [80, 50], [180, 250]]) # 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) - ] + 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'] + titles = ["Original", "Rotation 1", "Rotation 2", "Rotation 3"] for i in range(0, len(images)): - plt.subplot(2, 2, i + 1), plt.imshow(images[i], 'gray') + plt.subplot(2, 2, i + 1), plt.imshow(images[i], "gray") plt.title(titles[i]) - plt.axis('off') - plt.subplots_adjust(left=0.0, - bottom=0.05, - right=1.0, - top=0.95, - wspace=0.0) + plt.axis("off") + plt.subplots_adjust(left=0.0, bottom=0.05, right=1.0, top=0.95) plt.show() From 0b792c5842d9a1396541ed78084a558acd98bc47 Mon Sep 17 00:00:00 2001 From: Matheus Guimaraes Date: Tue, 22 Oct 2019 15:12:53 -0300 Subject: [PATCH 3/3] Add type hints in get_rotation() and enumerate() in loop --- digital_image_processing/rotation/rotation.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/digital_image_processing/rotation/rotation.py b/digital_image_processing/rotation/rotation.py index 5fae754fec4b..37b45ca39897 100644 --- a/digital_image_processing/rotation/rotation.py +++ b/digital_image_processing/rotation/rotation.py @@ -1,9 +1,11 @@ from matplotlib import pyplot as plt -from numpy import float32 +import numpy as np import cv2 -def get_rotation(img, pt1, pt2, rows, cols): +def get_rotation( + img: np.array, pt1: np.float32, pt2: np.float32, rows: int, cols: int +) -> np.array: """ Get image rotation :param img: np.array @@ -26,10 +28,10 @@ def get_rotation(img, pt1, pt2, rows, cols): img_rows, img_cols = gray_img.shape # set different points to rotate image - pts1 = float32([[50, 50], [200, 50], [50, 200]]) - pts2 = float32([[10, 100], [200, 50], [100, 250]]) - pts3 = float32([[50, 50], [150, 50], [120, 200]]) - pts4 = float32([[10, 100], [80, 50], [180, 250]]) + pts1 = np.float32([[50, 50], [200, 50], [50, 200]]) + pts2 = np.float32([[10, 100], [200, 50], [100, 250]]) + pts3 = np.float32([[50, 50], [150, 50], [120, 200]]) + pts4 = np.float32([[10, 100], [80, 50], [180, 250]]) # add all rotated images in a list images = [ @@ -42,8 +44,8 @@ def get_rotation(img, pt1, pt2, rows, cols): # plot different image rotations fig = plt.figure(1) titles = ["Original", "Rotation 1", "Rotation 2", "Rotation 3"] - for i in range(0, len(images)): - plt.subplot(2, 2, i + 1), plt.imshow(images[i], "gray") + 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)