changeset 98:b85912ab4064

refactored projection functions
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 12 Jul 2011 16:43:33 -0400
parents b3a1c26e2f22
children e7dc5a780f09
files python/cvutils.py python/moving.py
diffstat 2 files changed, 27 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/python/cvutils.py	Tue Jun 14 14:14:24 2011 -0400
+++ b/python/cvutils.py	Tue Jul 12 16:43:33 2011 -0400
@@ -24,32 +24,35 @@
     #out = utils.openCheck(resultFilename)
     img.save(resultFilename)
 
-def project(homography, p):
-    '''Returns the coordinates of the projection of the point p
+def projectArray(homography, points):
+    '''Returns the coordinates of the projected points (format 2xN points)
     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)
-        tmp = dot(homography, pAugmented)
-        return [tmp[0]/tmp[2], tmp[1]/tmp[2]]
+    from numpy.lib.function_base import append
+
+    if points.shape[0] != 2:
+        raise Exception('points of dimension {0} {1}'.format(points.shape[0], points.shape[1]))
+
+    if (homography!=None) and homography.size>0:
+        augmentedPoints = append(points,[[1]*points.shape[1]], 0)
+        prod = dot(homography, augmentedPoints)
+        return prod[0:2]/prod[2]
     else:
         return p
 
+def project(homography, p):
+    '''Returns the coordinates of the projection of the point p
+    through homography'''
+    from numpy.core.multiarray import array
+    return projectArray(homography, array([[p[0]],p[1]]))
+
 def projectTrajectory(homography, trajectory):
     '''Projects a series of points in the format
     [[x1, x2, ...],
-    [y1, y2, ...]]
-
-    Warning: not optimized, calls project()'''
-    projected = [[],[]]
-    for x, y in zip(trajectory[0], trajectory[1]):
-        p = [x,y]
-        pp = project(homography, p)
-        projected[0].append(pp[0])
-        projected[1].append(pp[1])
-    return projected
+    [y1, y2, ...]]'''
+    from numpy.core.multiarray import array
+    return projectArray(homography, array(trajectory))
 
 def invertHomography(homography):
     'Returns an inverted homography'
--- a/python/moving.py	Tue Jun 14 14:14:24 2011 -0400
+++ b/python/moving.py	Tue Jul 12 16:43:33 2011 -0400
@@ -2,6 +2,7 @@
 '''Libraries for moving objects, trajectories...'''
 
 import utils;
+import cvutils;
 
 from math import sqrt, hypot;
 
@@ -122,7 +123,7 @@
         return '(%f,%f)'%(self.x,self.y)
 
     def __repr__(self):
-        return str(self)
+        return self.__str__()
 
     def __sub__(self, other):
         return Point(self.x-other.x, self.y-other.y)
@@ -142,6 +143,11 @@
     def aslist(self):
         return [self.x, self.y]
 
+    def project(self, homography):
+        from numpy.core.multiarray import array
+        projected = cvutils.projectArray(homography, array([[self.x], [self.y]]))
+        return Point(projected[0], projected[1])
+
     def inPolygon(self, poly):
         '''Returns if the point x, y is inside the polygon.
         The polygon is defined by the ordered list of points in poly