comparison 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
comparison
equal deleted inserted replaced
190:36968a63efe1 191:0e60a306d324
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>
10 11
11 #include <iostream> 12 #include <iostream>
12 #include <vector> 13 #include <vector>
14 #include <map>
13 #include <algorithm> 15 #include <algorithm>
14 #include <utility> 16 #include <utility>
15 17
16 using namespace std; 18 using namespace std;
17 using namespace cv; 19 using namespace cv;
145 } 147 }
146 148
147 /******************** FeatureGraph ********************/ 149 /******************** FeatureGraph ********************/
148 150
149 void FeatureGraph::addFeature(const FeatureTrajectoryPtr& ft) { 151 void FeatureGraph::addFeature(const FeatureTrajectoryPtr& ft) {
150 UndirectedGraph::vertex_descriptor newVertex = add_vertex(graph); 152 vertex_descriptor newVertex = add_vertex(graph);
151 graph[newVertex].feature = ft; 153 graph[newVertex].feature = ft;
152 for (graph_traits<UndirectedGraph>::vertex_iterator vi = vertices(graph).first; 154 for (graph_traits<UndirectedGraph>::vertex_iterator vi = vertices(graph).first;
153 vi!=vertices(graph).second; ++vi) { // vi pointer to vertex_descriptor 155 vi!=vertices(graph).second; ++vi) { // vi pointer to vertex_descriptor
154 FeatureTrajectoryPtr ft2 = graph[*vi].feature; 156 FeatureTrajectoryPtr ft2 = graph[*vi].feature;
155 int lastInstant = static_cast<int>(min(ft->getLastInstant(), ft2->getLastInstant())); 157 int lastInstant = static_cast<int>(min(ft->getLastInstant(), ft2->getLastInstant()));
163 } 165 }
164 } 166 }
165 } 167 }
166 } 168 }
167 169
168 void FeatureGraph::connectedComponents(const int& lastInstant) { 170 vector<vector<FeatureGraph::vertex_descriptor> > FeatureGraph::connectedComponents(const int& lastInstant) {
169 vector<UndirectedGraph::vertex_descriptor> components(num_vertices(graph)); 171 int nVertices = num_vertices(graph);
170 //vector_property_map<UndirectedGraph::vertex_descriptor> components(num_vertices(graph)); 172 vector<vertex_descriptor> components(nVertices);
171 //vector_property_map< graph_traits<UndirectedGraph>::vertex_descriptor, property_map<UndirectedGraph, vertex_index_t>::const_type > components(num_vertices(graph)); 173 // map<UndirectedGraph::vertex_descriptor, graph_traits<UndirectedGraph>::vertices_size_type> vertex2component;
174 // associative_property_map< map<UndirectedGraph::vertex_descriptor, graph_traits<UndirectedGraph>::vertices_size_type> > components(vertex2component);
172 175
173 int num = connected_components(graph, &components[0]); 176 int num = connected_components(graph, &components[0]);
174 cout << "Total number of components: " << num << endl; 177 cout << "Total number of components: " << num << endl;
175 178
176 for (unsigned int i = 0; i < num_vertices(graph); ++i) 179 vector<unsigned int> lastInstants(num, 0);
177 cout << "Vertex " << i <<" is in component " << components[i] << endl; 180 vector<vector<vertex_descriptor> > tmpobjects(num), objects;
178 cout << endl; 181
182 for (int i = 0; i < nVertices; ++i) {
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;
184 if (lastInstants[components[i]] < graph[i].feature->getLastInstant())
185 lastInstants[components[i]] = graph[i].feature->getLastInstant();
186 tmpobjects[components[i]].push_back(i);
187 }
188
189 for (int i = 0; i < num; ++i) {
190 cout << i << " " << lastInstants[i] << endl;
191 if (static_cast<int>(lastInstants[i]) < lastInstant)
192 objects.push_back(tmpobjects[i]);
193 }
194
195 return objects;
196 }
197
198 vector<vector<unsigned int> > FeatureGraph::getFeatureGroups(const vector<vector<FeatureGraph::vertex_descriptor> >& objectHypotheses) {
199 vector<vector<unsigned int> > featureGroups;
200
201 for (unsigned int i=0; i<objectHypotheses.size(); ++i) {
202 // check that there is on average at least minNFeaturesPerGroup features at each frame in the group
203 float totalFeatureTime=0;
204 for (unsigned int j=0; j<objectHypotheses.size(); ++j)
205 totalFeatureTime += static_cast<float>(graph[objectHypotheses[i][j]].feature->length());
206
207 if (totalFeatureTime/static_cast<float>(objectHypotheses.size()) > minNFeaturesPerGroup) {
208 cout << "save group" << endl;
209 featureGroups.push_back(vector<unsigned int>());
210 for (unsigned int j=0; j<objectHypotheses.size(); ++j)
211 featureGroups.back().push_back(graph[objectHypotheses[i][j]].feature->getId());
212 }
213 // remove vertices: todo using listS
214 }
215
216 return featureGroups;
179 } 217 }
180 218
181 string FeatureGraph::informationString(void) { 219 string FeatureGraph::informationString(void) {
182 stringstream ss; 220 stringstream ss;
183 ss << num_vertices(graph) << " vertices, " << num_edges(graph) << " edges"; 221 ss << num_vertices(graph) << " vertices, " << num_edges(graph) << " edges";