Created
December 2, 2019 13:11
-
-
Save hsuRush/b2def27c98ce7ba3eb84a42e6d01328c to your computer and use it in GitHub Desktop.
rotate and shear without cropping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def rotate_and_shear_image(im, angle=30, x_shear=.7, y_shear=.7): | |
""" | |
Rotates an image (angle in degrees) and expands image to avoid cropping | |
change from https://stackoverflow.com/a/51109152 | |
""" | |
#print(im.shape) | |
height, width = im.shape[:2] # image shape has 3 dimensions | |
image_center = (width/2, height/2) # getRotationMatrix2D needs coordinates in reverse order (width, height) compared to shape | |
rotation_im = cv2.getRotationMatrix2D(image_center, angle, 1.) | |
rotation_im[0,1] += x_shear | |
rotation_im[1,0] += y_shear | |
# rotation calculates the cos and sin, taking absolutes of those. | |
abs_cos_x = abs(rotation_im[0,0]) | |
abs_sin_x = abs(rotation_im[0,1]) | |
abs_cos_y = abs(rotation_im[1,0]) | |
abs_sin_y = abs(rotation_im[1,1]) | |
# find the new width and height bounds | |
bound_w = int(width * abs_cos_x + height * abs_sin_x ) | |
bound_h = int(height * abs_sin_y + width * abs_cos_y ) | |
# subtract old image center (bringing image back to origo) and adding the new image center coordinates | |
rotation_im[0, 2] += bound_w/2 - image_center[0] - image_center[1] * x_shear | |
rotation_im[1, 2] += bound_h/2 - image_center[1] - image_center[0] * y_shear | |
board_mode = cv2.BORDER_CONSTANT | |
color = (randint(230,250), randint(230,250), randint(230,250)) | |
# rotate image with the new bounds and translated rotation imrix | |
im = cv2.warpAffine(im, rotation_im, (bound_w, bound_h), flags=cv2.INTER_LINEAR, borderMode=board_mode, borderValue=color) | |
im = cv2.resize(im, (WEIGHT, HEIGHT)) | |
#print(rotated_im.shape) | |
return im | |
if __name__ == '__main__': | |
im = cv2.imread(your_image_file) | |
im = rotate_and_shear_image(im, angle=30, x_shear=.5, y_shear=.4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment