changeset 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 404f3cade05f
children 74b1fc68d4df
files python/compute-homography.py python/cvutils.py
diffstat 2 files changed, 54 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/python/compute-homography.py	Tue Sep 06 17:55:06 2011 -0400
@@ -0,0 +1,40 @@
+#! /usr/bin/env python
+
+import sys,getopt
+
+import numpy as np
+import cv2
+
+import cvutils
+import utils
+
+options, args = getopt.getopt(sys.argv[1:], 'h',['help','video_frame='])
+options = dict(options)
+
+if '--help' in options.keys() or '-h' in options.keys():
+    print('''The argument should be the name of a file containing at least 4 non-colinear point coordinates:
+ - the first two lines are the x and y coordinates in the projected space (usually world space)
+ - the last two lines are the x and y coordinates in the origin space (usually image space)''')
+    sys.exit()
+
+if len(args) == 0:
+    print('Usage: {0} --help|-h [--video_frame <video frame filename>] [<point_correspondences.txt>]'.format(sys.argv[0]))
+    sys.exit()
+
+points = np.loadtxt(args[0], dtype=np.float32)
+srcPts = points[2:,:].T
+dstPts = points[:2,:].T
+homography, mask = cv2.findHomography(srcPts, dstPts) # method=0, ransacReprojThreshold=3
+np.savetxt(utils.removeExtension(sys.argv[1])+'-homography.txt',homography)
+
+if '--video_frame' in options.keys() and homography.size>0:
+    img = cv2.imread(options['--video_frame'])
+    for p in srcPts:
+        cv2.circle(img,tuple(p),2,cvutils.cvRed)
+    invHomography = np.linalg.inv(homography)
+    projectedDstPts = cvutils.projectArray(invHomography, dstPts.T).T
+    for i,p in enumerate(projectedDstPts):
+        cv2.circle(img,tuple(np.int32(np.round(p))),2,cvutils.cvBlue)
+        print('img: {0} / projected: {1}'.format(srcPts[i], p))
+    cv2.imshow('video frame',img)
+    cv2.waitKey()
--- a/python/cvutils.py	Thu Sep 01 18:37:35 2011 -0400
+++ b/python/cvutils.py	Tue Sep 06 17:55:06 2011 -0400
@@ -3,18 +3,27 @@
 
 import Image, ImageDraw # PIL
 try:
-    import cv,cv2
+    import cv2
     opencvExists = True
 except ImportError:
     print('OpenCV library could not be loaded')
     opencvExists = False
 from sys import stdout
 
+import utils
+
 #import aggdraw # agg on top of PIL (antialiased drawing)
 #import utils
 
 __metaclass__ = type
 
+cvRed = (0,0,255)
+cvGreen = (0,255,0)
+cvBlue = (255,0,0)
+cvColors = utils.PlottingPropertyValues([cvRed,
+                                         cvGreen,
+                                         cvBlue])
+
 def drawLines(filename, origins, destinations, w = 1, resultFilename='image.png'):
     '''Draws lines over the image '''
     
@@ -33,10 +42,10 @@
 
 def computeHomography(srcPoints, dstPoints, method=0, ransacReprojThreshold=0.0):
     '''Returns the homography matrix mapping from srcPoints to dstPoints (dimension Nx2)'''
-    cvSrcPoints = arrayToCvMat(srcPoints);
-    cvDstPoints = arrayToCvMat(dstPoints);
-    H = cv.CreateMat(3, 3, cv.CV_64FC1)
-    cv.FindHomography(cvSrcPoints, cvDstPoints, H, method, ransacReprojThreshold)
+    #cvSrcPoints = arrayToCvMat(srcPoints);
+    #cvDstPoints = arrayToCvMat(dstPoints);
+    #H = cv.CreateMat(3, 3, cv.CV_64FC1)
+    H, mask = cv2.findHomography(srcPoints, dstPoints, method, ransacReprojThreshold)
     return H
 
 def cvMatToArray(cvmat):