comparison c/Motion.cpp @ 194:09c7881073f3

connected commponents works, but issue with removing the vertices
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 12 Dec 2011 18:32:10 -0500
parents 38974d27dd2d
children aeab0b88c9b6
comparison
equal deleted inserted replaced
193:a728fce85881 194:09c7881073f3
5 5
6 #include "opencv2/core/core.hpp" 6 #include "opencv2/core/core.hpp"
7 #include "opencv2/highgui/highgui.hpp" 7 #include "opencv2/highgui/highgui.hpp"
8 8
9 #include <boost/graph/connected_components.hpp> 9 #include <boost/graph/connected_components.hpp>
10 #include <boost/property_map/property_map.hpp>
11 10
12 #include <iostream> 11 #include <iostream>
13 #include <vector> 12 #include <vector>
14 #include <map> 13 #include <map>
15 #include <algorithm> 14 #include <algorithm>
165 } 164 }
166 } 165 }
167 } 166 }
168 } 167 }
169 168
170 vector<vector<FeatureGraph::vertex_descriptor> > FeatureGraph::connectedComponents(const int& lastInstant) { 169 void FeatureGraph::connectedComponents(const int& lastInstant) {
171 computeVertexIndex(); 170 computeVertexIndex();
172 property_map<UndirectedGraph, int VertexInformation::*>::type components = get(&VertexInformation::index, graph); 171 property_map<UndirectedGraph, int VertexInformation::*>::type components = get(&VertexInformation::index, graph);
173 172
174 int num = connected_components(graph, components, vertex_index_map(get(&VertexInformation::index, graph))); 173 int num = connected_components(graph, components, vertex_index_map(get(&VertexInformation::index, graph)));
175 cout << "Total number of components: " << num << endl; 174 cout << "Total number of components: " << num << endl;
179 178
180 graph_traits<UndirectedGraph>::vertex_iterator vi, vend; 179 graph_traits<UndirectedGraph>::vertex_iterator vi, vend;
181 for(tie(vi,vend) = vertices(graph); vi != vend; ++vi) { 180 for(tie(vi,vend) = vertices(graph); vi != vend; ++vi) {
182 //for (int i = 0; i < nVertices; ++i) { 181 //for (int i = 0; i < nVertices; ++i) {
183 unsigned int id = components[*vi]; 182 unsigned int id = components[*vi];
184 cout << "Vertex " << *vi << " is in component " << id << endl;// "(last " << graph[i].feature->getLastInstant() << " " << lastInstants[components[i]] << " " << (lastInstants[components[i]] < graph[i].feature->getLastInstant()) << ")" << endl; 183 //cout << "Vertex " << *vi << " is in component " << id << endl;// "(last " << graph[i].feature->getLastInstant() << " " << lastInstants[components[i]] << " " << (lastInstants[components[i]] < graph[i].feature->getLastInstant()) << ")" << endl;
185 if (lastInstants[id] < graph[*vi].feature->getLastInstant()) 184 if (lastInstants[id] < graph[*vi].feature->getLastInstant())
186 lastInstants[id] = graph[*vi].feature->getLastInstant(); 185 lastInstants[id] = graph[*vi].feature->getLastInstant();
187 tmpobjects[id].push_back(*vi); 186 tmpobjects[id].push_back(*vi);
188 } 187 }
189 188
189 objectHypotheses.clear();
190 for (int i = 0; i < num; ++i) { 190 for (int i = 0; i < num; ++i) {
191 cout << i << " " << lastInstants[i] << endl; 191 cout << i << " " << lastInstants[i] << endl;
192 if (static_cast<int>(lastInstants[i]) < lastInstant) 192 if (static_cast<int>(lastInstants[i]) < lastInstant)
193 objects.push_back(tmpobjects[i]); 193 objectHypotheses.push_back(tmpobjects[i]);
194 } 194 }
195 195 }
196 return objects; 196
197 } 197 vector<vector<unsigned int> > FeatureGraph::getFeatureGroups(void) {
198
199 vector<vector<unsigned int> > FeatureGraph::getFeatureGroups(const vector<vector<FeatureGraph::vertex_descriptor> >& objectHypotheses) {
200 vector<vector<unsigned int> > featureGroups; 198 vector<vector<unsigned int> > featureGroups;
201 199
202 for (unsigned int i=0; i<objectHypotheses.size(); ++i) { 200 for (unsigned int i=0; i<objectHypotheses.size(); ++i) {
203 // check that there is on average at least minNFeaturesPerGroup features at each frame in the group 201 // check that there is on average at least minNFeaturesPerGroup features at each frame in the group
204 float totalFeatureTime=0; 202 float totalFeatureTime=0;
205 for (unsigned int j=0; j<objectHypotheses.size(); ++j) 203 for (unsigned int j=0; j<objectHypotheses[i].size(); ++j)
206 totalFeatureTime += static_cast<float>(graph[objectHypotheses[i][j]].feature->length()); 204 totalFeatureTime += static_cast<float>(graph[objectHypotheses[i][j]].feature->length());
207 205 cout << i << endl;
208 if (totalFeatureTime/static_cast<float>(objectHypotheses.size()) > minNFeaturesPerGroup) { 206 if (totalFeatureTime/static_cast<float>(objectHypotheses[i].size()) > minNFeaturesPerGroup) {
209 cout << "save group" << endl; 207 cout << "save group " << objectHypotheses[i].size() << " " << totalFeatureTime/static_cast<float>(objectHypotheses[i].size()) << endl;
210 featureGroups.push_back(vector<unsigned int>()); 208 featureGroups.push_back(vector<unsigned int>());
211 for (unsigned int j=0; j<objectHypotheses.size(); ++j) 209 for (unsigned int j=0; j<objectHypotheses[i].size(); ++j) {
212 featureGroups.back().push_back(graph[objectHypotheses[i][j]].feature->getId()); 210 featureGroups.back().push_back(graph[objectHypotheses[i][j]].feature->getId());cout << featureGroups.size() << " " << objectHypotheses[i][j] << endl;
211 clear_vertex(objectHypotheses[i][j], graph);cout << "cleared "<< objectHypotheses[i][j] << endl;
212 remove_vertex(objectHypotheses[i][j], graph);cout << "removed "<< objectHypotheses[i][j] << endl;
213 }
213 } 214 }
214 // remove vertices: todo using listS
215 } 215 }
216 216
217 return featureGroups; 217 return featureGroups;
218 } 218 }
219 219