Mercurial Hosting > traffic-intelligence
annotate python/compute-homography.py @ 184:d70e9b36889c
initial work on flow vectors and clustering algorithms
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Fri, 25 Nov 2011 18:38:54 -0500 |
parents | b0719b3ad3db |
children | ba71924cadf5 |
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 |
160
b0719b3ad3db
created function to load point correspondences and updates scripts that use it
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
151
diff
changeset
|
24 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
|
25 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
|
26 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
|
27 |
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 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
|
29 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
|
30 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
|
31 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
|
32 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
|
33 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
|
34 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
|
35 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
|
36 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
|
37 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
|
38 cv2.waitKey() |