diff 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
line wrap: on
line diff
--- a/c/Motion.cpp	Thu Jun 21 23:50:51 2012 -0400
+++ b/c/Motion.cpp	Mon Jun 25 23:50:18 2012 -0400
@@ -172,29 +172,30 @@
   }
 }
 
-void FeatureGraph::connectedComponents(const int& lastInstant) {
+void FeatureGraph::connectedComponents(const unsigned int& lastInstant) {
   computeVertexIndex();
   property_map<UndirectedGraph, int VertexInformation::*>::type components = get(&VertexInformation::index, graph);
 
   int num = connected_components(graph, components, vertex_index_map(get(&VertexInformation::index, graph)));
+#ifdef DEBUG
   cout << "last instant threshold " << lastInstant << " Total number of components: " << num << endl;
+#endif
 
   vector<unsigned int> lastInstants(num, 0); // last instant of component with id
   vector<vector<vertex_descriptor> > tmpobjects(num), objects; // vector of components (component = vector of vertex descriptors)
 
   graph_traits<UndirectedGraph>::vertex_iterator vi, vend;
   for(tie(vi,vend) = vertices(graph); vi != vend; ++vi) {
-    //for (int i = 0; i < nVertices; ++i) {
     unsigned int id = components[*vi];
-    //cout << "Vertex " << *vi << " is in component " << id << endl;// "(last " << graph[i].feature->getLastInstant() << " " << lastInstants[components[i]] << " " << (lastInstants[components[i]] < graph[i].feature->getLastInstant()) << ")" << endl;
-    if (lastInstants[id] < graph[*vi].feature->getLastInstant())
-      lastInstants[id] = graph[*vi].feature->getLastInstant();
+    lastInstants[id] = max(lastInstants[id], graph[*vi].feature->getLastInstant());
     tmpobjects[id].push_back(*vi);
   }
 
   objectHypotheses.clear();
   for (int i = 0; i < num; ++i) {
+#ifdef DEBUG
     cout << i << " " << lastInstants[i] << endl;
+#endif
     if (static_cast<int>(lastInstants[i]) < lastInstant)
       objectHypotheses.push_back(tmpobjects[i]);
   }
@@ -205,17 +206,26 @@
 
   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[i].size(); ++j)
-      totalFeatureTime += static_cast<float>(graph[objectHypotheses[i][j]].feature->length());
-    cout << i << endl;
-    if (totalFeatureTime/static_cast<float>(objectHypotheses[i].size()) > minNFeaturesPerGroup) {
-      cout << "save group " << objectHypotheses[i].size() << " " << totalFeatureTime/static_cast<float>(objectHypotheses[i].size()) << endl;
+    unsigned int totalFeatureTime= graph[objectHypotheses[i][0]].feature->length();
+    unsigned int firstInstant = graph[objectHypotheses[i][0]].feature->getFirstInstant();
+    unsigned int lastInstant = graph[objectHypotheses[i][0]].feature->getLastInstant();
+      for (unsigned int j=1; j<objectHypotheses[i].size(); ++j) {
+	totalFeatureTime += graph[objectHypotheses[i][j]].feature->length();
+	firstInstant = min(firstInstant, graph[objectHypotheses[i][j]].feature->getFirstInstant());
+	lastInstant = max(lastInstant, graph[objectHypotheses[i][j]].feature->getLastInstant());	
+      }
+    if (static_cast<float>(totalFeatureTime)/static_cast<float>(lastInstant-firstInstant+1) > minNFeaturesPerGroup) {
+#if DEBUG
+      cout << "save group " << i << " of " << objectHypotheses[i].size() << " features " << endl;
+#endif
       featureGroups.push_back(vector<unsigned int>());
       for (unsigned int j=0; j<objectHypotheses[i].size(); ++j) {
-	featureGroups.back().push_back(graph[objectHypotheses[i][j]].feature->getId());cout << featureGroups.size() << " " << objectHypotheses[i][j] << endl;
-	clear_vertex(objectHypotheses[i][j], graph);cout << "cleared "<< objectHypotheses[i][j] << endl;
-	remove_vertex(objectHypotheses[i][j], graph);cout << "removed "<< objectHypotheses[i][j] << endl;
+	featureGroups.back().push_back(graph[objectHypotheses[i][j]].feature->getId());
+#if DEBUG
+	cout << featureGroups.size() << " " << objectHypotheses[i][j] << endl;
+#endif
+	clear_vertex(objectHypotheses[i][j], graph);
+	remove_vertex(objectHypotheses[i][j], graph);
       }
     }
   }