-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathperspective_transform.py
35 lines (27 loc) · 1.52 KB
/
perspective_transform.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
import cv2
import numpy as np
def calculate_transform_matrices(image_width, image_height):
bottomW = image_width
topW = 249
bottomH = image_height - 20
topH = bottomH - 228
region_vertices = np.array([[((image_width - bottomW) // 2, bottomH),
((image_width - topW) // 2, topH),
((image_width + topW) // 2, topH),
((image_width + bottomW) // 2, bottomH)]])
offsetH = 10
offsetW = 100
dest_vertices = np.array([[(offsetW, image_height - offsetH),
(offsetW, offsetH),
(image_width - offsetW, offsetH),
(image_width - offsetW, image_height - offsetH)]])
perspective_transform_matrix = cv2.getPerspectiveTransform(
np.float32(region_vertices), np.float32(dest_vertices))
inversion_perspective_transform_matrix = cv2.getPerspectiveTransform(
np.float32(dest_vertices), np.float32(region_vertices))
return perspective_transform_matrix, inversion_perspective_transform_matrix
def perspective_transform(img, perspective_transform_matrix):
return cv2.warpPerspective(img, perspective_transform_matrix, (img.shape[1], img.shape[0]), flags=cv2.INTER_LINEAR)
def inversion_perspective_transform(img, inversion_perspective_transform_matrix):
return cv2.warpPerspective(img, inversion_perspective_transform_matrix, (img.shape[1], img.shape[0]),
flags=cv2.INTER_LINEAR)