Mercurial > hg > nsaunier > traffic-intelligence
comparison python/moving.py @ 248:571ba5ed22e2
added utils for bus processing
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Wed, 18 Jul 2012 02:54:22 -0400 |
| parents | bd8ab323c198 |
| children | 99173da7afae |
comparison
equal
deleted
inserted
replaced
| 247:8f0ed138d373 | 248:571ba5ed22e2 |
|---|---|
| 2 '''Libraries for moving objects, trajectories...''' | 2 '''Libraries for moving objects, trajectories...''' |
| 3 | 3 |
| 4 import utils; | 4 import utils; |
| 5 import cvutils; | 5 import cvutils; |
| 6 | 6 |
| 7 from math import sqrt, hypot; | 7 from math import sqrt; |
| 8 | 8 |
| 9 #from shapely.geometry import Polygon | 9 #from shapely.geometry import Polygon |
| 10 | 10 |
| 11 __metaclass__ = type | 11 __metaclass__ = type |
| 12 | 12 |
| 46 def inside(self, interval2): | 46 def inside(self, interval2): |
| 47 'indicates if the temporal interval of self is comprised in interval2' | 47 'indicates if the temporal interval of self is comprised in interval2' |
| 48 return (self.first >= interval2.first) and (self.last <= interval2.last) | 48 return (self.first >= interval2.first) and (self.last <= interval2.last) |
| 49 | 49 |
| 50 def union(self, interval2): | 50 def union(self, interval2): |
| 51 '''Largest interval comprising self and interval2''' | 51 '''Smallest interval comprising self and interval2''' |
| 52 return TimeInterval(min(self.first, interval2.first), max(self.last, interval2.last)) | 52 return TimeInterval(min(self.first, interval2.first), max(self.last, interval2.last)) |
| 53 | 53 |
| 54 def intersection(self, interval2): | 54 def intersection(self, interval2): |
| 55 '''Largest interval comprised in both self and interval2''' | 55 '''Largest interval comprised in both self and interval2''' |
| 56 return TimeInterval(max(self.first, interval2.first), min(self.last, interval2.last)) | 56 return TimeInterval(max(self.first, interval2.first), min(self.last, interval2.last)) |
| 57 | |
| 58 def distance(self, interval2): | |
| 59 if not self.intersection(interval2).empty(): | |
| 60 return 0 | |
| 61 elif self.first > interval2.last: | |
| 62 return self.first - interval2.last | |
| 63 elif self.last < interval2.first: | |
| 64 return interval2.first - self.last | |
| 65 else: | |
| 66 return None | |
| 67 | |
| 68 | |
| 69 def unionIntervals(intervals): | |
| 70 'returns the smallest interval containing all intervals' | |
| 71 inter = intervals[0] | |
| 72 for i in intervals[1:]: | |
| 73 inter = inter.union(i) | |
| 74 return inter | |
| 57 | 75 |
| 58 | 76 |
| 59 class TimeInterval(Interval): | 77 class TimeInterval(Interval): |
| 60 '''Temporal interval based on frame numbers (hence the modified length method) | 78 '''Temporal interval based on frame numbers (hence the modified length method) |
| 61 may be modified directly by setting first and last''' | 79 may be modified directly by setting first and last''' |
| 377 def norm(self): | 395 def norm(self): |
| 378 '''Returns the list of the norms at each instant''' | 396 '''Returns the list of the norms at each instant''' |
| 379 # def add(x, y): return x+y | 397 # def add(x, y): return x+y |
| 380 # sq = map(add, [x*x for x in self.positions[0]], [y*y for y in self.positions[1]]) | 398 # sq = map(add, [x*x for x in self.positions[0]], [y*y for y in self.positions[1]]) |
| 381 # return sqrt(sq) | 399 # return sqrt(sq) |
| 382 return [hypot(x,y) for x,y in zip(self.positions[0], self.positions[1])] | 400 from numpy import hypot |
| 401 return hypot(self.positions[0], self.positions[1]) | |
| 383 | 402 |
| 384 def cumulatedDisplacement(self): | 403 def cumulatedDisplacement(self): |
| 385 'Returns the sum of the distances between each successive point' | 404 'Returns the sum of the distances between each successive point' |
| 386 displacement = 0 | 405 displacement = 0 |
| 387 for i in xrange(self.length()-1): | 406 for i in xrange(self.length()-1): |
| 504 self.positions.draw(options, withOrigin, **kwargs) | 523 self.positions.draw(options, withOrigin, **kwargs) |
| 505 | 524 |
| 506 def drawOnWorldImage(self, nPixelsPerUnitDistance, imageHeight, options = '', withOrigin = False, **kwargs): | 525 def drawOnWorldImage(self, nPixelsPerUnitDistance, imageHeight, options = '', withOrigin = False, **kwargs): |
| 507 self.positions.drawOnWorldImage(nPixelsPerUnitDistance, imageHeight, options, withOrigin, **kwargs) | 526 self.positions.drawOnWorldImage(nPixelsPerUnitDistance, imageHeight, options, withOrigin, **kwargs) |
| 508 | 527 |
| 528 def play(self, videoFilename, homography = None): | |
| 529 cvutils.displayTrajectories(videoFilename, [self], homography, self.getFirstInstant(), self.getLastInstant()) | |
| 530 | |
| 509 def getInstantsCrossingLane(self, p1, p2): | 531 def getInstantsCrossingLane(self, p1, p2): |
| 510 '''Returns the instant(s) | 532 '''Returns the instant(s) |
| 511 at which the object passes from one side of the segment to the other | 533 at which the object passes from one side of the segment to the other |
| 512 empty list if there is no crossing''' | 534 empty list if there is no crossing''' |
| 513 indices = self.positions.getIntersections(p1, p2) | 535 indices = self.positions.getIntersections(p1, p2) |
