forked from spmallick/learnopencv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomography2.py
48 lines (34 loc) · 1.16 KB
/
homography2.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
import cv2
import numpy as np
import sys
def mouseHandler(event,x,y,flags,param):
global im_temp, pts_dst
if event == cv2.EVENT_LBUTTONDOWN:
cv2.circle(im_temp,(x,y),3,(0,255,255),5,cv2.LINE_AA)
cv2.imshow("Image", im_temp)
if len(pts_dst) < 4:
pts_dst = np.append(pts_dst,[(x,y)],axis=0)
# Read in the image.
im_src = cv2.imread(sys.argv[1])
height, width = im_src.shape[:2]
# Create a list of points.
pts_src = np.empty((0,2),dtype=np.int32)
pts_src = np.append(pts_src, [(0,0)], axis=0)
pts_src = np.append(pts_src, [(width-1,0)], axis=0)
pts_src = np.append(pts_src, [(width-1,height-1)], axis=0)
pts_src = np.append(pts_src, [(0,height-1)], axis=0)
# Destination image
im_dst = cv2.imread(sys.argv[2])
# Create a window
cv2.namedWindow("Image", 1)
im_temp = im_dst
pts_dst = np.empty((0,2),dtype=np.int32)
cv2.setMouseCallback("Image",mouseHandler)
cv2.imshow("Image", im_temp)
cv2.waitKey(0)
tform, status = cv2.findHomography(pts_src, pts_dst)
im_temp = cv2.warpPerspective(im_src, tform,(width,height))
cv2.fillConvexPoly(im_dst, pts_dst, 0, cv2.LINE_AA)
im_dst = im_dst + im_temp
cv2.imshow("Image", im_dst)
cv2.waitKey(0)