annotate scripts/compute-homography.py @ 476:6551a3cf1750

modified compute-homography to work with argparse
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 25 Mar 2014 19:43:28 -0400
parents 51810d737d86
children d337bffd7283
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
151
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
1 #! /usr/bin/env python
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
2
476
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
3 import sys, argparse
151
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
4
238
be3761a09b20 added functions to input point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 237
diff changeset
5 import matplotlib.pyplot as plt
151
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
6 import numpy as np
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
7 import cv2
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
8
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
9 import cvutils
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
10 import utils
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
11
476
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
12 parser = argparse.ArgumentParser(description='The program computes the homography matrix from at least 4 non-colinear point correspondences inputed in the same order in a video frame and a aerial photo/ground map, or from the list of corresponding points in the two planes.', epilog = '''The point correspondence file contains at least 4 non-colinear point coordinates
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
13 with the following format:
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
14 - the first two lines are the x and y coordinates in the projected space (usually world space)
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
15 - the last two lines are the x and y coordinates in the origin space (usually image space)
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
16
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
17 If providing video and world images, with a number of points to input
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
18 and a ration to convert pixels to world distance unit (eg meters per pixel),
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
19 the images will be shown in turn and the user should click
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
20 in the same order the corresponding points in world and image spaces.''', formatter_class=argparse.RawDescriptionHelpFormatter,)
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
21
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
22 parser.add_argument('-p', dest = 'pointCorrespondencesFilename', help = 'name of the text file containing the point correspondences')
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
23 parser.add_argument('-i', dest = 'videoFrameFilename', help = 'filename of the video frame')
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
24 parser.add_argument('-w', dest = 'worldFilename', help = 'filename of the aerial photo/ground map')
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
25 parser.add_argument('-n', dest = 'nPoints', help = 'number of corresponding points to input', default = 4, type = int)
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
26 parser.add_argument('-u', dest = 'unitsPerPixel', help = 'number of units per pixel', default = 1., type = float)
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
27 parser.add_argument('--display', dest = 'displayPoints', help = 'display original and projected points on both images', action = 'store_true')
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
28
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
29 args = parser.parse_args()
151
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
30
217
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
31 # TODO process camera intrinsic and extrinsic parameters to obtain image to world homography, taking example from Work/src/python/generate-homography.py script
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
32 # cameraMat = load(videoFilenamePrefix+'-camera.txt');
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
33 # T1 = cameraMat[3:6,:].copy();
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
34 # A = cameraMat[0:3,0:3].copy();
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
35
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
36 # # pay attention, rotation may be the transpose
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
37 # # R = T1[:,0:3].T;
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
38 # R = T1[:,0:3];
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
39 # rT = dot(R, T1[:,3]/1000);
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
40 # T = zeros((3,4),'f');
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
41 # T[:,0:3] = R[:];
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
42 # T[:,3] = rT;
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
43
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
44 # AT = dot(A,T);
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
45
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
46 # nPoints = 4;
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
47 # worldPoints = cvCreateMat(nPoints, 3, CV_64FC1);
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
48 # imagePoints = cvCreateMat(nPoints, 3, CV_64FC1);
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
49
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
50 # # extract homography from the camera calibration
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
51 # worldPoints = cvCreateMat(4, 3, CV_64FC1);
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
52 # imagePoints = cvCreateMat(4, 3, CV_64FC1);
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
53
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
54 # worldPoints[0,:] = [[1, 1, 0]];
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
55 # worldPoints[1,:] = [[1, 2, 0]];
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
56 # worldPoints[2,:] = [[2, 1, 0]];
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
57 # worldPoints[3,:] = [[2, 2, 0]];
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
58
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
59 # wPoints = [[1,1,2,2],
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
60 # [1,2,1,2],
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
61 # [0,0,0,0]];
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
62 # iPoints = utils.worldToImage(AT, wPoints);
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
63
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
64 # for i in range(nPoints):
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
65 # imagePoints[i,:] = [iPoints[:,i].tolist()];
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
66
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
67 # H = cvCreateMat(3, 3, CV_64FC1);
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
68
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
69 # cvFindHomography(imagePoints, worldPoints, H);
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
70
151
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
71
238
be3761a09b20 added functions to input point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 237
diff changeset
72 homography = np.array([])
476
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
73 if args.pointCorrespondencesFilename != None:
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
74 worldPts, videoPts = cvutils.loadPointCorrespondences(args.pointCorrespondencesFilename)
238
be3761a09b20 added functions to input point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 237
diff changeset
75 homography, mask = cv2.findHomography(videoPts, worldPts) # method=0, ransacReprojThreshold=3
476
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
76 elif args.videoFrameFilename != None and args.worldFilename != None:
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
77 worldImg = plt.imread(args.worldFilename)
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
78 videoImg = plt.imread(args.videoFrameFilename)
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
79 print('Click on {0} points in the video frame'.format(args.nPoints))
238
be3761a09b20 added functions to input point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 237
diff changeset
80 plt.figure()
be3761a09b20 added functions to input point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 237
diff changeset
81 plt.imshow(videoImg)
476
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
82 videoPts = np.array(plt.ginput(args.nPoints, timeout=3000))
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
83 print('Click on {0} points in the world image'.format(args.nPoints))
238
be3761a09b20 added functions to input point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 237
diff changeset
84 plt.figure()
be3761a09b20 added functions to input point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 237
diff changeset
85 plt.imshow(worldImg)
476
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
86 worldPts = args.unitsPerPixel*np.array(plt.ginput(args.nPoints, timeout=3000))
238
be3761a09b20 added functions to input point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 237
diff changeset
87 plt.close('all')
be3761a09b20 added functions to input point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 237
diff changeset
88 homography, mask = cv2.findHomography(videoPts, worldPts)
be3761a09b20 added functions to input point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 237
diff changeset
89 # save the points in file
be3761a09b20 added functions to input point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 237
diff changeset
90 f = open('point-correspondences.txt', 'a')
be3761a09b20 added functions to input point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 237
diff changeset
91 np.savetxt(f, worldPts.T)
be3761a09b20 added functions to input point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 237
diff changeset
92 np.savetxt(f, videoPts.T)
be3761a09b20 added functions to input point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 237
diff changeset
93 f.close()
151
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
94
238
be3761a09b20 added functions to input point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 237
diff changeset
95 if homography.size>0:
be3761a09b20 added functions to input point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 237
diff changeset
96 np.savetxt('homography.txt',homography)
be3761a09b20 added functions to input point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 237
diff changeset
97
476
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
98 if args.displayPoints and args.videoFrameFilename != None and args.worldFilename != None and homography.size>0:
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
99 worldImg = plt.imread(args.worldFilename)
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
100 videoImg = plt.imread(args.videoFrameFilename)
151
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
101 invHomography = np.linalg.inv(homography)
238
be3761a09b20 added functions to input point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 237
diff changeset
102 projectedWorldPts = cvutils.projectArray(invHomography, worldPts.T).T
476
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
103 projectedVideoPts = cvutils.projectArray(invHomography, videoPts.T).T
238
be3761a09b20 added functions to input point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 237
diff changeset
104 for i in range(worldPts.shape[0]):
476
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
105 cv2.circle(worldImg,tuple(np.int32(np.round(worldPts[i]/args.unitsPerPixel))),2,cvutils.cvRed)
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
106 cv2.circle(worldImg,tuple(np.int32(np.round(projectedVideoPts[i]/args.unitsPerPixel))),2,cvutils.cvRed)
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
107 # TODO print numbers and in image space
238
be3761a09b20 added functions to input point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 237
diff changeset
108 cv2.imshow('video frame',videoImg)
476
6551a3cf1750 modified compute-homography to work with argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 443
diff changeset
109 #cv2.imshow('world image',worldImg)
151
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
110 cv2.waitKey()
346
5f75d6c23ed5 added opencv function to destroy OpenCV windows (seems to work only on Windows)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 334
diff changeset
111 cv2.destroyAllWindows()