Mercurial > hg > nsaunier > traffic-intelligence
comparison python/utils.py @ 29:ca8e716cc231
added moving average filter
| author | Nicolas Saunier <nico@confins.net> |
|---|---|
| date | Sat, 30 Jan 2010 21:43:07 -0500 |
| parents | 44689029a86f |
| children | c000f37c316d |
comparison
equal
deleted
inserted
replaced
| 28:9ae709a2e8d0 | 29:ca8e716cc231 |
|---|---|
| 46 else: | 46 else: |
| 47 return None | 47 return None |
| 48 | 48 |
| 49 def crossProduct(l1, l2): | 49 def crossProduct(l1, l2): |
| 50 return l1[0]*l2[1]-l1[1]*l2[0] | 50 return l1[0]*l2[1]-l1[1]*l2[0] |
| 51 | |
| 52 def filterMovingWindow(input, halfWidth): | |
| 53 '''Returns an array obtained after the smoothing of the input by a moving average | |
| 54 The first and last points are copied from the original.''' | |
| 55 width = float(halfWidth*2+1) | |
| 56 win = ones(width,'d') | |
| 57 result = convolve(win/width,array(inputSignal),'same') | |
| 58 result[:halfWidth] = inputSignal[:halfWidth] | |
| 59 result[-halfWidth:] = inputSignal[-halfWidth:] | |
| 60 return result | |
| 51 | 61 |
| 52 ######################### | 62 ######################### |
| 53 # file I/O section | 63 # file I/O section |
| 54 ######################### | 64 ######################### |
| 55 | 65 |
| 111 from numpy.linalg.linalg import inv | 121 from numpy.linalg.linalg import inv |
| 112 invH = inv(homography) | 122 invH = inv(homography) |
| 113 invH /= invH[2,2] | 123 invH /= invH[2,2] |
| 114 return invH | 124 return invH |
| 115 | 125 |
| 116 def project(homography, p): | |
| 117 '''Returns the coordinates of the projection of the point p | |
| 118 through homography''' | |
| 119 from numpy.core._dotblas import dot | |
| 120 from numpy.core.multiarray import array | |
| 121 from numpy.lib.function_base import insert | |
| 122 if (homography!=None) and (len(homography)>0): | |
| 123 pAugmented = insert(array(p), [2],[1], axis=0) | |
| 124 projected = dot(homography, pAugmented) | |
| 125 projected[0] /= projected[2] | |
| 126 projected[1] /= projected[2] | |
| 127 else: | |
| 128 projected = p | |
| 129 return projected[:2] | |
| 130 | |
| 131 def projectTrajectory(homography, trajectory): | |
| 132 '''Projects a series of points in the format | |
| 133 [[x1, x2, ...], | |
| 134 [y1, y2, ...]] | |
| 135 | |
| 136 Warning: not optimized, calls project()''' | |
| 137 projected = [[],[]] | |
| 138 for x, y in zip(trajectory[0], trajectory[1]): | |
| 139 p = [x,y] | |
| 140 pp = project(homography, p) | |
| 141 projected[0].append(pp[0]) | |
| 142 projected[1].append(pp[1]) | |
| 143 return projected | |
| 144 | |
| 145 def plotPolygon(poly, options = ''): | 126 def plotPolygon(poly, options = ''): |
| 146 from numpy.core.multiarray import array | 127 from numpy.core.multiarray import array |
| 147 from matplotlib.pyplot import plot | 128 from matplotlib.pyplot import plot |
| 148 from shapely.geometry import Polygon | 129 from shapely.geometry import Polygon |
| 149 | 130 |
