annotate c/Motion.cpp @ 138:c1b260b48d2a

corrected initialization bugs and feature shortening before saving
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 19 Aug 2011 12:15:23 -0400
parents 445e773c9be3
children 47329bd16cc0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
136
0f790de9437e renamed Feature to Motion files and added code to test blob db
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 135
diff changeset
1 #include "Motion.hpp"
129
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
2
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
3 #include "opencv2/core/core.hpp"
132
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 129
diff changeset
4 #include "opencv2/highgui/highgui.hpp"
129
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
5
133
63dd4355b6d1 saving of feature positions in sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 132
diff changeset
6 #include "src/TrajectoryDBAccessList.h"
63dd4355b6d1 saving of feature positions in sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 132
diff changeset
7
129
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
8 using namespace std;
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
9 using namespace cv;
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
10
132
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 129
diff changeset
11 FeatureTrajectory::FeatureTrajectory(const int& frameNum, const cv::Point2f& p) {
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 129
diff changeset
12 addPoint(frameNum, p);
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 129
diff changeset
13 }
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 129
diff changeset
14
138
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
15 bool FeatureTrajectory::smallDisplacement(const unsigned int& nDisplacements, const float& minTotalFeatureDisplacement) const {
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
16 bool result = false;
135
32d2722d4028 added constraint on minimum displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 133
diff changeset
17 unsigned int nPositions = positions.size();
32d2722d4028 added constraint on minimum displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 133
diff changeset
18 if (nPositions > nDisplacements) {
32d2722d4028 added constraint on minimum displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 133
diff changeset
19 float disp = 0;
137
445e773c9be3 created the parameter structure to parse parameters (bug remaining)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 136
diff changeset
20 for (unsigned int i=0; i<nDisplacements; i++)
135
32d2722d4028 added constraint on minimum displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 133
diff changeset
21 disp += displacementDistances[nPositions-2-i];
138
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
22 result = disp < minTotalFeatureDisplacement;
135
32d2722d4028 added constraint on minimum displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 133
diff changeset
23 }
32d2722d4028 added constraint on minimum displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 133
diff changeset
24 return result;
32d2722d4028 added constraint on minimum displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 133
diff changeset
25 }
32d2722d4028 added constraint on minimum displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 133
diff changeset
26
129
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
27 void FeatureTrajectory::addPoint(const int& frameNum, const Point2f& p) {
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
28 positions.add(frameNum, p);
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
29 computeMotionData(frameNum);
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
30 }
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
31
138
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
32 void FeatureTrajectory::shorten(void) {
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
33 positions.pop_back();
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
34 velocities.pop_back();
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
35 displacementDistances.pop_back();
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
36 }
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
37
133
63dd4355b6d1 saving of feature positions in sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 132
diff changeset
38 void FeatureTrajectory::write(TrajectoryDBAccess<Point2f>& trajectoryDB) const {
63dd4355b6d1 saving of feature positions in sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 132
diff changeset
39 /// \todo save velocities
63dd4355b6d1 saving of feature positions in sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 132
diff changeset
40 trajectoryDB.write(positions);
63dd4355b6d1 saving of feature positions in sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 132
diff changeset
41 }
63dd4355b6d1 saving of feature positions in sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 132
diff changeset
42
132
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 129
diff changeset
43 #ifdef USE_OPENCV
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 129
diff changeset
44 /// \todo add option for anti-aliased drawing, thickness
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 129
diff changeset
45 void FeatureTrajectory::draw(Mat& img, const Scalar& color) const {
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 129
diff changeset
46 Point2f p1 = positions[0];
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 129
diff changeset
47 for (unsigned int i=1; i<positions.size(); i++) {
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 129
diff changeset
48 Point2f p2 = positions[i];
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 129
diff changeset
49 line(img, p1, p2, color, 1);
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 129
diff changeset
50 p1 = p2;
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 129
diff changeset
51 }
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 129
diff changeset
52 }
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 129
diff changeset
53 #endif
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 129
diff changeset
54
129
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
55 // protected
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
56
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
57 void FeatureTrajectory::computeMotionData(const int& frameNum) {
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
58 unsigned int nPositions = positions.size();
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
59 if (nPositions >= 3) {
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
60 Point2f displacement = positions[nPositions-1] - positions[nPositions-2];
135
32d2722d4028 added constraint on minimum displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 133
diff changeset
61 if (nPositions == 2) // duplicate first displacement so that positions and velocities have the same length
32d2722d4028 added constraint on minimum displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 133
diff changeset
62 velocities.add(frameNum-1, displacement);
129
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
63 velocities.add(frameNum, displacement);
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
64 float dist = norm(displacement);
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
65 displacementDistances.push_back(dist);
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
66 }
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
67 }