Skip to content
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

enhance rotate function(remove black box) #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 8 additions & 1 deletion FaceRecognizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,11 @@ def shape_to_np(shape, dtype="int"):
cv2.imshow("Detected face", image)
cv2.waitKey(0)

rotateImage.rotateFunction(image)
rotate_90 = rotateImage.rotate_image(image, 90)
rotate_180 = rotateImage.rotate_image(image, 180)

# Rotate Image
cv2.imshow("Image Rotated 90", rotate_90)
cv2.imshow("Image Rotated 180", rotate_180)
cv2.waitKey(0)
cv2.destroyAllWindows()
90 changes: 50 additions & 40 deletions rotateImage.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,51 @@
import cv2

# read image as grey scale

def rotateFunction(img):

# img = cv2.imread(imagePath)
# get image height, width
(h, w) = img.shape[:2
]
# calculate the center of the image
center = (w / 2, h / 2)

angle90 = 90
angle180 = 180

scale = 1.0

# Perform the counter clockwise rotation holding at the center
# 90 degrees
M = cv2.getRotationMatrix2D(center, angle90, scale)
rotated90 = cv2.warpAffine(img, M, (h, w))

# 180 degrees
M = cv2.getRotationMatrix2D(center, angle180, scale)
rotated180 = cv2.warpAffine(img, M, (w, h))

cv2.imshow('Image rotated by 90 degrees', rotated90)
cv2.waitKey(0) # waits until a key is pressed
cv2.destroyAllWindows() # destroys the window showing image

cv2.imshow('Image rotated by 180 degrees', rotated180)
cv2.waitKey(0) # waits until a key is pressed
cv2.destroyAllWindows() # destroys the window showing image

return rotated90, rotated180


if __name__ == '__main__':
image = "C:\\Users\\KSHITIJ\\PycharmProjects\\rotateImage\\virat.jpg"
rotateFunction(image)
import numpy as np


# rotation of the image
def rotate_image(image, angle):
# Size of the image input
size = (image.shape[1], image.shape[0])
center_of_image = tuple(np.array(size) / 2)
rotation = np.vstack(
[cv2.getRotationMatrix2D(center_of_image, angle, 1.0), [0, 0, 1]]
)

rotation2 = np.matrix(rotation[0:2, 0:2])
width = size[0] * 0.5
height = size[1] * 0.5
rotated_coordinates = [
(np.array([-width, height]) * rotation2).A[0],
(np.array([width, height]) * rotation2).A[0],
(np.array([-width, -height]) * rotation2).A[0],
(np.array([width, -height]) * rotation2).A[0]
]

# Size of the image output
x_coordinates = [pt[0] for pt in rotated_coordinates]
x_positif = [x for x in x_coordinates if x > 0]
x_negatif = [x for x in x_coordinates if x < 0]
y_coordinates = [pt[1] for pt in rotated_coordinates]
y_positif = [y for y in y_coordinates if y > 0]
y_negatif = [y for y in y_coordinates if y < 0]
right = max(x_positif)
left = min(x_negatif)
top = max(y_positif)
bottom = min(y_negatif)
new_width = int(abs(right - left))
new_height = int(abs(top - bottom))
translation_matrix = np.matrix([
[1, 0, int(new_width * 0.5 - width)],
[0, 1, int(new_height * 0.5 - height)],
[0, 0, 1]
])

compute_matrix = (np.matrix(translation_matrix) * np.matrix(rotation))[0:2, :]
result = cv2.warpAffine(
image,
compute_matrix,
(new_width, new_height),
flags=cv2.INTER_LINEAR
)

return result