Mercurial Hosting > traffic-intelligence
comparison 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 |
comparison
equal
deleted
inserted
replaced
97:b3a1c26e2f22 | 98:b85912ab4064 |
---|---|
22 del draw | 22 del draw |
23 | 23 |
24 #out = utils.openCheck(resultFilename) | 24 #out = utils.openCheck(resultFilename) |
25 img.save(resultFilename) | 25 img.save(resultFilename) |
26 | 26 |
27 def projectArray(homography, points): | |
28 '''Returns the coordinates of the projected points (format 2xN points) | |
29 through homography''' | |
30 from numpy.core._dotblas import dot | |
31 from numpy.core.multiarray import array | |
32 from numpy.lib.function_base import append | |
33 | |
34 if points.shape[0] != 2: | |
35 raise Exception('points of dimension {0} {1}'.format(points.shape[0], points.shape[1])) | |
36 | |
37 if (homography!=None) and homography.size>0: | |
38 augmentedPoints = append(points,[[1]*points.shape[1]], 0) | |
39 prod = dot(homography, augmentedPoints) | |
40 return prod[0:2]/prod[2] | |
41 else: | |
42 return p | |
43 | |
27 def project(homography, p): | 44 def project(homography, p): |
28 '''Returns the coordinates of the projection of the point p | 45 '''Returns the coordinates of the projection of the point p |
29 through homography''' | 46 through homography''' |
30 from numpy.core._dotblas import dot | |
31 from numpy.core.multiarray import array | 47 from numpy.core.multiarray import array |
32 from numpy.lib.function_base import insert | 48 return projectArray(homography, array([[p[0]],p[1]])) |
33 if (homography!=None) and (len(homography)>0): | |
34 pAugmented = insert(array(p), [2],[1], axis=0) | |
35 tmp = dot(homography, pAugmented) | |
36 return [tmp[0]/tmp[2], tmp[1]/tmp[2]] | |
37 else: | |
38 return p | |
39 | 49 |
40 def projectTrajectory(homography, trajectory): | 50 def projectTrajectory(homography, trajectory): |
41 '''Projects a series of points in the format | 51 '''Projects a series of points in the format |
42 [[x1, x2, ...], | 52 [[x1, x2, ...], |
43 [y1, y2, ...]] | 53 [y1, y2, ...]]''' |
44 | 54 from numpy.core.multiarray import array |
45 Warning: not optimized, calls project()''' | 55 return projectArray(homography, array(trajectory)) |
46 projected = [[],[]] | |
47 for x, y in zip(trajectory[0], trajectory[1]): | |
48 p = [x,y] | |
49 pp = project(homography, p) | |
50 projected[0].append(pp[0]) | |
51 projected[1].append(pp[1]) | |
52 return projected | |
53 | 56 |
54 def invertHomography(homography): | 57 def invertHomography(homography): |
55 'Returns an inverted homography' | 58 'Returns an inverted homography' |
56 from numpy.linalg.linalg import inv | 59 from numpy.linalg.linalg import inv |
57 invH = inv(homography) | 60 invH = inv(homography) |