annotate python/compute-homography.py @ 235:584613399513

added script and functions to remove object tables
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 05 Jul 2012 23:32:14 -0400
parents ba71924cadf5
children eb4525853030
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
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
3 import sys,getopt
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
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
5 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
6 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
7
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 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
9 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
10
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 options, args = getopt.getopt(sys.argv[1:], 'h',['help','video_frame='])
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
12 options = dict(options)
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
13
217
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
14 # 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
15 # cameraMat = load(videoFilenamePrefix+'-camera.txt');
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
16 # T1 = cameraMat[3:6,:].copy();
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
17 # A = cameraMat[0:3,0:3].copy();
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
18
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
19 # # pay attention, rotation may be the transpose
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
20 # # R = T1[:,0:3].T;
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
21 # R = T1[:,0:3];
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
22 # rT = dot(R, T1[:,3]/1000);
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
23 # T = zeros((3,4),'f');
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
24 # T[:,0:3] = R[:];
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
25 # T[:,3] = rT;
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
26
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
27 # AT = dot(A,T);
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
28
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
29 # nPoints = 4;
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
30 # worldPoints = cvCreateMat(nPoints, 3, CV_64FC1);
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
31 # imagePoints = cvCreateMat(nPoints, 3, CV_64FC1);
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
32
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
33 # # extract homography from the camera calibration
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
34 # worldPoints = cvCreateMat(4, 3, CV_64FC1);
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
35 # imagePoints = cvCreateMat(4, 3, CV_64FC1);
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
36
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
37 # worldPoints[0,:] = [[1, 1, 0]];
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
38 # worldPoints[1,:] = [[1, 2, 0]];
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
39 # worldPoints[2,:] = [[2, 1, 0]];
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
40 # worldPoints[3,:] = [[2, 2, 0]];
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
41
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
42 # wPoints = [[1,1,2,2],
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
43 # [1,2,1,2],
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
44 # [0,0,0,0]];
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
45 # iPoints = utils.worldToImage(AT, wPoints);
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
46
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
47 # for i in range(nPoints):
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
48 # imagePoints[i,:] = [iPoints[:,i].tolist()];
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 # H = cvCreateMat(3, 3, CV_64FC1);
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
51
ba71924cadf5 added comment/TODO
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 160
diff changeset
52 # cvFindHomography(imagePoints, worldPoints, H);
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
235
584613399513 added script and functions to remove object tables
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 217
diff changeset
55 if '--help' in options.keys() or '-h' in options.keys() or len(args) == 0::
584613399513 added script and functions to remove object tables
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 217
diff changeset
56 print('Usage: {0} --help|-h [--video_frame <video frame filename>] [<point_correspondences.txt>]'.format(sys.argv[0]))
584613399513 added script and functions to remove object tables
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 217
diff changeset
57 print('''The positional argument should be the name
584613399513 added script and functions to remove object tables
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 217
diff changeset
58 of a file containing at least 4 non-colinear point coordinates (point correspondences:
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
59 - the first two lines are the x and y coordinates in the projected space (usually world space)
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
60 - the last two lines are the x and y coordinates in the origin space (usually image space)''')
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
61 sys.exit()
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
62
160
b0719b3ad3db created function to load point correspondences and updates scripts that use it
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 151
diff changeset
63 dstPts, srcPts = cvutils.loadPointCorrespondences(args[0])
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
64 homography, mask = cv2.findHomography(srcPts, dstPts) # method=0, ransacReprojThreshold=3
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
65 np.savetxt(utils.removeExtension(sys.argv[1])+'-homography.txt',homography)
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
66
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
67 if '--video_frame' in options.keys() and homography.size>0:
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
68 img = cv2.imread(options['--video_frame'])
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
69 for p in srcPts:
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
70 cv2.circle(img,tuple(p),2,cvutils.cvRed)
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 invHomography = np.linalg.inv(homography)
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
72 projectedDstPts = cvutils.projectArray(invHomography, dstPts.T).T
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
73 for i,p in enumerate(projectedDstPts):
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
74 cv2.circle(img,tuple(np.int32(np.round(p))),2,cvutils.cvBlue)
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
75 print('img: {0} / projected: {1}'.format(srcPts[i], p))
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
76 cv2.imshow('video frame',img)
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
77 cv2.waitKey()