-
Notifications
You must be signed in to change notification settings - Fork 307
/
Copy pathperspective_correction.py
47 lines (39 loc) · 1.61 KB
/
perspective_correction.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
import numpy as np
import cv2 as cv
def mouse_event_handler(event, x, y, flags, param):
if event == cv.EVENT_LBUTTONDOWN:
param.append((x, y))
if __name__ == '__main__':
img_file = '../data/sunglok_card.jpg'
card_size = (450, 250)
offset = 10
# Prepare the rectified points
pts_dst = np.array([[0, 0], [card_size[0], 0], [0, card_size[1]], [card_size[0], card_size[1]]])
# Load an image
img = cv.imread(img_file)
assert img is not None, 'Cannot read the given image, ' + img_file
# Get the matched points from mouse clicks
pts_src = []
wnd_name = 'Perspective Correction: Point Selection'
cv.namedWindow(wnd_name)
cv.setMouseCallback(wnd_name, mouse_event_handler, pts_src)
while len(pts_src) < 4:
img_display = img.copy()
cv.rectangle(img_display, (offset, offset), (offset + card_size[0], offset + card_size[1]), (0, 0, 255), 2)
idx = min(len(pts_src), len(pts_dst))
cv.circle(img_display, offset + pts_dst[idx], 5, (0, 255, 0), -1)
cv.imshow(wnd_name, img_display)
key = cv.waitKey(10)
if key == 27: # ESC
break
if len(pts_src) == 4:
# Calculate planar homography and rectify perspective distortion
H, _ = cv.findHomography(np.array(pts_src), pts_dst)
print(f'pts_src = {pts_src}')
print(f'pts_dst = {pts_dst}')
print(f'H = {H}')
img_rectify = cv.warpPerspective(img, H, card_size)
# Show the rectified image
cv.imshow('Perspective Distortion Correction: Rectified Image', img_rectify)
cv.waitKey(0)
cv.destroyAllWindows()