Mercurial > hg > nsaunier > traffic-intelligence
comparison c/Motion.cpp @ 136:0f790de9437e
renamed Feature to Motion files and added code to test blob db
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Thu, 18 Aug 2011 22:25:21 -0400 |
| parents | c/Feature.cpp@32d2722d4028 |
| children | 445e773c9be3 |
comparison
equal
deleted
inserted
replaced
| 135:32d2722d4028 | 136:0f790de9437e |
|---|---|
| 1 #include "Motion.hpp" | |
| 2 | |
| 3 #include "opencv2/core/core.hpp" | |
| 4 #include "opencv2/highgui/highgui.hpp" | |
| 5 | |
| 6 #include "src/TrajectoryDBAccessList.h" | |
| 7 | |
| 8 using namespace std; | |
| 9 using namespace cv; | |
| 10 | |
| 11 FeatureTrajectory::FeatureTrajectory(const int& frameNum, const cv::Point2f& p) { | |
| 12 addPoint(frameNum, p); | |
| 13 } | |
| 14 | |
| 15 bool FeatureTrajectory::largeDisplacement(const unsigned int& nDisplacements, const float& minTotalFeatureDisplacement) const { | |
| 16 bool result = true; | |
| 17 unsigned int nPositions = positions.size(); | |
| 18 if (nPositions > nDisplacements) { | |
| 19 float disp = 0; | |
| 20 for (int i=0; i<nDisplacements; i++) | |
| 21 disp += displacementDistances[nPositions-2-i]; | |
| 22 result = disp > minTotalFeatureDisplacement; | |
| 23 } | |
| 24 return result; | |
| 25 } | |
| 26 | |
| 27 void FeatureTrajectory::addPoint(const int& frameNum, const Point2f& p) { | |
| 28 positions.add(frameNum, p); | |
| 29 computeMotionData(frameNum); | |
| 30 } | |
| 31 | |
| 32 void FeatureTrajectory::write(TrajectoryDBAccess<Point2f>& trajectoryDB) const { | |
| 33 /// \todo save velocities | |
| 34 trajectoryDB.write(positions); | |
| 35 } | |
| 36 | |
| 37 #ifdef USE_OPENCV | |
| 38 /// \todo add option for anti-aliased drawing, thickness | |
| 39 void FeatureTrajectory::draw(Mat& img, const Scalar& color) const { | |
| 40 Point2f p1 = positions[0]; | |
| 41 for (unsigned int i=1; i<positions.size(); i++) { | |
| 42 Point2f p2 = positions[i]; | |
| 43 line(img, p1, p2, color, 1); | |
| 44 p1 = p2; | |
| 45 } | |
| 46 } | |
| 47 #endif | |
| 48 | |
| 49 // protected | |
| 50 | |
| 51 void FeatureTrajectory::computeMotionData(const int& frameNum) { | |
| 52 unsigned int nPositions = positions.size(); | |
| 53 if (nPositions >= 3) { | |
| 54 Point2f displacement = positions[nPositions-1] - positions[nPositions-2]; | |
| 55 if (nPositions == 2) // duplicate first displacement so that positions and velocities have the same length | |
| 56 velocities.add(frameNum-1, displacement); | |
| 57 velocities.add(frameNum, displacement); | |
| 58 float dist = norm(displacement); | |
| 59 displacementDistances.push_back(dist); | |
| 60 } | |
| 61 } |
