changeset 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 f0f800b95765
children 426321b46e44
files c/Motion.cpp c/feature-based-tracking.cpp include/Motion.hpp
diffstat 3 files changed, 29 insertions(+), 21 deletions(-) [+]
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);
       }
     }
   }
--- a/c/feature-based-tracking.cpp	Thu Jun 21 23:50:51 2012 -0400
+++ b/c/feature-based-tracking.cpp	Mon Jun 25 23:50:18 2012 -0400
@@ -272,10 +272,9 @@
   // cout << "Loaded " << trajectories.size() << " trajectories one by one in " << 1000.0 * (c_end-c_start) / CLOCKS_PER_SEC << " CPU seconds" << endl;
 
   trajectoryDB->createViewInstants();
-  // int maxTrajectoryLength;
-  // trajectoryDB->maxTrajectoryLength(maxTrajectoryLength);
-  // cout << "max trajectory length " << maxTrajectoryLength << endl;
-  //maxTrajectoryLength = 20; // for tests
+
+  // unsigned int maxTrajectoryLength;
+  // // trajectoryDB->maxTrajectoryLength(maxTrajectoryLength);
 
   // alternative: read and load features in batches directly select * from positions where trajectory_id in select trajectory_id from positions where frame_number <100 and frame_number > 50 group by trajectory_id
   int queryIntervalLength = 100;
@@ -283,7 +282,6 @@
   FeatureGraph featureGraph(params.mmConnectionDistance, params.mmSegmentationDistance, params.minFeatureTime, params.minNFeaturesPerGroup);
 
   // main loop
-  // TODO version that can be interrupted?
   for (int frameNum = params.frame1; ((frameNum-params.frame1 < params.nFrames) || (params.nFrames < 0)); frameNum++) { // frameNum += queryIntervalLength // interval = frameNum, min(frameNum+queryIntervalLength, frameNum+params.nFrames) // stop if no trajectory available ? problem if nothing moves, timeout, get max of trajectory frame numbers
     vector<int> trajectoryIds;
     success  = trajectoryDB->trajectoryIdEndingAt(trajectoryIds, frameNum); // ending
@@ -302,7 +300,7 @@
     // check for connected components that are old enough (no chance to match with trajectories to be added later
     // we could check only when some features are getting old enough?
     if (frameNum%10 == 0) {
-      featureGraph.connectedComponents(frameNum-maxTrajectoryLength+params.minFeatureTime);
+      featureGraph.connectedComponents(frameNum+params.minFeatureTime);
       vector<vector<unsigned int> > featureGroups = featureGraph.getFeatureGroups();
       for (unsigned int i=0; i<featureGroups.size(); ++i) {
 	trajectoryDB->writeObject(savedObjectId, featureGroups[i], -1, 1, string("objects"), string("objects_features"));
--- a/include/Motion.hpp	Thu Jun 21 23:50:51 2012 -0400
+++ b/include/Motion.hpp	Mon Jun 25 23:50:18 2012 -0400
@@ -113,7 +113,7 @@
   // find connected components, check if old enough, if so, remove
 
   /// Computes the connected components: features have to be older than lastInstant
-  void connectedComponents(const int& lastInstant);
+  void connectedComponents(const unsigned int& lastInstant);
 
   /** Performs some checks on groups of features and return their lists of ids if correct
       Removes the vertices from the graph */