MCPcopy Create free account
hub / github.com/PyImageSearch/imutils / four_point_transform

Function four_point_transform

imutils/perspective.py:36–72  ·  view source on GitHub ↗
(image, pts)

Source from the content-addressed store, hash-verified

34 return np.array([tl, tr, br, bl], dtype="float32")
35
36def four_point_transform(image, pts):
37 # obtain a consistent order of the points and unpack them
38 # individually
39 rect = order_points(pts)
40 (tl, tr, br, bl) = rect
41
42 # compute the width of the new image, which will be the
43 # maximum distance between bottom-right and bottom-left
44 # x-coordiates or the top-right and top-left x-coordinates
45 widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
46 widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
47 maxWidth = max(int(widthA), int(widthB))
48
49 # compute the height of the new image, which will be the
50 # maximum distance between the top-right and bottom-right
51 # y-coordinates or the top-left and bottom-left y-coordinates
52 heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
53 heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
54 maxHeight = max(int(heightA), int(heightB))
55
56 # now that we have the dimensions of the new image, construct
57 # the set of destination points to obtain a "birds eye view",
58 # (i.e. top-down view) of the image, again specifying points
59 # in the top-left, top-right, bottom-right, and bottom-left
60 # order
61 dst = np.array([
62 [0, 0],
63 [maxWidth - 1, 0],
64 [maxWidth - 1, maxHeight - 1],
65 [0, maxHeight - 1]], dtype="float32")
66
67 # compute the perspective transform matrix and then apply it
68 M = cv2.getPerspectiveTransform(rect, dst)
69 warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))
70
71 # return the warped image
72 return warped

Callers

nothing calls this directly

Calls 1

order_pointsFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…