Mercurial Hosting > traffic-intelligence
annotate python/compute-homography.py @ 151:4af774bb186d
wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Tue, 06 Sep 2011 17:55:06 -0400 |
parents | |
children | b0719b3ad3db |
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 |
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
|
14 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
|
15 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
|
16 - 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
|
17 - 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
|
18 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
|
19 |
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
|
20 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
|
21 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
|
22 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
|
23 |
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
|
24 points = np.loadtxt(args[0], dtype=np.float32) |
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
|
25 srcPts = points[2:,:].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
|
26 dstPts = points[:2,:].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
|
27 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
|
28 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
|
29 |
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 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
|
31 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
|
32 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
|
33 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
|
34 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
|
35 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
|
36 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
|
37 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
|
38 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
|
39 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
|
40 cv2.waitKey() |