Mercurial Hosting > traffic-intelligence
comparison c/Motion.cpp @ 614:5e09583275a4
Merged Nicolas/trafficintelligence into default
author | Mohamed Gomaa <eng.m.gom3a@gmail.com> |
---|---|
date | Fri, 05 Dec 2014 12:13:53 -0500 |
parents | f86f5f25730a |
children | 045d05cef9d0 |
comparison
equal
deleted
inserted
replaced
598:11f96bd08552 | 614:5e09583275a4 |
---|---|
49 unsigned int nPositions = positions->size(); | 49 unsigned int nPositions = positions->size(); |
50 if (nPositions > nDisplacements) { | 50 if (nPositions > nDisplacements) { |
51 float disp = 0; | 51 float disp = 0; |
52 for (unsigned int i=0; i<nDisplacements; i++) | 52 for (unsigned int i=0; i<nDisplacements; i++) |
53 disp += displacementDistances[nPositions-2-i]; | 53 disp += displacementDistances[nPositions-2-i]; |
54 result = disp < minTotalFeatureDisplacement; | 54 result = disp <= minTotalFeatureDisplacement; |
55 } | 55 } |
56 return result; | 56 return result; |
57 } | 57 } |
58 | 58 |
59 bool FeatureTrajectory::isMotionSmooth(const int& accelerationBound, const int& deviationBound) const { | 59 bool FeatureTrajectory::isMotionSmooth(const int& accelerationBound, const int& deviationBound) const { |
64 if (displacementDistances[nPositions-2] > displacementDistances[nPositions-3]) | 64 if (displacementDistances[nPositions-2] > displacementDistances[nPositions-3]) |
65 ratio = displacementDistances[nPositions-2] / displacementDistances[nPositions-3]; | 65 ratio = displacementDistances[nPositions-2] / displacementDistances[nPositions-3]; |
66 else | 66 else |
67 ratio = displacementDistances[nPositions-3] / displacementDistances[nPositions-2]; | 67 ratio = displacementDistances[nPositions-3] / displacementDistances[nPositions-2]; |
68 | 68 |
69 float cosine = scalarProduct((*velocities)[nPositions-3],(*velocities)[nPositions-2]) / (displacementDistances[nPositions-3] * displacementDistances[nPositions-2]); | 69 float cosine = (*velocities)[nPositions-3].dot((*velocities)[nPositions-2]) / (displacementDistances[nPositions-3] * displacementDistances[nPositions-2]); |
70 | 70 |
71 result &= (ratio < accelerationBound) & (cosine > deviationBound); | 71 result = (ratio < accelerationBound) & (cosine > deviationBound); |
72 } | 72 } |
73 return result; | 73 return result; |
74 } | 74 } |
75 | 75 |
76 bool FeatureTrajectory::minMaxSimilarity(const FeatureTrajectory& ft, const int& firstInstant, const int& lastInstant, const float& connectionDistance, const float& segmentationDistance) { | 76 bool FeatureTrajectory::minMaxSimilarity(const FeatureTrajectory& ft, const int& firstInstant, const int& lastInstant, const float& connectionDistance, const float& segmentationDistance) { |
107 | 107 |
108 void FeatureTrajectory::shorten(void) { | 108 void FeatureTrajectory::shorten(void) { |
109 positions->pop_back(); | 109 positions->pop_back(); |
110 velocities->pop_back(); | 110 velocities->pop_back(); |
111 displacementDistances.pop_back(); | 111 displacementDistances.pop_back(); |
112 } | |
113 | |
114 void FeatureTrajectory::movingAverage(const unsigned int& nFramesSmoothing) { | |
115 positions->movingAverage(nFramesSmoothing); | |
116 velocities->movingAverage(nFramesSmoothing); | |
112 } | 117 } |
113 | 118 |
114 void FeatureTrajectory::write(TrajectoryDBAccess<Point2f>& trajectoryDB, const string& positionsTableName, const string& velocitiesTableName) const { | 119 void FeatureTrajectory::write(TrajectoryDBAccess<Point2f>& trajectoryDB, const string& positionsTableName, const string& velocitiesTableName) const { |
115 trajectoryDB.write(*positions, positionsTableName); | 120 trajectoryDB.write(*positions, positionsTableName); |
116 trajectoryDB.write(*velocities, velocitiesTableName); | 121 trajectoryDB.write(*velocities, velocitiesTableName); |
162 int firstInstant = static_cast<int>(MAX(ft->getFirstInstant(), ft2->getFirstInstant())); | 167 int firstInstant = static_cast<int>(MAX(ft->getFirstInstant(), ft2->getFirstInstant())); |
163 if (lastInstant-firstInstant > static_cast<int>(minFeatureTime)) { // equivalent to lastInstant-firstInstant+1 >= minFeatureTime | 168 if (lastInstant-firstInstant > static_cast<int>(minFeatureTime)) { // equivalent to lastInstant-firstInstant+1 >= minFeatureTime |
164 if (ft->minMaxSimilarity(*ft2, firstInstant, lastInstant, connectionDistance, segmentationDistance)) { | 169 if (ft->minMaxSimilarity(*ft2, firstInstant, lastInstant, connectionDistance, segmentationDistance)) { |
165 UndirectedGraph::edge_descriptor e; | 170 UndirectedGraph::edge_descriptor e; |
166 bool unused; | 171 bool unused; |
167 tie(e, unused) = add_edge(newVertex, *vi, graph); | 172 boost::tuples::tie(e, unused) = add_edge(newVertex, *vi, graph); |
168 // no need to add measures to graph[e] (edge properties) | 173 // no need to add measures to graph[e] (edge properties) |
169 } | 174 } |
170 } | 175 } |
171 } | 176 } |
172 } | 177 } |
183 | 188 |
184 vector<unsigned int> lastInstants(num, 0); // last instant of component with id | 189 vector<unsigned int> lastInstants(num, 0); // last instant of component with id |
185 vector<vector<vertex_descriptor> > tmpobjects(num), objects; // vector of components (component = vector of vertex descriptors) | 190 vector<vector<vertex_descriptor> > tmpobjects(num), objects; // vector of components (component = vector of vertex descriptors) |
186 | 191 |
187 graph_traits<UndirectedGraph>::vertex_iterator vi, vend; | 192 graph_traits<UndirectedGraph>::vertex_iterator vi, vend; |
188 for(tie(vi,vend) = vertices(graph); vi != vend; ++vi) { | 193 for(boost::tuples::tie(vi,vend) = vertices(graph); vi != vend; ++vi) { |
189 unsigned int id = components[*vi]; | 194 unsigned int id = components[*vi]; |
190 lastInstants[id] = max(lastInstants[id], graph[*vi].feature->getLastInstant()); | 195 lastInstants[id] = max(lastInstants[id], graph[*vi].feature->getLastInstant()); |
191 tmpobjects[id].push_back(*vi); | 196 tmpobjects[id].push_back(*vi); |
192 } | 197 } |
193 | 198 |
199 if (lastInstants[i] < lastInstant) | 204 if (lastInstants[i] < lastInstant) |
200 objectHypotheses.push_back(tmpobjects[i]); | 205 objectHypotheses.push_back(tmpobjects[i]); |
201 } | 206 } |
202 } | 207 } |
203 | 208 |
204 vector<vector<unsigned int> > FeatureGraph::getFeatureGroups(void) { | 209 void FeatureGraph::getFeatureGroups(vector<vector<FeatureTrajectoryPtr> >& featureGroups) { |
205 vector<vector<unsigned int> > featureGroups; | 210 featureGroups.clear(); |
206 | 211 |
207 for (unsigned int i=0; i<objectHypotheses.size(); ++i) { | 212 for (unsigned int i=0; i<objectHypotheses.size(); ++i) { |
208 // check that there is on average at least minNFeaturesPerGroup features at each frame in the group | 213 // check that there is on average at least minNFeaturesPerGroup features at each frame in the group |
209 unsigned int totalFeatureTime= graph[objectHypotheses[i][0]].feature->length(); | 214 unsigned int totalFeatureTime= graph[objectHypotheses[i][0]].feature->length(); |
210 unsigned int firstInstant = graph[objectHypotheses[i][0]].feature->getFirstInstant(); | 215 unsigned int firstInstant = graph[objectHypotheses[i][0]].feature->getFirstInstant(); |
216 } | 221 } |
217 if (static_cast<float>(totalFeatureTime)/static_cast<float>(lastInstant-firstInstant+1) > minNFeaturesPerGroup) { | 222 if (static_cast<float>(totalFeatureTime)/static_cast<float>(lastInstant-firstInstant+1) > minNFeaturesPerGroup) { |
218 #if DEBUG | 223 #if DEBUG |
219 cout << "save group " << i << " of " << objectHypotheses[i].size() << " features " << endl; | 224 cout << "save group " << i << " of " << objectHypotheses[i].size() << " features " << endl; |
220 #endif | 225 #endif |
221 featureGroups.push_back(vector<unsigned int>()); | 226 featureGroups.push_back(vector<FeatureTrajectoryPtr>()); |
222 for (unsigned int j=0; j<objectHypotheses[i].size(); ++j) { | 227 for (unsigned int j=0; j<objectHypotheses[i].size(); ++j) { |
223 featureGroups.back().push_back(graph[objectHypotheses[i][j]].feature->getId()); | 228 featureGroups.back().push_back(graph[objectHypotheses[i][j]].feature); |
224 #if DEBUG | 229 #if DEBUG |
225 cout << featureGroups.size() << " " << objectHypotheses[i][j] << endl; | 230 cout << featureGroups.size() << " " << objectHypotheses[i][j] << endl; |
226 #endif | 231 #endif |
227 clear_vertex(objectHypotheses[i][j], graph); | 232 clear_vertex(objectHypotheses[i][j], graph); |
228 remove_vertex(objectHypotheses[i][j], graph); | 233 remove_vertex(objectHypotheses[i][j], graph); |
229 } | 234 } |
230 } | 235 } |
231 } | 236 } |
232 | |
233 return featureGroups; | |
234 } | 237 } |
235 | 238 |
236 string FeatureGraph::informationString(void) const { | 239 string FeatureGraph::informationString(void) const { |
237 stringstream ss; | 240 stringstream ss; |
238 ss << num_vertices(graph) << " vertices, " << num_edges(graph) << " edges"; | 241 ss << num_vertices(graph) << " vertices, " << num_edges(graph) << " edges"; |
244 int FeatureGraph::getNEdges(void) const { return num_edges(graph);} | 247 int FeatureGraph::getNEdges(void) const { return num_edges(graph);} |
245 | 248 |
246 void FeatureGraph::computeVertexIndex(void) { | 249 void FeatureGraph::computeVertexIndex(void) { |
247 graph_traits<FeatureGraph::UndirectedGraph>::vertex_iterator vi, vend; | 250 graph_traits<FeatureGraph::UndirectedGraph>::vertex_iterator vi, vend; |
248 graph_traits<FeatureGraph::UndirectedGraph>::vertices_size_type cnt = 0; | 251 graph_traits<FeatureGraph::UndirectedGraph>::vertices_size_type cnt = 0; |
249 for(tie(vi,vend) = vertices(graph); vi != vend; ++vi) | 252 for(boost::tuples::tie(vi,vend) = vertices(graph); vi != vend; ++vi) |
250 graph[*vi].index = cnt++; | 253 graph[*vi].index = cnt++; |
251 } | 254 } |