-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomography-example.py
54 lines (44 loc) · 1.36 KB
/
homography-example.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
#!/bin/python
"""
USAGE:
homography-example.py <image>
"""
import cv2
import numpy as np
import docopt
points1 = []
points2 = []
arguments = docopt.docopt(__doc__)
img1 = cv2.imread(arguments['<image>'])
img1_display = img1.copy()
img2 = np.zeros(img1.shape)
def mark(img, x, y):
for i in range(y-5, y+5):
for j in range(x-5, x+5):
img[i][j] = [0,0,255]
def runprogram():
H = cv2.getPerspectiveTransform(np.float32(points1), np.float32(points2))
img_p = cv2.warpPerspective(img1, H, (img1.shape[1], img1.shape[0]))
for p in points2:
mark(img_p, p[0], p[1])
cv2.imshow('Image2', img_p)
def CallBackFunc(event, x, y, flags, userdata):
if event == cv2.EVENT_LBUTTONDOWN:
print 'Click (%s,%s)' % (x, y)
if userdata == 1 and len(points1) < 4:
mark(img1_display, x, y)
cv2.imshow("Image1", img1_display)
points1.append([x,y])
elif userdata == 2 and len(points2) < 4:
mark(img2, x, y)
cv2.imshow("Image2", img2)
points2.append([x,y])
if len(points2) == 4:
runprogram()
cv2.namedWindow('Image1')
cv2.setMouseCallback('Image1', CallBackFunc, 1)
cv2.imshow('Image1', img1_display)
cv2.namedWindow('Image2')
cv2.setMouseCallback('Image2', CallBackFunc, 2)
cv2.imshow("Image2", img2)
cv2.waitKey()