Mercurial Hosting > traffic-intelligence
diff python/cvutils.py @ 98:b85912ab4064
refactored projection functions
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Tue, 12 Jul 2011 16:43:33 -0400 |
parents | 40e8e3bb3702 |
children | e7dc5a780f09 |
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'