Mercurial Hosting > traffic-intelligence
comparison python/moving.py @ 203:e2f31813ade6
added code to display trajectories on videa
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Tue, 06 Mar 2012 18:10:19 -0500 |
parents | c06379f25ab8 |
children | ada6e8fbe4c6 |
comparison
equal
deleted
inserted
replaced
202:b0b964ba9489 | 203:e2f31813ade6 |
---|---|
150 '2-norm distance (Euclidean distance)' | 150 '2-norm distance (Euclidean distance)' |
151 return sqrt(self.norm2Squared()) | 151 return sqrt(self.norm2Squared()) |
152 | 152 |
153 def aslist(self): | 153 def aslist(self): |
154 return [self.x, self.y] | 154 return [self.x, self.y] |
155 | |
156 def astuple(self): | |
157 return (self.x, self.y) | |
155 | 158 |
156 def project(self, homography): | 159 def project(self, homography): |
157 from numpy.core.multiarray import array | 160 from numpy.core.multiarray import array |
158 projected = cvutils.projectArray(homography, array([[self.x], [self.y]])) | 161 projected = cvutils.projectArray(homography, array([[self.x], [self.y]])) |
159 return Point(projected[0], projected[1]) | 162 return Point(projected[0], projected[1]) |
255 i.e. a temporal sequence of positions | 258 i.e. a temporal sequence of positions |
256 | 259 |
257 the class is iterable.''' | 260 the class is iterable.''' |
258 | 261 |
259 def __init__(self, positions=None): | 262 def __init__(self, positions=None): |
260 if positions: | 263 if positions != None: |
261 self.positions = positions | 264 self.positions = positions |
262 else: | 265 else: |
263 self.positions = [[],[]] | 266 self.positions = [[],[]] |
264 | 267 |
265 @staticmethod | 268 @staticmethod |
285 raise StopIteration | 288 raise StopIteration |
286 else: | 289 else: |
287 self.iterInstantNum += 1 | 290 self.iterInstantNum += 1 |
288 return self[self.iterInstantNum-1] | 291 return self[self.iterInstantNum-1] |
289 | 292 |
293 def length(self): | |
294 return len(self.positions[0]) | |
295 | |
290 def addPositionXY(self, x, y): | 296 def addPositionXY(self, x, y): |
291 self.positions[0].append(x) | 297 self.positions[0].append(x) |
292 self.positions[1].append(y) | 298 self.positions[1].append(y) |
293 | 299 |
294 def addPosition(self, p): | 300 def addPosition(self, p): |
295 self.addPositionXY(p.x, p.y) | 301 self.addPositionXY(p.x, p.y) |
296 | 302 |
297 @staticmethod | 303 @staticmethod |
298 def _draw(positions, options = '', withOrigin = False): | 304 def _draw(positions, options = '', withOrigin = False, lastCoordinate = None): |
299 from matplotlib.pylab import plot | 305 from matplotlib.pylab import plot |
300 plot(positions[0], positions[1], options) | 306 if lastCoordinate == None: |
307 plot(positions[0], positions[1], options) | |
308 elif 0 <= lastCoordinate <= len(positions[0]): | |
309 plot(positions[0][:lastCoordinate], positions[1][:lastCoordinate], options) | |
301 if withOrigin: | 310 if withOrigin: |
302 plot([positions[0][0]], [positions[1][0]], 'ro') | 311 plot([positions[0][0]], [positions[1][0]], 'ro') |
303 | 312 |
313 def project(self, homography): | |
314 from numpy.core.multiarray import array | |
315 projected = cvutils.projectArray(homography, array(self.positions)) | |
316 return Trajectory(projected) | |
317 | |
304 def draw(self, options = '', withOrigin = False): | 318 def draw(self, options = '', withOrigin = False): |
305 Trajectory._draw(self.positions, options, withOrigin) | 319 Trajectory._draw(self.positions, options, withOrigin) |
306 | 320 |
307 def drawOnImage(self, nPixelsPerUnitDistance, imageHeight, options = '', withOrigin = False): | 321 def drawAt(self, lastCoordinate, options = '', withOrigin = False): |
322 Trajectory._draw(self.positions, options, withOrigin, lastCoordinate) | |
323 | |
324 def drawOnWorldImage(self, nPixelsPerUnitDistance, imageHeight, options = '', withOrigin = False): | |
308 from matplotlib.pylab import plot | 325 from matplotlib.pylab import plot |
309 imgPositions = [[x*nPixelsPerUnitDistance for x in self.positions[0]], | 326 imgPositions = [[x*nPixelsPerUnitDistance for x in self.positions[0]], |
310 [-x*nPixelsPerUnitDistance+imageHeight for x in self.positions[1]]] | 327 [-x*nPixelsPerUnitDistance+imageHeight for x in self.positions[1]]] |
311 Trajectory._draw(imgPositions, options, withOrigin) | 328 Trajectory._draw(imgPositions, options, withOrigin) |
312 | |
313 def length(self): | |
314 return len(self.positions[0]) | |
315 | 329 |
316 def getXCoordinates(self): | 330 def getXCoordinates(self): |
317 return self.positions[0] | 331 return self.positions[0] |
318 | 332 |
319 def getYCoordinates(self): | 333 def getYCoordinates(self): |
476 return self.positions.getYCoordinates() | 490 return self.positions.getYCoordinates() |
477 | 491 |
478 def draw(self, options = '', withOrigin = False): | 492 def draw(self, options = '', withOrigin = False): |
479 self.positions.draw(options, withOrigin) | 493 self.positions.draw(options, withOrigin) |
480 | 494 |
481 def drawOnImage(self, nPixelsPerUnitDistance, imageHeight, options = '', withOrigin = False): | 495 def drawWorldOnImage(self, nPixelsPerUnitDistance, imageHeight, options = '', withOrigin = False): |
482 self.positions.drawOnImage(nPixelsPerUnitDistance, imageHeight, options, withOrigin) | 496 self.positions.drawWorldOnImage(nPixelsPerUnitDistance, imageHeight, options, withOrigin) |
483 | 497 |
484 def getInstantsCrossingLane(self, p1, p2): | 498 def getInstantsCrossingLane(self, p1, p2): |
485 '''Returns the instant(s) | 499 '''Returns the instant(s) |
486 at which the object passes from one side of the segment to the other | 500 at which the object passes from one side of the segment to the other |
487 empty list if there is no crossing''' | 501 empty list if there is no crossing''' |