diff c/feature-based-tracking.cpp @ 509:935430b1d408

corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 23 May 2014 16:27:26 -0400
parents 081a9da6f85b
children 018653d1db3c
line wrap: on
line diff
--- a/c/feature-based-tracking.cpp	Fri May 23 10:35:51 2014 -0400
+++ b/c/feature-based-tracking.cpp	Fri May 23 16:27:26 2014 -0400
@@ -23,6 +23,7 @@
 #include <iostream>
 #include <vector>
 #include <ctime>
+#include <cmath>
 
 using namespace std;
 using namespace cv;
@@ -78,6 +79,18 @@
   float minTotalFeatureDisplacement = params.nDisplacements*params.minFeatureDisplacement;
   Size window = Size(params.windowSize, params.windowSize);
 
+  int interpolationMethod = -1;
+  if (params.interpolationMethod == 0)
+    interpolationMethod = INTER_NEAREST;
+  else if (params.interpolationMethod == 1)
+    interpolationMethod = INTER_LINEAR;
+  else if (params.interpolationMethod == 2)
+    interpolationMethod = INTER_CUBIC;
+  else if (params.interpolationMethod == 3)
+    interpolationMethod = INTER_LANCZOS4;
+  else
+    cout << "Unsupported option " << interpolationMethod << " for interpolation method" << endl;
+
   // BruteForceMatcher<Hamming> descMatcher;
   // vector<DMatch> matches;
 
@@ -95,7 +108,6 @@
   }
   
   Size videoSize = capture->getSize();
-  //cout << capture->getSize() << " " << params.undistortedImageMultiplication*videoSize << endl;
   unsigned int nFrames = capture->getNbFrames();
   cout << "Video " << params.videoFilename <<
 	  ": width=" << videoSize.width <<
@@ -103,23 +115,21 @@
 	  ", nframes=" << nFrames << endl;
 
   Mat newIntrinsicCameraMatrix = intrinsicCameraMatrix.clone(); 
-  Size newVideoSize = videoSize;
+  Mat map1, map2;
   if (params.undistort) {
-    newVideoSize = Size(static_cast<int>(videoSize.width*params.undistortedImageMultiplication), static_cast<int>(videoSize.height*params.undistortedImageMultiplication));
-    newIntrinsicCameraMatrix.at<float>(0,2) = newVideoSize.width/2.;
-    newIntrinsicCameraMatrix.at<float>(1,2) = newVideoSize.height/2.;
+    videoSize = Size(static_cast<int>(round(videoSize.width*params.undistortedImageMultiplication)), static_cast<int>(round(videoSize.height*params.undistortedImageMultiplication)));
+    newIntrinsicCameraMatrix.at<float>(0,2) = videoSize.width/2.;
+    newIntrinsicCameraMatrix.at<float>(1,2) = videoSize.height/2.;
+    initUndistortRectifyMap(intrinsicCameraMatrix, params.distortionCoefficients, Mat::eye(3,3, CV_32FC1), newIntrinsicCameraMatrix, videoSize, CV_32FC1, map1, map2);
+    
+    cout << "Undistorted width=" << videoSize.width <<
+      ", height=" << videoSize.height << endl;
   }
   
-  Mat map1, map2;
-  Mat R = Mat::eye(3,3, CV_32FC1);
-  if (params.undistort)
-    initUndistortRectifyMap(intrinsicCameraMatrix, params.distortionCoefficients, R, newIntrinsicCameraMatrix, newVideoSize, CV_32FC1, map1, map2);
-  
-  // todo mask in new size
   Mat mask = imread(params.maskFilename, 0);
   if (mask.empty()) {
     cout << "Mask filename " << params.maskFilename << " could not be opened." << endl;
-    mask = Mat::ones(newVideoSize, CV_8UC1);
+    mask = Mat::ones(videoSize, CV_8UC1);
   }
 
   boost::shared_ptr<TrajectoryDBAccess<Point2f> > trajectoryDB = boost::shared_ptr<TrajectoryDBAccess<Point2f> >(new TrajectoryDBAccessList<Point2f>());
@@ -150,7 +160,7 @@
   for (unsigned int frameNum = params.frame1; (frameNum < lastFrameNum) && !::interruptionKey(key); frameNum++) {
       bool success = capture->getNextFrame(frame);
       if (params.undistort) {
-	remap(frame, undistortedFrame, map1, map2, INTER_LINEAR, BORDER_CONSTANT, 0.);
+	remap(frame, undistortedFrame, map1, map2, interpolationMethod, BORDER_CONSTANT, 0.);
 	frame = undistortedFrame;
       }
       //if (!success || frame.empty() || frame.size() != videoSize)