Mercurial > hg > nsaunier > traffic-intelligence
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 150:404f3cade05f | 151:4af774bb186d |
|---|---|
| 1 #! /usr/bin/env python | |
| 2 | |
| 3 import sys,getopt | |
| 4 | |
| 5 import numpy as np | |
| 6 import cv2 | |
| 7 | |
| 8 import cvutils | |
| 9 import utils | |
| 10 | |
| 11 options, args = getopt.getopt(sys.argv[1:], 'h',['help','video_frame=']) | |
| 12 options = dict(options) | |
| 13 | |
| 14 if '--help' in options.keys() or '-h' in options.keys(): | |
| 15 print('''The argument should be the name of a file containing at least 4 non-colinear point coordinates: | |
| 16 - the first two lines are the x and y coordinates in the projected space (usually world space) | |
| 17 - the last two lines are the x and y coordinates in the origin space (usually image space)''') | |
| 18 sys.exit() | |
| 19 | |
| 20 if len(args) == 0: | |
| 21 print('Usage: {0} --help|-h [--video_frame <video frame filename>] [<point_correspondences.txt>]'.format(sys.argv[0])) | |
| 22 sys.exit() | |
| 23 | |
| 24 points = np.loadtxt(args[0], dtype=np.float32) | |
| 25 srcPts = points[2:,:].T | |
| 26 dstPts = points[:2,:].T | |
| 27 homography, mask = cv2.findHomography(srcPts, dstPts) # method=0, ransacReprojThreshold=3 | |
| 28 np.savetxt(utils.removeExtension(sys.argv[1])+'-homography.txt',homography) | |
| 29 | |
| 30 if '--video_frame' in options.keys() and homography.size>0: | |
| 31 img = cv2.imread(options['--video_frame']) | |
| 32 for p in srcPts: | |
| 33 cv2.circle(img,tuple(p),2,cvutils.cvRed) | |
| 34 invHomography = np.linalg.inv(homography) | |
| 35 projectedDstPts = cvutils.projectArray(invHomography, dstPts.T).T | |
| 36 for i,p in enumerate(projectedDstPts): | |
| 37 cv2.circle(img,tuple(np.int32(np.round(p))),2,cvutils.cvBlue) | |
| 38 print('img: {0} / projected: {1}'.format(srcPts[i], p)) | |
| 39 cv2.imshow('video frame',img) | |
| 40 cv2.waitKey() |
