-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimutils.py
52 lines (44 loc) · 1.73 KB
/
imutils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import cv2
import numpy as np
def resize_img(img, width=-1, height=-1, interpolation=None):
if height == -1 and width == -1:
raise TypeError("Invalid arguments. Width or height must be provided.")
h = img.shape[0]
w = img.shape[1]
if height == -1:
aspect_ratio = float(w) / h
new_height = int(width / aspect_ratio)
if interpolation is not None:
return cv2.resize(img, (width, new_height), interpolation=interpolation)
else:
return cv2.resize(img, (width, new_height))
elif width == -1:
aspect_ratio = h / float(w)
new_width = int(height / aspect_ratio)
if interpolation is not None:
return cv2.resize(img, (new_width, height), interpolation=interpolation)
else:
return cv2.resize(img, (new_width, height))
def rotate_img(image, angle, bounds=False):
# 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])
if bounds:
# 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
else:
nW = image.shape[1]
nH = image.shape[0]
# perform the actual rotation and return the image
return cv2.warpAffine(image, M, (nW, nH))