diff 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
line wrap: on
line diff
--- a/c/Motion.cpp	Wed Dec 07 18:51:32 2011 -0500
+++ b/c/Motion.cpp	Thu Dec 08 18:32:35 2011 -0500
@@ -7,9 +7,11 @@
 #include "opencv2/highgui/highgui.hpp"
 
 #include <boost/graph/connected_components.hpp>
+#include <boost/property_map/property_map.hpp>
 
 #include <iostream>
 #include <vector>
+#include <map>
 #include <algorithm>
 #include <utility>
 
@@ -147,7 +149,7 @@
 /******************** FeatureGraph ********************/
 
 void FeatureGraph::addFeature(const FeatureTrajectoryPtr& ft) {
-  UndirectedGraph::vertex_descriptor newVertex = add_vertex(graph);
+  vertex_descriptor newVertex = add_vertex(graph);
   graph[newVertex].feature = ft;
   for (graph_traits<UndirectedGraph>::vertex_iterator vi = vertices(graph).first; 
        vi!=vertices(graph).second; ++vi) { // vi pointer to vertex_descriptor
@@ -165,17 +167,53 @@
   }
 }
 
-void FeatureGraph::connectedComponents(const int& lastInstant) {
-  vector<UndirectedGraph::vertex_descriptor> components(num_vertices(graph));
-  //vector_property_map<UndirectedGraph::vertex_descriptor> components(num_vertices(graph));
-  //vector_property_map< graph_traits<UndirectedGraph>::vertex_descriptor, property_map<UndirectedGraph, vertex_index_t>::const_type > components(num_vertices(graph));
+vector<vector<FeatureGraph::vertex_descriptor> > FeatureGraph::connectedComponents(const int& lastInstant) {
+  int nVertices = num_vertices(graph);
+  vector<vertex_descriptor> components(nVertices);
+  // map<UndirectedGraph::vertex_descriptor, graph_traits<UndirectedGraph>::vertices_size_type> vertex2component;
+  // associative_property_map< map<UndirectedGraph::vertex_descriptor, graph_traits<UndirectedGraph>::vertices_size_type> > components(vertex2component);
 
   int num = connected_components(graph, &components[0]);
   cout << "Total number of components: " << num << endl;
 
-  for (unsigned int i = 0; i < num_vertices(graph); ++i)
-    cout << "Vertex " << i <<" is in component " << components[i] << endl;
-  cout << endl;
+  vector<unsigned int> lastInstants(num, 0);
+  vector<vector<vertex_descriptor> > tmpobjects(num), objects;
+
+  for (int i = 0; i < nVertices; ++i) {
+    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;
+    if (lastInstants[components[i]] < graph[i].feature->getLastInstant())
+      lastInstants[components[i]] = graph[i].feature->getLastInstant();
+    tmpobjects[components[i]].push_back(i);
+  }
+
+  for (int i = 0; i < num; ++i) {
+    cout << i << " " << lastInstants[i] << endl;
+    if (static_cast<int>(lastInstants[i]) < lastInstant)
+      objects.push_back(tmpobjects[i]);
+  }
+
+  return objects;
+}
+
+vector<vector<unsigned int> > FeatureGraph::getFeatureGroups(const vector<vector<FeatureGraph::vertex_descriptor> >& objectHypotheses) {
+  vector<vector<unsigned int> > featureGroups;
+
+  for (unsigned int i=0; i<objectHypotheses.size(); ++i) {
+    // check that there is on average at least minNFeaturesPerGroup features at each frame in the group
+    float totalFeatureTime=0;
+    for (unsigned int j=0; j<objectHypotheses.size(); ++j)
+      totalFeatureTime += static_cast<float>(graph[objectHypotheses[i][j]].feature->length());
+    
+    if (totalFeatureTime/static_cast<float>(objectHypotheses.size()) > minNFeaturesPerGroup) {
+      cout << "save group" << endl;
+      featureGroups.push_back(vector<unsigned int>());
+      for (unsigned int j=0; j<objectHypotheses.size(); ++j)
+	featureGroups.back().push_back(graph[objectHypotheses[i][j]].feature->getId());
+    }
+    // remove vertices: todo using listS
+  }
+
+  return featureGroups;
 }
 
 string FeatureGraph::informationString(void) {