Mercurial > hg > nsaunier > traffic-intelligence
comparison c/Motion.cpp @ 176:9323427aa0a3
changed positions and velocities to shared pointers and renamed methods
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Thu, 27 Oct 2011 13:56:46 -0400 |
| parents | cde87a07eb58 |
| children | ae2286b1a3fd |
comparison
equal
deleted
inserted
replaced
| 175:a234a5e818f3 | 176:9323427aa0a3 |
|---|---|
| 11 | 11 |
| 12 /******************** FeatureTrajectory ********************/ | 12 /******************** FeatureTrajectory ********************/ |
| 13 | 13 |
| 14 FeatureTrajectory::FeatureTrajectory(const int& frameNum, const cv::Point2f& p, const Mat& homography) | 14 FeatureTrajectory::FeatureTrajectory(const int& frameNum, const cv::Point2f& p, const Mat& homography) |
| 15 : lost(false) { | 15 : lost(false) { |
| 16 positions = TrajectoryPoint2fPtr(new TrajectoryPoint2f()); | |
| 17 velocities = TrajectoryPoint2fPtr(new TrajectoryPoint2f()); | |
| 16 addPoint(frameNum, p, homography); | 18 addPoint(frameNum, p, homography); |
| 17 } | 19 } |
| 18 | 20 |
| 19 bool FeatureTrajectory::smallDisplacement(const unsigned int& nDisplacements, const float& minTotalFeatureDisplacement) const { | 21 bool FeatureTrajectory::isDisplacementSmall(const unsigned int& nDisplacements, const float& minTotalFeatureDisplacement) const { |
| 20 bool result = false; | 22 bool result = false; |
| 21 unsigned int nPositions = positions.size(); | 23 unsigned int nPositions = positions->size(); |
| 22 if (nPositions > nDisplacements) { | 24 if (nPositions > nDisplacements) { |
| 23 float disp = 0; | 25 float disp = 0; |
| 24 for (unsigned int i=0; i<nDisplacements; i++) | 26 for (unsigned int i=0; i<nDisplacements; i++) |
| 25 disp += displacementDistances[nPositions-2-i]; | 27 disp += displacementDistances[nPositions-2-i]; |
| 26 result = disp < minTotalFeatureDisplacement; | 28 result = disp < minTotalFeatureDisplacement; |
| 27 } | 29 } |
| 28 return result; | 30 return result; |
| 29 } | 31 } |
| 30 | 32 |
| 31 bool FeatureTrajectory::motionSmooth(const int& accelerationBound, const int& deviationBound) const { | 33 bool FeatureTrajectory::isMotionSmooth(const int& accelerationBound, const int& deviationBound) const { |
| 32 bool result = true; | 34 bool result = true; |
| 33 unsigned int nPositions = positions.size(); | 35 unsigned int nPositions = positions->size(); |
| 34 if (nPositions >= 3) { | 36 if (nPositions >= 3) { |
| 35 float ratio; | 37 float ratio; |
| 36 if (displacementDistances[nPositions-2] > displacementDistances[nPositions-3]) | 38 if (displacementDistances[nPositions-2] > displacementDistances[nPositions-3]) |
| 37 ratio = displacementDistances[nPositions-2] / displacementDistances[nPositions-3]; | 39 ratio = displacementDistances[nPositions-2] / displacementDistances[nPositions-3]; |
| 38 else | 40 else |
| 39 ratio = displacementDistances[nPositions-3] / displacementDistances[nPositions-2]; | 41 ratio = displacementDistances[nPositions-3] / displacementDistances[nPositions-2]; |
| 40 | 42 |
| 41 float cosine = scalarProduct(velocities[nPositions-3],velocities[nPositions-2]) / (displacementDistances[nPositions-3] * displacementDistances[nPositions-2]); | 43 float cosine = scalarProduct((*velocities)[nPositions-3],(*velocities)[nPositions-2]) / (displacementDistances[nPositions-3] * displacementDistances[nPositions-2]); |
| 42 | 44 |
| 43 result &= (ratio < accelerationBound) & (cosine > deviationBound); | 45 result &= (ratio < accelerationBound) & (cosine > deviationBound); |
| 44 } | 46 } |
| 45 return result; | 47 return result; |
| 46 } | 48 } |
| 47 | 49 |
| 48 void FeatureTrajectory::addPoint(const int& frameNum, const Point2f& p, const Mat& homography) { | 50 void FeatureTrajectory::addPoint(const int& frameNum, const Point2f& p, const Mat& homography) { |
| 49 Point2f pp = p; | 51 Point2f pp = p; |
| 50 if (!homography.empty()) | 52 if (!homography.empty()) |
| 51 pp = project(p, homography); | 53 pp = project(p, homography); |
| 52 positions.add(frameNum, pp); | 54 positions->add(frameNum, pp); |
| 53 computeMotionData(frameNum); | 55 computeMotionData(frameNum); |
| 54 assert(positions.size() == displacementDistances.size()+1); | 56 assert(positions.size() == displacementDistances.size()+1); |
| 55 assert(positions.size() == velocities.size()+1); | 57 assert(positions.size() == velocities.size()+1); |
| 56 } | 58 } |
| 57 | 59 |
| 58 void FeatureTrajectory::shorten(void) { | 60 void FeatureTrajectory::shorten(void) { |
| 59 positions.pop_back(); | 61 positions->pop_back(); |
| 60 velocities.pop_back(); | 62 velocities->pop_back(); |
| 61 displacementDistances.pop_back(); | 63 displacementDistances.pop_back(); |
| 62 } | 64 } |
| 63 | 65 |
| 64 void FeatureTrajectory::write(TrajectoryDBAccess<Point2f>& trajectoryDB, const string& positionsTableName, const string& velocitiesTableName) const { | 66 void FeatureTrajectory::write(TrajectoryDBAccess<Point2f>& trajectoryDB, const string& positionsTableName, const string& velocitiesTableName) const { |
| 65 trajectoryDB.write(positions, positionsTableName); | 67 trajectoryDB.write(*positions, positionsTableName); |
| 66 trajectoryDB.write(velocities, velocitiesTableName); | 68 trajectoryDB.write(*velocities, velocitiesTableName); |
| 67 } | 69 } |
| 68 | 70 |
| 69 #ifdef USE_OPENCV | 71 #ifdef USE_OPENCV |
| 70 /// \todo add option for anti-aliased drawing, thickness | 72 /// \todo add option for anti-aliased drawing, thickness |
| 71 void FeatureTrajectory::draw(Mat& img, const Mat& homography, const Scalar& color) const { | 73 void FeatureTrajectory::draw(Mat& img, const Mat& homography, const Scalar& color) const { |
| 72 Point2f p1, p2; | 74 Point2f p1, p2; |
| 73 if (!homography.empty()) | 75 if (!homography.empty()) |
| 74 p1 = project(positions[0], homography); | 76 p1 = project((*positions)[0], homography); |
| 75 else | 77 else |
| 76 p1 = positions[0]; | 78 p1 = (*positions)[0]; |
| 77 for (unsigned int i=1; i<positions.size(); i++) { | 79 for (unsigned int i=1; i<positions->size(); i++) { |
| 78 if (!homography.empty()) | 80 if (!homography.empty()) |
| 79 p2 = project(positions[i], homography); | 81 p2 = project((*positions)[i], homography); |
| 80 else | 82 else |
| 81 p2 = positions[i]; | 83 p2 = (*positions)[i]; |
| 82 line(img, p1, p2, color, 1); | 84 line(img, p1, p2, color, 1); |
| 83 p1 = p2; | 85 p1 = p2; |
| 84 } | 86 } |
| 85 } | 87 } |
| 86 #endif | 88 #endif |
| 87 | 89 |
| 88 // protected | 90 // protected |
| 89 | 91 |
| 90 void FeatureTrajectory::computeMotionData(const int& frameNum) { | 92 void FeatureTrajectory::computeMotionData(const int& frameNum) { |
| 91 unsigned int nPositions = positions.size(); | 93 unsigned int nPositions = positions->size(); |
| 92 if (nPositions >= 2) { | 94 if (nPositions >= 2) { |
| 93 Point2f displacement = positions[nPositions-1] - positions[nPositions-2]; | 95 Point2f displacement = (*positions)[nPositions-1] - (*positions)[nPositions-2]; |
| 94 //if (nPositions == 2) // duplicate first displacement so that positions and velocities have the same length | 96 //if (nPositions == 2) // duplicate first displacement so that positions and velocities have the same length |
| 95 //velocities.add(frameNum-1, displacement); | 97 //velocities.add(frameNum-1, displacement); |
| 96 velocities.add(frameNum, displacement); | 98 velocities->add(frameNum, displacement); |
| 97 float dist = norm(displacement); | 99 float dist = norm(displacement); |
| 98 displacementDistances.push_back(dist); | 100 displacementDistances.push_back(dist); |
| 99 } | 101 } |
| 100 } | 102 } |
| 101 | 103 |
