Mercurial Hosting > traffic-intelligence
annotate python/compute-homography.py @ 231:249d65ff6c35
merged modifications for windows
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Mon, 02 Jul 2012 23:49:39 -0400 |
parents | ba71924cadf5 |
children | 584613399513 |
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 |
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
|
55 if '--help' in options.keys() or '-h' in options.keys(): |
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
|
56 print('''The argument should be the name of a file containing at least 4 non-colinear point coordinates: |
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
|
57 - 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
|
58 - 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
|
59 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
|
60 |
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 if len(args) == 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
|
62 print('Usage: {0} --help|-h [--video_frame <video frame filename>] [<point_correspondences.txt>]'.format(sys.argv[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
|
63 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
|
64 |
160
b0719b3ad3db
created function to load point correspondences and updates scripts that use it
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
151
diff
changeset
|
65 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
|
66 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
|
67 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
|
68 |
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 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
|
70 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
|
71 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
|
72 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
|
73 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
|
74 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
|
75 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
|
76 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
|
77 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
|
78 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
|
79 cv2.waitKey() |