Mercurial > hg > nsaunier > traffic-intelligence
comparison python/moving.py @ 515:727e3c529519
renamed all draw functions to plot for consistency
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Fri, 06 Jun 2014 14:10:42 -0400 |
| parents | a40c75f04903 |
| children | bce1fe45d1b2 |
comparison
equal
deleted
inserted
replaced
| 514:1ba618fb0f70 | 515:727e3c529519 |
|---|---|
| 177 return Point(-self.x, -self.y) | 177 return Point(-self.x, -self.y) |
| 178 | 178 |
| 179 def multiply(self, alpha): | 179 def multiply(self, alpha): |
| 180 return Point(self.x*alpha, self.y*alpha) | 180 return Point(self.x*alpha, self.y*alpha) |
| 181 | 181 |
| 182 def draw(self, options = 'o', **kwargs): | 182 def plot(self, options = 'o', **kwargs): |
| 183 from matplotlib.pylab import plot | 183 from matplotlib.pylab import plot |
| 184 plot([self.x], [self.y], options, **kwargs) | 184 plot([self.x], [self.y], options, **kwargs) |
| 185 | 185 |
| 186 def norm2Squared(self): | 186 def norm2Squared(self): |
| 187 '''2-norm distance (Euclidean distance)''' | 187 '''2-norm distance (Euclidean distance)''' |
| 358 return FlowVector(self.position+other.position, self.velocity+other.velocity) | 358 return FlowVector(self.position+other.position, self.velocity+other.velocity) |
| 359 | 359 |
| 360 def multiply(self, alpha): | 360 def multiply(self, alpha): |
| 361 return FlowVector(self.position.multiply(alpha), self.velocity.multiply(alpha)) | 361 return FlowVector(self.position.multiply(alpha), self.velocity.multiply(alpha)) |
| 362 | 362 |
| 363 def draw(self, options = '', **kwargs): | 363 def plot(self, options = '', **kwargs): |
| 364 from matplotlib.pylab import plot | 364 from matplotlib.pylab import plot |
| 365 plot([self.position.x, self.position.x+self.velocity.x], [self.position.y, self.position.y+self.velocity.y], options, **kwargs) | 365 plot([self.position.x, self.position.x+self.velocity.x], [self.position.y, self.position.y+self.velocity.y], options, **kwargs) |
| 366 self.position.draw(options+'x', **kwargs) | 366 self.position.plot(options+'x', **kwargs) |
| 367 | 367 |
| 368 @staticmethod | 368 @staticmethod |
| 369 def similar(f1, f2, maxDistance2, maxDeltavelocity2): | 369 def similar(f1, f2, maxDistance2, maxDeltavelocity2): |
| 370 return (f1.position-f2.position).norm2Squared()<maxDistance2 and (f1.velocity-f2.velocity).norm2Squared()<maxDeltavelocity2 | 370 return (f1.position-f2.position).norm2Squared()<maxDistance2 and (f1.velocity-f2.velocity).norm2Squared()<maxDeltavelocity2 |
| 371 | 371 |
| 472 def duplicateLastPosition(self): | 472 def duplicateLastPosition(self): |
| 473 self.positions[0].append(self.positions[0][-1]) | 473 self.positions[0].append(self.positions[0][-1]) |
| 474 self.positions[1].append(self.positions[1][-1]) | 474 self.positions[1].append(self.positions[1][-1]) |
| 475 | 475 |
| 476 @staticmethod | 476 @staticmethod |
| 477 def _draw(positions, options = '', withOrigin = False, lastCoordinate = None, timeStep = 1, **kwargs): | 477 def _plot(positions, options = '', withOrigin = False, lastCoordinate = None, timeStep = 1, **kwargs): |
| 478 from matplotlib.pylab import plot | 478 from matplotlib.pylab import plot |
| 479 if lastCoordinate == None: | 479 if lastCoordinate == None: |
| 480 plot(positions[0][::timeStep], positions[1][::timeStep], options, **kwargs) | 480 plot(positions[0][::timeStep], positions[1][::timeStep], options, **kwargs) |
| 481 elif 0 <= lastCoordinate <= len(positions[0]): | 481 elif 0 <= lastCoordinate <= len(positions[0]): |
| 482 plot(positions[0][:lastCoordinate:timeStep], positions[1][:lastCoordinate:timeStep], options, **kwargs) | 482 plot(positions[0][:lastCoordinate:timeStep], positions[1][:lastCoordinate:timeStep], options, **kwargs) |
| 484 plot([positions[0][0]], [positions[1][0]], 'ro', **kwargs) | 484 plot([positions[0][0]], [positions[1][0]], 'ro', **kwargs) |
| 485 | 485 |
| 486 def project(self, homography): | 486 def project(self, homography): |
| 487 return Trajectory(cvutils.projectTrajectory(homography, self.positions)) | 487 return Trajectory(cvutils.projectTrajectory(homography, self.positions)) |
| 488 | 488 |
| 489 def draw(self, options = '', withOrigin = False, timeStep = 1, **kwargs): | 489 def plot(self, options = '', withOrigin = False, timeStep = 1, **kwargs): |
| 490 Trajectory._draw(self.positions, options, withOrigin, None, timeStep, **kwargs) | 490 Trajectory._plot(self.positions, options, withOrigin, None, timeStep, **kwargs) |
| 491 | 491 |
| 492 def drawAt(self, lastCoordinate, options = '', withOrigin = False, timeStep = 1, **kwargs): | 492 def plotAt(self, lastCoordinate, options = '', withOrigin = False, timeStep = 1, **kwargs): |
| 493 Trajectory._draw(self.positions, options, withOrigin, lastCoordinate, timeStep, **kwargs) | 493 Trajectory._plot(self.positions, options, withOrigin, lastCoordinate, timeStep, **kwargs) |
| 494 | 494 |
| 495 def drawOnWorldImage(self, nPixelsPerUnitDistance, imageHeight, options = '', withOrigin = False, timeStep = 1, **kwargs): | 495 def plotOnWorldImage(self, nPixelsPerUnitDistance, imageHeight, options = '', withOrigin = False, timeStep = 1, **kwargs): |
| 496 from matplotlib.pylab import plot | 496 from matplotlib.pylab import plot |
| 497 imgPositions = [[x*nPixelsPerUnitDistance for x in self.positions[0]], | 497 imgPositions = [[x*nPixelsPerUnitDistance for x in self.positions[0]], |
| 498 [-x*nPixelsPerUnitDistance+imageHeight for x in self.positions[1]]] | 498 [-x*nPixelsPerUnitDistance+imageHeight for x in self.positions[1]]] |
| 499 Trajectory._draw(imgPositions, options, withOrigin, timeStep, **kwargs) | 499 Trajectory._plot(imgPositions, options, withOrigin, timeStep, **kwargs) |
| 500 | 500 |
| 501 def getXCoordinates(self): | 501 def getXCoordinates(self): |
| 502 return self.positions[0] | 502 return self.positions[0] |
| 503 | 503 |
| 504 def getYCoordinates(self): | 504 def getYCoordinates(self): |
| 745 return self.positions.getXCoordinates() | 745 return self.positions.getXCoordinates() |
| 746 | 746 |
| 747 def getYCoordinates(self): | 747 def getYCoordinates(self): |
| 748 return self.positions.getYCoordinates() | 748 return self.positions.getYCoordinates() |
| 749 | 749 |
| 750 def draw(self, options = '', withOrigin = False, timeStep = 1, **kwargs): | 750 def plot(self, options = '', withOrigin = False, timeStep = 1, **kwargs): |
| 751 self.positions.draw(options, withOrigin, timeStep, **kwargs) | 751 self.positions.plot(options, withOrigin, timeStep, **kwargs) |
| 752 | 752 |
| 753 def drawOnWorldImage(self, nPixelsPerUnitDistance, imageHeight, options = '', withOrigin = False, timeStep = 1, **kwargs): | 753 def plotOnWorldImage(self, nPixelsPerUnitDistance, imageHeight, options = '', withOrigin = False, timeStep = 1, **kwargs): |
| 754 self.positions.drawOnWorldImage(nPixelsPerUnitDistance, imageHeight, options, withOrigin, timeStep, **kwargs) | 754 self.positions.plotOnWorldImage(nPixelsPerUnitDistance, imageHeight, options, withOrigin, timeStep, **kwargs) |
| 755 | 755 |
| 756 def play(self, videoFilename, homography = None): | 756 def play(self, videoFilename, homography = None): |
| 757 cvutils.displayTrajectories(videoFilename, [self], homography, self.getFirstInstant(), self.getLastInstant()) | 757 cvutils.displayTrajectories(videoFilename, [self], homography, self.getFirstInstant(), self.getLastInstant()) |
| 758 | 758 |
| 759 def speedDiagnostics(self, framerate = 1., display = False): | 759 def speedDiagnostics(self, framerate = 1., display = False): |
| 763 coef = utils.linearRegression(range(len(speeds)), speeds) | 763 coef = utils.linearRegression(range(len(speeds)), speeds) |
| 764 print('min/5th perc speed: {} / {}\nspeed diff: {}\nspeed stdev: {}\nregression: {}'.format(min(speeds), scoreatpercentile(speeds, 5), speeds[-2]-speeds[1], std(speeds), coef[0])) | 764 print('min/5th perc speed: {} / {}\nspeed diff: {}\nspeed stdev: {}\nregression: {}'.format(min(speeds), scoreatpercentile(speeds, 5), speeds[-2]-speeds[1], std(speeds), coef[0])) |
| 765 if display: | 765 if display: |
| 766 from matplotlib.pyplot import figure, plot, axis | 766 from matplotlib.pyplot import figure, plot, axis |
| 767 figure(1) | 767 figure(1) |
| 768 self.draw() | 768 self.plot() |
| 769 axis('equal') | 769 axis('equal') |
| 770 figure(2) | 770 figure(2) |
| 771 plot(list(self.getTimeInterval()), speeds) | 771 plot(list(self.getTimeInterval()), speeds) |
| 772 | 772 |
| 773 @staticmethod | 773 @staticmethod |
| 842 def plotRoadUsers(objects, colors): | 842 def plotRoadUsers(objects, colors): |
| 843 '''Colors is a PlottingPropertyValues instance''' | 843 '''Colors is a PlottingPropertyValues instance''' |
| 844 from matplotlib.pyplot import figure, axis | 844 from matplotlib.pyplot import figure, axis |
| 845 figure() | 845 figure() |
| 846 for obj in objects: | 846 for obj in objects: |
| 847 obj.draw(colors.get(obj.userType)) | 847 obj.plot(colors.get(obj.userType)) |
| 848 axis('equal') | 848 axis('equal') |
| 849 | 849 |
| 850 | 850 |
| 851 if __name__ == "__main__": | 851 if __name__ == "__main__": |
| 852 import doctest | 852 import doctest |
