Created
December 17, 2020 10:04
-
-
Save dashjim/cf105cdc66f01810169faee05ee49bb9 to your computer and use it in GitHub Desktop.
Check the image and rotate it
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 guess_direction(cut_points): | |
# bbs = np.asarray(cut_points) | |
bbs = cut_points | |
l = bbs[:, 2] - bbs[:, 0] | |
# Convert negative length to positive. | |
l = np.asarray([-i if i < 0 else i for i in l]) | |
w = bbs[:, 3] - bbs[:, 1] | |
w = np.asarray([-i if i < 0 else i for i in w]) | |
if l.mean() - w.mean() > 0: | |
return 0 # horizontal | |
else: | |
return 1 # Vertical - need to rotate 90 degree | |
def rotate_bound(image, angle=90): | |
# grab the dimensions of the image and then determine the | |
# center | |
(h, w) = image.shape[:2] | |
(cX, cY) = (w // 2, h // 2) | |
# grab the rotation matrix (applying the negative of the | |
# angle to rotate clockwise), then grab the sine and cosine | |
# (i.e., the rotation components of the matrix) | |
M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0) | |
cos = np.abs(M[0, 0]) | |
sin = np.abs(M[0, 1]) | |
# compute the new bounding dimensions of the image | |
nW = int((h * sin) + (w * cos)) | |
nH = int((h * cos) + (w * sin)) | |
# adjust the rotation matrix to take into account translation | |
M[0, 2] += (nW / 2) - cX | |
M[1, 2] += (nH / 2) - cY | |
# perform the actual rotation and return the image | |
return cv2.warpAffine(image, M, (nW, nH)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment