Skip to content

Add algorithm to rotate images #1420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 22, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Edit function to be compliant in Black and Flake8 formats
  • Loading branch information
matheustguimaraes committed Oct 22, 2019
commit 505d1364ae6ce1637fe9f127ce26fc4d3166cba9
35 changes: 16 additions & 19 deletions digital_image_processing/rotation/rotation.py
Original file line number Diff line number Diff line change
@@ -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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added type hints in get_rotation(). Thanks for the help!

Expand All @@ -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

Expand All @@ -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")
Copy link
Member

@cclauss cclauss Oct 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for i, image in enumerate(images):
    plt.subplot(2, 2, i + 1), plt.imshow(image, "gray")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added enumerate in loop.

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()