changeset 14:e7bbe8465591

homography and other utils
author Nicolas Saunier <nico@confins.net>
date Sat, 14 Nov 2009 19:02:46 -0500
parents 30559b2cf7a9
children 3ead4bcd001c 9d6831cfe675
files python/moving.py python/utils.py
diffstat 2 files changed, 37 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/python/moving.py	Fri Nov 13 19:29:01 2009 -0500
+++ b/python/moving.py	Sat Nov 14 19:02:46 2009 -0500
@@ -101,6 +101,15 @@
         from matplotlib.pylab import plot
         plot(self.positions[0], self.positions[1])
 
+    def xBounds(self):
+        # look for function that does min and max in one pass
+        return [min(self.positions[0]), max(self.positions[0])]
+    
+    def yBounds(self):
+        # look for function that does min and max in one pass
+        return [min(self.positions[1]), max(self.positions[1])]
+    
+
 class MovingObject(STObject):
     '''Class for moving objects
     i.e. with a trajectory and a geometry (volume)
--- a/python/utils.py	Fri Nov 13 19:29:01 2009 -0500
+++ b/python/utils.py	Sat Nov 14 19:02:46 2009 -0500
@@ -45,7 +45,8 @@
         return filename
 
 def listfiles(dirname, extension, remove = False):
-    '''Returns the list of files with the extension in the directory dirname'''
+    '''Returns the list of files with the extension in the directory dirname
+    If remove is True, the filenames are stripped from the extension'''
     from os import listdir
     tmp = [f for f in listdir(dirname) if f.endswith(extension)]
     tmp.sort()
@@ -54,6 +55,32 @@
     else:
         return tmp
 
+def removeFile(filename):
+    '''Deletes the file while avoiding raising an error 
+    if the file does not exist'''
+    if (os.path.exists(filename)):
+        os.remove(filename)
+
+def invertHomography(homography):
+    'Returns an inverted homography'
+    invH = inv(homography)
+    invH /= invH[2,2]
+    return invH
+
+def project(homography, p):
+    '''Returns the coordinates of the projection of the point p
+    through homography'''
+    from numpy.core._dotblas import dot
+    from numpy.core.multiarray import array
+    from numpy.lib.function_base import insert
+    if (homography!=None) and (len(homography)>0):
+        pAugmented = insert(array(p), [2],[1], axis=0)
+        projected = dot(homography, pAugmented)
+        projected[0] /= projected[2]
+        projected[1] /= projected[2]
+    else:
+        projected = p
+    return projected[:2]