Mercurial > hg > nsaunier > traffic-intelligence
comparison c/Motion.cpp @ 221:bc93e87a2108
cleaned and corrected connected components and feature groups
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Mon, 25 Jun 2012 23:50:18 -0400 |
| parents | f7ddfc4aeb1e |
| children | d4d3b1e8a9f1 |
comparison
equal
deleted
inserted
replaced
| 220:f0f800b95765 | 221:bc93e87a2108 |
|---|---|
| 170 } | 170 } |
| 171 } | 171 } |
| 172 } | 172 } |
| 173 } | 173 } |
| 174 | 174 |
| 175 void FeatureGraph::connectedComponents(const int& lastInstant) { | 175 void FeatureGraph::connectedComponents(const unsigned int& lastInstant) { |
| 176 computeVertexIndex(); | 176 computeVertexIndex(); |
| 177 property_map<UndirectedGraph, int VertexInformation::*>::type components = get(&VertexInformation::index, graph); | 177 property_map<UndirectedGraph, int VertexInformation::*>::type components = get(&VertexInformation::index, graph); |
| 178 | 178 |
| 179 int num = connected_components(graph, components, vertex_index_map(get(&VertexInformation::index, graph))); | 179 int num = connected_components(graph, components, vertex_index_map(get(&VertexInformation::index, graph))); |
| 180 #ifdef DEBUG | |
| 180 cout << "last instant threshold " << lastInstant << " Total number of components: " << num << endl; | 181 cout << "last instant threshold " << lastInstant << " Total number of components: " << num << endl; |
| 182 #endif | |
| 181 | 183 |
| 182 vector<unsigned int> lastInstants(num, 0); // last instant of component with id | 184 vector<unsigned int> lastInstants(num, 0); // last instant of component with id |
| 183 vector<vector<vertex_descriptor> > tmpobjects(num), objects; // vector of components (component = vector of vertex descriptors) | 185 vector<vector<vertex_descriptor> > tmpobjects(num), objects; // vector of components (component = vector of vertex descriptors) |
| 184 | 186 |
| 185 graph_traits<UndirectedGraph>::vertex_iterator vi, vend; | 187 graph_traits<UndirectedGraph>::vertex_iterator vi, vend; |
| 186 for(tie(vi,vend) = vertices(graph); vi != vend; ++vi) { | 188 for(tie(vi,vend) = vertices(graph); vi != vend; ++vi) { |
| 187 //for (int i = 0; i < nVertices; ++i) { | |
| 188 unsigned int id = components[*vi]; | 189 unsigned int id = components[*vi]; |
| 189 //cout << "Vertex " << *vi << " is in component " << id << endl;// "(last " << graph[i].feature->getLastInstant() << " " << lastInstants[components[i]] << " " << (lastInstants[components[i]] < graph[i].feature->getLastInstant()) << ")" << endl; | 190 lastInstants[id] = max(lastInstants[id], graph[*vi].feature->getLastInstant()); |
| 190 if (lastInstants[id] < graph[*vi].feature->getLastInstant()) | |
| 191 lastInstants[id] = graph[*vi].feature->getLastInstant(); | |
| 192 tmpobjects[id].push_back(*vi); | 191 tmpobjects[id].push_back(*vi); |
| 193 } | 192 } |
| 194 | 193 |
| 195 objectHypotheses.clear(); | 194 objectHypotheses.clear(); |
| 196 for (int i = 0; i < num; ++i) { | 195 for (int i = 0; i < num; ++i) { |
| 196 #ifdef DEBUG | |
| 197 cout << i << " " << lastInstants[i] << endl; | 197 cout << i << " " << lastInstants[i] << endl; |
| 198 #endif | |
| 198 if (static_cast<int>(lastInstants[i]) < lastInstant) | 199 if (static_cast<int>(lastInstants[i]) < lastInstant) |
| 199 objectHypotheses.push_back(tmpobjects[i]); | 200 objectHypotheses.push_back(tmpobjects[i]); |
| 200 } | 201 } |
| 201 } | 202 } |
| 202 | 203 |
| 203 vector<vector<unsigned int> > FeatureGraph::getFeatureGroups(void) { | 204 vector<vector<unsigned int> > FeatureGraph::getFeatureGroups(void) { |
| 204 vector<vector<unsigned int> > featureGroups; | 205 vector<vector<unsigned int> > featureGroups; |
| 205 | 206 |
| 206 for (unsigned int i=0; i<objectHypotheses.size(); ++i) { | 207 for (unsigned int i=0; i<objectHypotheses.size(); ++i) { |
| 207 // check that there is on average at least minNFeaturesPerGroup features at each frame in the group | 208 // check that there is on average at least minNFeaturesPerGroup features at each frame in the group |
| 208 float totalFeatureTime=0; | 209 unsigned int totalFeatureTime= graph[objectHypotheses[i][0]].feature->length(); |
| 209 for (unsigned int j=0; j<objectHypotheses[i].size(); ++j) | 210 unsigned int firstInstant = graph[objectHypotheses[i][0]].feature->getFirstInstant(); |
| 210 totalFeatureTime += static_cast<float>(graph[objectHypotheses[i][j]].feature->length()); | 211 unsigned int lastInstant = graph[objectHypotheses[i][0]].feature->getLastInstant(); |
| 211 cout << i << endl; | 212 for (unsigned int j=1; j<objectHypotheses[i].size(); ++j) { |
| 212 if (totalFeatureTime/static_cast<float>(objectHypotheses[i].size()) > minNFeaturesPerGroup) { | 213 totalFeatureTime += graph[objectHypotheses[i][j]].feature->length(); |
| 213 cout << "save group " << objectHypotheses[i].size() << " " << totalFeatureTime/static_cast<float>(objectHypotheses[i].size()) << endl; | 214 firstInstant = min(firstInstant, graph[objectHypotheses[i][j]].feature->getFirstInstant()); |
| 215 lastInstant = max(lastInstant, graph[objectHypotheses[i][j]].feature->getLastInstant()); | |
| 216 } | |
| 217 if (static_cast<float>(totalFeatureTime)/static_cast<float>(lastInstant-firstInstant+1) > minNFeaturesPerGroup) { | |
| 218 #if DEBUG | |
| 219 cout << "save group " << i << " of " << objectHypotheses[i].size() << " features " << endl; | |
| 220 #endif | |
| 214 featureGroups.push_back(vector<unsigned int>()); | 221 featureGroups.push_back(vector<unsigned int>()); |
| 215 for (unsigned int j=0; j<objectHypotheses[i].size(); ++j) { | 222 for (unsigned int j=0; j<objectHypotheses[i].size(); ++j) { |
| 216 featureGroups.back().push_back(graph[objectHypotheses[i][j]].feature->getId());cout << featureGroups.size() << " " << objectHypotheses[i][j] << endl; | 223 featureGroups.back().push_back(graph[objectHypotheses[i][j]].feature->getId()); |
| 217 clear_vertex(objectHypotheses[i][j], graph);cout << "cleared "<< objectHypotheses[i][j] << endl; | 224 #if DEBUG |
| 218 remove_vertex(objectHypotheses[i][j], graph);cout << "removed "<< objectHypotheses[i][j] << endl; | 225 cout << featureGroups.size() << " " << objectHypotheses[i][j] << endl; |
| 226 #endif | |
| 227 clear_vertex(objectHypotheses[i][j], graph); | |
| 228 remove_vertex(objectHypotheses[i][j], graph); | |
| 219 } | 229 } |
| 220 } | 230 } |
| 221 } | 231 } |
| 222 | 232 |
| 223 return featureGroups; | 233 return featureGroups; |
