-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtransform.py
50 lines (37 loc) · 2.16 KB
/
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python
__author__ = 'Aleksandar Gyorev'
__email__ = '[email protected]'
import cv2
import numpy as np
class Transform(object):
def __init__(self):
pass
@staticmethod
def get_points_order(_points):
box = np.zeros((4, 2), dtype='float32') # order will be: TL, TR, BR, BL
coord_sum = _points.sum(axis = 1)
box[0] = _points[np.argmin(coord_sum)] # TL - has the min sum
box[2] = _points[np.argmax(coord_sum)] # BR - has the max sum
coord_diff = np.diff(_points, axis = 1)
box[1] = _points[np.argmin(coord_diff)] # TR - has the min diff
box[3] = _points[np.argmax(coord_diff)] # BL - has the max diff
return box # return the ordered coordinates
@staticmethod
def get_box_transform(_image, _points):
init_box = Transform.get_points_order(_points)
(tl, tr, br, bl) = init_box # get the correct order
width_top = np.sqrt(((tl[0] - tr[0]) ** 2) + ((tl[1] - tr[1]) ** 2)) # distance b/w TL and TR
width_bot = np.sqrt(((bl[0] - br[0]) ** 2) + ((bl[1] - br[1]) ** 2)) # distance b/w BL and BR
max_width = max(int(width_top), int(width_bot)) # the width of the new image
height_left = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2)) # distance b/w TL and BL
height_right = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2)) # distance b/w TR and BR
max_height = max(int(height_left), int(height_right)) # the height of the new image
dest_box = np.array([ # the resulting edge points after the transform
[ 0, 0 ], # TL
[max_width - 1, 0 ], # TR
[max_width - 1, max_height - 1 ], # BR
[ 0, max_height - 1]], # BL
dtype='float32')
M = cv2.getPerspectiveTransform(init_box, dest_box) # transformation matrix
image = cv2.warpPerspective(_image, M, (max_width, max_height)) # apply the transform
return image # return the warped image