Skip to content

Commit b8c92f2

Browse files
authored
Add files via upload
1 parent 7ab014e commit b8c92f2

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

transform.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import numpy as np
2+
import cv2
3+
4+
def order_points(pts):
5+
rect = np.zeros((4, 2), dtype = "float32")
6+
7+
s = pts.sum(axis = 1)
8+
rect[0] = pts[np.argmin(s)]
9+
rect[2] = pts[np.argmax(s)]
10+
11+
diff = np.diff(pts, axis = 1)
12+
rect[1] = pts[np.argmin(diff)]
13+
rect[3] = pts[np.argmax(diff)]
14+
15+
return rect
16+
17+
def four_point_transform(image, pts):
18+
rect = order_points(pts)
19+
(tl, tr, br, bl) = rect
20+
21+
widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
22+
widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
23+
maxWidth = max(int(widthA), int(widthB))
24+
25+
heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
26+
heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
27+
maxHeight = max(int(heightA), int(heightB))
28+
29+
dst = np.array([
30+
[0, 0],
31+
[maxWidth - 1, 0],
32+
[maxWidth - 1, maxHeight - 1],
33+
[0, maxHeight - 1]], dtype = "float32")
34+
35+
M = cv2.getPerspectiveTransform(rect, dst)
36+
warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))
37+
38+
return warped

0 commit comments

Comments
 (0)