annotate c/Motion.cpp @ 191:0e60a306d324

added basic code to identify features and save them (crash)
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 08 Dec 2011 18:32:35 -0500
parents 36968a63efe1
children 38974d27dd2d
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"
139
47329bd16cc0 cleaned code, added condition on smooth displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 138
diff changeset
2 #include "cvutils.hpp"
129
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
3
188
1435965d8181 work on connected components
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 187
diff changeset
4 #include "src/TrajectoryDBAccessList.h"
1435965d8181 work on connected components
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 187
diff changeset
5
129
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
6 #include "opencv2/core/core.hpp"
132
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 129
diff changeset
7 #include "opencv2/highgui/highgui.hpp"
129
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
8
188
1435965d8181 work on connected components
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 187
diff changeset
9 #include <boost/graph/connected_components.hpp>
191
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
10 #include <boost/property_map/property_map.hpp>
188
1435965d8181 work on connected components
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 187
diff changeset
11
1435965d8181 work on connected components
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 187
diff changeset
12 #include <iostream>
1435965d8181 work on connected components
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 187
diff changeset
13 #include <vector>
191
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
14 #include <map>
188
1435965d8181 work on connected components
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 187
diff changeset
15 #include <algorithm>
1435965d8181 work on connected components
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 187
diff changeset
16 #include <utility>
133
63dd4355b6d1 saving of feature positions in sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 132
diff changeset
17
129
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
18 using namespace std;
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
19 using namespace cv;
190
36968a63efe1 Got the connected_components to finally work using a vecS for the vertex list in the adjacency list.
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 189
diff changeset
20 using namespace boost;
129
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
21
163
cde87a07eb58 added graph structures
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 147
diff changeset
22 /******************** FeatureTrajectory ********************/
cde87a07eb58 added graph structures
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 147
diff changeset
23
cde87a07eb58 added graph structures
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 147
diff changeset
24 FeatureTrajectory::FeatureTrajectory(const int& frameNum, const cv::Point2f& p, const Mat& homography)
cde87a07eb58 added graph structures
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 147
diff changeset
25 : lost(false) {
176
9323427aa0a3 changed positions and velocities to shared pointers and renamed methods
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 163
diff changeset
26 positions = TrajectoryPoint2fPtr(new TrajectoryPoint2f());
9323427aa0a3 changed positions and velocities to shared pointers and renamed methods
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 163
diff changeset
27 velocities = TrajectoryPoint2fPtr(new TrajectoryPoint2f());
147
0089fb29cd26 added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 142
diff changeset
28 addPoint(frameNum, p, homography);
132
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 129
diff changeset
29 }
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 129
diff changeset
30
177
ae2286b1a3fd added loading FeatureTrajectory from database, printing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 176
diff changeset
31 FeatureTrajectory::FeatureTrajectory(TrajectoryPoint2fPtr& _positions, TrajectoryPoint2fPtr& _velocities)
ae2286b1a3fd added loading FeatureTrajectory from database, printing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 176
diff changeset
32 : lost(false) {
ae2286b1a3fd added loading FeatureTrajectory from database, printing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 176
diff changeset
33 positions = _positions;
ae2286b1a3fd added loading FeatureTrajectory from database, printing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 176
diff changeset
34 velocities = _velocities;
ae2286b1a3fd added loading FeatureTrajectory from database, printing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 176
diff changeset
35 }
ae2286b1a3fd added loading FeatureTrajectory from database, printing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 176
diff changeset
36
ae2286b1a3fd added loading FeatureTrajectory from database, printing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 176
diff changeset
37 FeatureTrajectory::FeatureTrajectory(const int& id, TrajectoryDBAccessList<Point2f>& trajectoryDB, const string& positionsTableName, const string& velocitiesTableName) {
ae2286b1a3fd added loading FeatureTrajectory from database, printing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 176
diff changeset
38 bool success = trajectoryDB.read(positions, id, positionsTableName);
ae2286b1a3fd added loading FeatureTrajectory from database, printing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 176
diff changeset
39 if (!success)
ae2286b1a3fd added loading FeatureTrajectory from database, printing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 176
diff changeset
40 cout << "problem loading positions" << endl;
ae2286b1a3fd added loading FeatureTrajectory from database, printing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 176
diff changeset
41 success = trajectoryDB.read(velocities, id, velocitiesTableName);
ae2286b1a3fd added loading FeatureTrajectory from database, printing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 176
diff changeset
42 if (!success)
ae2286b1a3fd added loading FeatureTrajectory from database, printing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 176
diff changeset
43 cout << "problem loading velocities" << endl;
179
4f10e97cb677 added getting first and last instant for each feature
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 177
diff changeset
44 // take advantage to request first and last instant from database
4f10e97cb677 added getting first and last instant for each feature
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 177
diff changeset
45 trajectoryDB.timeInterval(firstInstant, lastInstant, id);
177
ae2286b1a3fd added loading FeatureTrajectory from database, printing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 176
diff changeset
46 }
ae2286b1a3fd added loading FeatureTrajectory from database, printing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 176
diff changeset
47
176
9323427aa0a3 changed positions and velocities to shared pointers and renamed methods
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 163
diff changeset
48 bool FeatureTrajectory::isDisplacementSmall(const unsigned int& nDisplacements, const float& minTotalFeatureDisplacement) const {
138
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
49 bool result = false;
176
9323427aa0a3 changed positions and velocities to shared pointers and renamed methods
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 163
diff changeset
50 unsigned int nPositions = positions->size();
135
32d2722d4028 added constraint on minimum displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 133
diff changeset
51 if (nPositions > nDisplacements) {
32d2722d4028 added constraint on minimum displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 133
diff changeset
52 float disp = 0;
137
445e773c9be3 created the parameter structure to parse parameters (bug remaining)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 136
diff changeset
53 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
54 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
55 result = disp < minTotalFeatureDisplacement;
135
32d2722d4028 added constraint on minimum displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 133
diff changeset
56 }
32d2722d4028 added constraint on minimum displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 133
diff changeset
57 return result;
32d2722d4028 added constraint on minimum displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 133
diff changeset
58 }
32d2722d4028 added constraint on minimum displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 133
diff changeset
59
176
9323427aa0a3 changed positions and velocities to shared pointers and renamed methods
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 163
diff changeset
60 bool FeatureTrajectory::isMotionSmooth(const int& accelerationBound, const int& deviationBound) const {
139
47329bd16cc0 cleaned code, added condition on smooth displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 138
diff changeset
61 bool result = true;
176
9323427aa0a3 changed positions and velocities to shared pointers and renamed methods
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 163
diff changeset
62 unsigned int nPositions = positions->size();
139
47329bd16cc0 cleaned code, added condition on smooth displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 138
diff changeset
63 if (nPositions >= 3) {
47329bd16cc0 cleaned code, added condition on smooth displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 138
diff changeset
64 float ratio;
47329bd16cc0 cleaned code, added condition on smooth displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 138
diff changeset
65 if (displacementDistances[nPositions-2] > displacementDistances[nPositions-3])
47329bd16cc0 cleaned code, added condition on smooth displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 138
diff changeset
66 ratio = displacementDistances[nPositions-2] / displacementDistances[nPositions-3];
47329bd16cc0 cleaned code, added condition on smooth displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 138
diff changeset
67 else
47329bd16cc0 cleaned code, added condition on smooth displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 138
diff changeset
68 ratio = displacementDistances[nPositions-3] / displacementDistances[nPositions-2];
47329bd16cc0 cleaned code, added condition on smooth displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 138
diff changeset
69
176
9323427aa0a3 changed positions and velocities to shared pointers and renamed methods
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 163
diff changeset
70 float cosine = scalarProduct((*velocities)[nPositions-3],(*velocities)[nPositions-2]) / (displacementDistances[nPositions-3] * displacementDistances[nPositions-2]);
139
47329bd16cc0 cleaned code, added condition on smooth displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 138
diff changeset
71
47329bd16cc0 cleaned code, added condition on smooth displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 138
diff changeset
72 result &= (ratio < accelerationBound) & (cosine > deviationBound);
47329bd16cc0 cleaned code, added condition on smooth displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 138
diff changeset
73 }
47329bd16cc0 cleaned code, added condition on smooth displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 138
diff changeset
74 return result;
47329bd16cc0 cleaned code, added condition on smooth displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 138
diff changeset
75 }
47329bd16cc0 cleaned code, added condition on smooth displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 138
diff changeset
76
186
6c48283a78ca work on feature similarity, issue with getting point
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 180
diff changeset
77 bool FeatureTrajectory::minMaxSimilarity(const FeatureTrajectory& ft, const int& firstInstant, const int& lastInstant, float connectionDistance, float segmentationDistance) {
187
aa1061fb9695 using minmaxsimilarity
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 186
diff changeset
78 float minDistance = norm(positions->getPointAtInstant(firstInstant)-ft.positions->getPointAtInstant(firstInstant));
186
6c48283a78ca work on feature similarity, issue with getting point
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 180
diff changeset
79 float maxDistance = minDistance;
190
36968a63efe1 Got the connected_components to finally work using a vecS for the vertex list in the adjacency list.
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 189
diff changeset
80 bool connected = (minDistance <= connectionDistance);
186
6c48283a78ca work on feature similarity, issue with getting point
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 180
diff changeset
81 int t=firstInstant+1;
6c48283a78ca work on feature similarity, issue with getting point
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 180
diff changeset
82 while (t <= lastInstant && connected) {
187
aa1061fb9695 using minmaxsimilarity
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 186
diff changeset
83 float distance = norm(positions->getPointAtInstant(t)-ft.positions->getPointAtInstant(t));
186
6c48283a78ca work on feature similarity, issue with getting point
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 180
diff changeset
84 if (distance < minDistance)
6c48283a78ca work on feature similarity, issue with getting point
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 180
diff changeset
85 minDistance = distance;
6c48283a78ca work on feature similarity, issue with getting point
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 180
diff changeset
86 else if (distance > maxDistance)
6c48283a78ca work on feature similarity, issue with getting point
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 180
diff changeset
87 maxDistance = distance;
6c48283a78ca work on feature similarity, issue with getting point
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 180
diff changeset
88 connected = connected && (maxDistance-minDistance < segmentationDistance);
6c48283a78ca work on feature similarity, issue with getting point
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 180
diff changeset
89 t++;
6c48283a78ca work on feature similarity, issue with getting point
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 180
diff changeset
90 }
6c48283a78ca work on feature similarity, issue with getting point
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 180
diff changeset
91
6c48283a78ca work on feature similarity, issue with getting point
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 180
diff changeset
92 return connected;
6c48283a78ca work on feature similarity, issue with getting point
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 180
diff changeset
93 }
6c48283a78ca work on feature similarity, issue with getting point
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 180
diff changeset
94
147
0089fb29cd26 added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 142
diff changeset
95 void FeatureTrajectory::addPoint(const int& frameNum, const Point2f& p, const Mat& homography) {
0089fb29cd26 added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 142
diff changeset
96 Point2f pp = p;
0089fb29cd26 added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 142
diff changeset
97 if (!homography.empty())
0089fb29cd26 added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 142
diff changeset
98 pp = project(p, homography);
176
9323427aa0a3 changed positions and velocities to shared pointers and renamed methods
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 163
diff changeset
99 positions->add(frameNum, pp);
129
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
100 computeMotionData(frameNum);
139
47329bd16cc0 cleaned code, added condition on smooth displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 138
diff changeset
101 assert(positions.size() == displacementDistances.size()+1);
47329bd16cc0 cleaned code, added condition on smooth displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 138
diff changeset
102 assert(positions.size() == velocities.size()+1);
129
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
103 }
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
104
138
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
105 void FeatureTrajectory::shorten(void) {
176
9323427aa0a3 changed positions and velocities to shared pointers and renamed methods
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 163
diff changeset
106 positions->pop_back();
9323427aa0a3 changed positions and velocities to shared pointers and renamed methods
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 163
diff changeset
107 velocities->pop_back();
138
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
108 displacementDistances.pop_back();
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
109 }
c1b260b48d2a corrected initialization bugs and feature shortening before saving
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 137
diff changeset
110
142
a3532db00c28 added code to write velocities
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 139
diff changeset
111 void FeatureTrajectory::write(TrajectoryDBAccess<Point2f>& trajectoryDB, const string& positionsTableName, const string& velocitiesTableName) const {
176
9323427aa0a3 changed positions and velocities to shared pointers and renamed methods
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 163
diff changeset
112 trajectoryDB.write(*positions, positionsTableName);
9323427aa0a3 changed positions and velocities to shared pointers and renamed methods
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 163
diff changeset
113 trajectoryDB.write(*velocities, velocitiesTableName);
133
63dd4355b6d1 saving of feature positions in sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 132
diff changeset
114 }
63dd4355b6d1 saving of feature positions in sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 132
diff changeset
115
132
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 129
diff changeset
116 #ifdef USE_OPENCV
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 129
diff changeset
117 /// \todo add option for anti-aliased drawing, thickness
147
0089fb29cd26 added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 142
diff changeset
118 void FeatureTrajectory::draw(Mat& img, const Mat& homography, const Scalar& color) const {
0089fb29cd26 added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 142
diff changeset
119 Point2f p1, p2;
0089fb29cd26 added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 142
diff changeset
120 if (!homography.empty())
176
9323427aa0a3 changed positions and velocities to shared pointers and renamed methods
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 163
diff changeset
121 p1 = project((*positions)[0], homography);
147
0089fb29cd26 added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 142
diff changeset
122 else
176
9323427aa0a3 changed positions and velocities to shared pointers and renamed methods
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 163
diff changeset
123 p1 = (*positions)[0];
9323427aa0a3 changed positions and velocities to shared pointers and renamed methods
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 163
diff changeset
124 for (unsigned int i=1; i<positions->size(); i++) {
147
0089fb29cd26 added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 142
diff changeset
125 if (!homography.empty())
176
9323427aa0a3 changed positions and velocities to shared pointers and renamed methods
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 163
diff changeset
126 p2 = project((*positions)[i], homography);
147
0089fb29cd26 added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 142
diff changeset
127 else
176
9323427aa0a3 changed positions and velocities to shared pointers and renamed methods
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 163
diff changeset
128 p2 = (*positions)[i];
132
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 129
diff changeset
129 line(img, p1, p2, color, 1);
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 129
diff changeset
130 p1 = p2;
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 129
diff changeset
131 }
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 129
diff changeset
132 }
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 129
diff changeset
133 #endif
45c64e68053c added drawing function for features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 129
diff changeset
134
129
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
135 // protected
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
136
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
137 void FeatureTrajectory::computeMotionData(const int& frameNum) {
176
9323427aa0a3 changed positions and velocities to shared pointers and renamed methods
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 163
diff changeset
138 unsigned int nPositions = positions->size();
139
47329bd16cc0 cleaned code, added condition on smooth displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 138
diff changeset
139 if (nPositions >= 2) {
176
9323427aa0a3 changed positions and velocities to shared pointers and renamed methods
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 163
diff changeset
140 Point2f displacement = (*positions)[nPositions-1] - (*positions)[nPositions-2];
139
47329bd16cc0 cleaned code, added condition on smooth displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 138
diff changeset
141 //if (nPositions == 2) // duplicate first displacement so that positions and velocities have the same length
47329bd16cc0 cleaned code, added condition on smooth displacement
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 138
diff changeset
142 //velocities.add(frameNum-1, displacement);
176
9323427aa0a3 changed positions and velocities to shared pointers and renamed methods
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 163
diff changeset
143 velocities->add(frameNum, displacement);
129
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
144 float dist = norm(displacement);
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
145 displacementDistances.push_back(dist);
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
146 }
4742b2b6d851 created basic feature saving code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
147 }
163
cde87a07eb58 added graph structures
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 147
diff changeset
148
cde87a07eb58 added graph structures
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 147
diff changeset
149 /******************** FeatureGraph ********************/
179
4f10e97cb677 added getting first and last instant for each feature
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 177
diff changeset
150
180
3a4eef37384f method to add features and vertices to graph
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 179
diff changeset
151 void FeatureGraph::addFeature(const FeatureTrajectoryPtr& ft) {
191
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
152 vertex_descriptor newVertex = add_vertex(graph);
180
3a4eef37384f method to add features and vertices to graph
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 179
diff changeset
153 graph[newVertex].feature = ft;
190
36968a63efe1 Got the connected_components to finally work using a vecS for the vertex list in the adjacency list.
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 189
diff changeset
154 for (graph_traits<UndirectedGraph>::vertex_iterator vi = vertices(graph).first;
180
3a4eef37384f method to add features and vertices to graph
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 179
diff changeset
155 vi!=vertices(graph).second; ++vi) { // vi pointer to vertex_descriptor
3a4eef37384f method to add features and vertices to graph
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 179
diff changeset
156 FeatureTrajectoryPtr ft2 = graph[*vi].feature;
187
aa1061fb9695 using minmaxsimilarity
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 186
diff changeset
157 int lastInstant = static_cast<int>(min(ft->getLastInstant(), ft2->getLastInstant()));
aa1061fb9695 using minmaxsimilarity
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 186
diff changeset
158 int firstInstant = static_cast<int>(max(ft->getFirstInstant(), ft2->getFirstInstant()));
aa1061fb9695 using minmaxsimilarity
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 186
diff changeset
159 if (lastInstant-firstInstant > static_cast<int>(minFeatureTime)) { // equivalent to lastInstant-firstInstant+1 >= minFeatureTime
186
6c48283a78ca work on feature similarity, issue with getting point
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 180
diff changeset
160 if (ft->minMaxSimilarity(*ft2, firstInstant, lastInstant, connectionDistance, segmentationDistance)) {
6c48283a78ca work on feature similarity, issue with getting point
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 180
diff changeset
161 UndirectedGraph::edge_descriptor e;
6c48283a78ca work on feature similarity, issue with getting point
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 180
diff changeset
162 bool unused;
6c48283a78ca work on feature similarity, issue with getting point
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 180
diff changeset
163 tie(e, unused) = add_edge(newVertex, *vi, graph);
189
1116f0a1ff31 work on connected components (does not compile)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 188
diff changeset
164 // no need to add measures to graph[e] (edge properties)
186
6c48283a78ca work on feature similarity, issue with getting point
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 180
diff changeset
165 }
180
3a4eef37384f method to add features and vertices to graph
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 179
diff changeset
166 }
3a4eef37384f method to add features and vertices to graph
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 179
diff changeset
167 }
3a4eef37384f method to add features and vertices to graph
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 179
diff changeset
168 }
3a4eef37384f method to add features and vertices to graph
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 179
diff changeset
169
191
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
170 vector<vector<FeatureGraph::vertex_descriptor> > FeatureGraph::connectedComponents(const int& lastInstant) {
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
171 int nVertices = num_vertices(graph);
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
172 vector<vertex_descriptor> components(nVertices);
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
173 // map<UndirectedGraph::vertex_descriptor, graph_traits<UndirectedGraph>::vertices_size_type> vertex2component;
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
174 // associative_property_map< map<UndirectedGraph::vertex_descriptor, graph_traits<UndirectedGraph>::vertices_size_type> > components(vertex2component);
188
1435965d8181 work on connected components
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 187
diff changeset
175
190
36968a63efe1 Got the connected_components to finally work using a vecS for the vertex list in the adjacency list.
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 189
diff changeset
176 int num = connected_components(graph, &components[0]);
36968a63efe1 Got the connected_components to finally work using a vecS for the vertex list in the adjacency list.
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 189
diff changeset
177 cout << "Total number of components: " << num << endl;
36968a63efe1 Got the connected_components to finally work using a vecS for the vertex list in the adjacency list.
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 189
diff changeset
178
191
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
179 vector<unsigned int> lastInstants(num, 0);
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
180 vector<vector<vertex_descriptor> > tmpobjects(num), objects;
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
181
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
182 for (int i = 0; i < nVertices; ++i) {
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
183 cout << "Vertex " << i <<" is in component " << components[i] << endl;// "(last " << graph[i].feature->getLastInstant() << " " << lastInstants[components[i]] << " " << (lastInstants[components[i]] < graph[i].feature->getLastInstant()) << ")" << endl;
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
184 if (lastInstants[components[i]] < graph[i].feature->getLastInstant())
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
185 lastInstants[components[i]] = graph[i].feature->getLastInstant();
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
186 tmpobjects[components[i]].push_back(i);
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
187 }
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
188
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
189 for (int i = 0; i < num; ++i) {
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
190 cout << i << " " << lastInstants[i] << endl;
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
191 if (static_cast<int>(lastInstants[i]) < lastInstant)
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
192 objects.push_back(tmpobjects[i]);
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
193 }
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
194
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
195 return objects;
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
196 }
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
197
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
198 vector<vector<unsigned int> > FeatureGraph::getFeatureGroups(const vector<vector<FeatureGraph::vertex_descriptor> >& objectHypotheses) {
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
199 vector<vector<unsigned int> > featureGroups;
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
200
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
201 for (unsigned int i=0; i<objectHypotheses.size(); ++i) {
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
202 // check that there is on average at least minNFeaturesPerGroup features at each frame in the group
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
203 float totalFeatureTime=0;
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
204 for (unsigned int j=0; j<objectHypotheses.size(); ++j)
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
205 totalFeatureTime += static_cast<float>(graph[objectHypotheses[i][j]].feature->length());
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
206
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
207 if (totalFeatureTime/static_cast<float>(objectHypotheses.size()) > minNFeaturesPerGroup) {
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
208 cout << "save group" << endl;
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
209 featureGroups.push_back(vector<unsigned int>());
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
210 for (unsigned int j=0; j<objectHypotheses.size(); ++j)
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
211 featureGroups.back().push_back(graph[objectHypotheses[i][j]].feature->getId());
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
212 }
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
213 // remove vertices: todo using listS
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
214 }
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
215
0e60a306d324 added basic code to identify features and save them (crash)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 190
diff changeset
216 return featureGroups;
188
1435965d8181 work on connected components
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 187
diff changeset
217 }
1435965d8181 work on connected components
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 187
diff changeset
218
180
3a4eef37384f method to add features and vertices to graph
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 179
diff changeset
219 string FeatureGraph::informationString(void) {
3a4eef37384f method to add features and vertices to graph
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 179
diff changeset
220 stringstream ss;
3a4eef37384f method to add features and vertices to graph
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 179
diff changeset
221 ss << num_vertices(graph) << " vertices, " << num_edges(graph) << " edges";
3a4eef37384f method to add features and vertices to graph
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 179
diff changeset
222 return ss.str();
3a4eef37384f method to add features and vertices to graph
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 179
diff changeset
223 }