changeset 225:d4d3b1e8a9f1

added code to process only needed frames based on saved features
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 26 Jun 2012 03:37:19 -0400
parents 37a434fb848e
children 91197f6a03fe
files c/Motion.cpp c/feature-based-tracking.cpp
diffstat 2 files changed, 40 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/c/Motion.cpp	Tue Jun 26 03:36:55 2012 -0400
+++ b/c/Motion.cpp	Tue Jun 26 03:37:19 2012 -0400
@@ -101,8 +101,8 @@
   if (frameNum > lastInstant)
     lastInstant = frameNum;
   computeMotionData(frameNum);
-  assert(positions.size() == displacementDistances.size()+1);
-  assert(positions.size() == velocities.size()+1);
+  assert(positions->size() == displacementDistances.size()+1);
+  assert(positions->size() == velocities->size()+1);
 }
 
 void FeatureTrajectory::shorten(void) { 
@@ -158,8 +158,8 @@
        vi!=vertices(graph).second; ++vi) { // vi pointer to vertex_descriptor
     FeatureTrajectoryPtr ft2 = graph[*vi].feature;
     if (newVertex != *vi) {
-      int lastInstant = static_cast<int>(min(ft->getLastInstant(), ft2->getLastInstant()));
-      int firstInstant = static_cast<int>(max(ft->getFirstInstant(), ft2->getFirstInstant()));
+      int lastInstant = static_cast<int>(MIN(ft->getLastInstant(), ft2->getLastInstant()));
+      int firstInstant = static_cast<int>(MAX(ft->getFirstInstant(), ft2->getFirstInstant()));
       if (lastInstant-firstInstant > static_cast<int>(minFeatureTime)) { // equivalent to lastInstant-firstInstant+1 >= minFeatureTime
 	if (ft->minMaxSimilarity(*ft2, firstInstant, lastInstant, connectionDistance, segmentationDistance)) {
 	  UndirectedGraph::edge_descriptor e;
@@ -211,8 +211,8 @@
     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());	
+	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
--- a/c/feature-based-tracking.cpp	Tue Jun 26 03:36:55 2012 -0400
+++ b/c/feature-based-tracking.cpp	Tue Jun 26 03:37:19 2012 -0400
@@ -271,10 +271,14 @@
   // c_end = std::clock();
   // cout << "Loaded " << trajectories.size() << " trajectories one by one in " << 1000.0 * (c_end-c_start) / CLOCKS_PER_SEC << " CPU seconds" << endl;
 
-  trajectoryDB->createViewInstants("table");
+  trajectoryDB->createInstants("table");
 
-  unsigned int maxTrajectoryLength;
-  trajectoryDB->maxTrajectoryLength(maxTrajectoryLength);
+  unsigned int maxTrajectoryLength = 0;
+  success = trajectoryDB->maxTrajectoryLength(maxTrajectoryLength);
+  if (!success || maxTrajectoryLength == 0) {
+    cout << "problem with trajectory length " << success << endl;
+    exit(0);
+  }
   cout << "Longest trajectory: " << maxTrajectoryLength << endl;
 
   // 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
@@ -283,10 +287,17 @@
   FeatureGraph featureGraph(params.mmConnectionDistance, params.mmSegmentationDistance, params.minFeatureTime, params.minNFeaturesPerGroup);
 
   // main loop
-  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
+  int frameNum;
+  unsigned int firstFrameNum, lastFrameNum;
+  trajectoryDB->firstLastInstants(firstFrameNum, lastFrameNum);
+  firstFrameNum = MAX(firstFrameNum, params.frame1);
+  if (params.nFrames>0)
+    lastFrameNum = MIN(lastFrameNum,params.frame1+params.nFrames);
+  for (frameNum = firstFrameNum; frameNum<lastFrameNum; frameNum ++) {
     vector<int> trajectoryIds;
     success  = trajectoryDB->trajectoryIdEndingAt(trajectoryIds, frameNum); // ending
-    cout << "frame " << frameNum << endl;
+    if (frameNum%100 ==0)
+      cout << "frame " << frameNum << endl;
     //success  = trajectoryDB->trajectoryIdInInterval(trajectoryIds, frameNum, min(frameNum+queryIntervalLength-1, frameNum+params.nFrames)); // ending
 #if DEBUG
     cout << trajectoryIds.size() << " trajectories " << endl;
@@ -307,14 +318,26 @@
     }
 
     // check for connected components
-    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"));
-      savedObjectId++;
+    int lastInstant = frameNum+params.minFeatureTime-maxTrajectoryLength;
+    if (lastInstant > 0) {
+      featureGraph.connectedComponents(lastInstant);
+      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"));
+	savedObjectId++;
+      }
     }
     
-    cout << featureGraph.informationString() << endl;
+    if (frameNum%100 ==0)
+      cout << featureGraph.informationString() << endl;
+  }
+
+  // save remaining objects
+  featureGraph.connectedComponents(frameNum+maxTrajectoryLength+1);
+  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"));
+    savedObjectId++;
   }
 
   trajectoryDB->endTransaction();