-
Notifications
You must be signed in to change notification settings - Fork 0
/
Preprocessing.py
55 lines (47 loc) · 1.46 KB
/
Preprocessing.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
53
54
55
import conf
import cv2
class Distortion:
"""
correct image distortion using camera matrix and distortion coefficient k1
"""
def __init__(self):
pass
def correct(self, frame):
"""
:param frame: numpy matrix
:return: corrected numpy matrix
"""
camera_matrix, _ = cv2.getOptimalNewCameraMatrix(
conf.CAMERA_MATRIX,
conf.DIST_K,
(frame.shape[1], frame.shape[0]),
alpha=1,
)
map1, map2 = cv2.initUndistortRectifyMap(
conf.CAMERA_MATRIX,
conf.DIST_K,
None,
camera_matrix,
(frame.shape[1], frame.shape[0]),
cv2.CV_16SC2
)
return cv2.remap(frame, map1, map2, interpolation=cv2.INTER_LINEAR)
class Prospective:
"""
homographic transformation from the starting position to the upside prospective
"""
def __init__(self):
pass
def frame_transform(self, frame):
"""
:param frame: input numpy matrix to be transformed
:return: transformed numpy matrix
"""
return cv2.warpPerspective(frame, conf.UP_TRANSF_MATRIX, dsize=(774, 555))
def point_transform(self, point):
"""
:param point: tuple of coordinates to be transformed
:return: tuple of transformed coord
"""
point = cv2.perspectiveTransform(point, conf.UP_TRANSF_MATRIX)
return [point[0][0][0], point[0][0][1]]