changeset 1002:6c5ce3ec497e

improved handling of path for feature based tracking
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 01 Jun 2018 17:19:24 -0400
parents cc7c6b821ae6
children 75af46516b2b
files c/Makefile c/Parameters.cpp c/feature-based-tracking.cpp c/utils.cpp include/Parameters.hpp include/utils.hpp
diffstat 6 files changed, 35 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/c/Makefile	Wed May 30 14:44:08 2018 -0400
+++ b/c/Makefile	Fri Jun 01 17:19:24 2018 -0400
@@ -11,7 +11,7 @@
 #LDFLAGS = -Wl,-Bstatic -lm
 LDFLAGS = -lm
 LDFLAGS += -lTrajectoryManagementAndAnalysis -lsqlite3
-LDFLAGS += -lboost_program_options
+LDFLAGS += -lboost_program_options -lboost_filesystem -lboost_system
 #LDFLAGS += -lfltk
 
 CFLAGS = -Wall -W -Wextra -std=c++11
--- a/c/Parameters.cpp	Wed May 30 14:44:08 2018 -0400
+++ b/c/Parameters.cpp	Fri Jun 01 17:19:24 2018 -0400
@@ -1,11 +1,14 @@
 #include "Parameters.hpp"
 
 #include <boost/program_options.hpp>
+#include <boost/filesystem.hpp>
 
 #include <iostream>
 #include <fstream>
 
 namespace po = boost::program_options;
+namespace fs = boost::filesystem; // soon std
+
 using namespace std;
 
 KLTFeatureTrackingParameters::KLTFeatureTrackingParameters(const int argc, char* argv[]) {
@@ -92,9 +95,10 @@
       cout << cmdLine << endl;
       exit(0);      
     }
-      
+
     cout << "Using configuration file " << configurationFilename << endl;
-
+    parentDirname = fs::path(configurationFilename).parent_path().string();
+    
     ifstream configurationFile(configurationFilename.c_str());
     store(po::parse_config_file(configurationFile, cmdLineAndFile, true), vm);
     notify(vm);
--- a/c/feature-based-tracking.cpp	Wed May 30 14:44:08 2018 -0400
+++ b/c/feature-based-tracking.cpp	Fri Jun 01 17:19:24 2018 -0400
@@ -16,6 +16,7 @@
 #include "opencv2/calib3d/calib3d.hpp"
 
 #include <boost/foreach.hpp>
+#include <boost/filesystem.hpp>
 
 #include <iostream>
 #include <vector>
@@ -24,6 +25,8 @@
 #include <memory>
 #include <limits>
 
+namespace fs = boost::filesystem; // soon std
+
 using namespace std;
 using namespace cv;
 
@@ -63,7 +66,8 @@
 }
 
 void trackFeatures(const KLTFeatureTrackingParameters& params) {
-  Mat homography = ::loadMat(params.homographyFilename, " ");
+  Mat homography = ::loadMat(::getRelativeFilename(params.parentDirname, params.homographyFilename), " ");
+    
   Mat invHomography;
   if (params.display && !homography.empty())
     invHomography = homography.inv();
@@ -90,8 +94,8 @@
     cout << "Empty video filename. Exiting." << endl;
     exit(0);
   }
-    
-  VideoCapture capture(params.videoFilename);
+
+  VideoCapture capture(::getRelativeFilename(params.parentDirname, params.videoFilename));
   if(!capture.isOpened()) {
     cout << "Video filename " << params.videoFilename << " could not be opened. Exiting." << endl;
     exit(0);
@@ -113,7 +117,7 @@
   Mat map1, map2;
   Mat intrinsicCameraMatrix, newIntrinsicCameraMatrix;
   if (params.undistort) {
-    intrinsicCameraMatrix = ::loadMat(params.intrinsicCameraFilename, " ");
+    intrinsicCameraMatrix = ::loadMat(::getRelativeFilename(params.parentDirname, params.intrinsicCameraFilename), " ");
     Size undistortedVideoSize = Size(static_cast<int>(round(videoSize.width*params.undistortedImageMultiplication)), static_cast<int>(round(videoSize.height*params.undistortedImageMultiplication)));
     newIntrinsicCameraMatrix = getDefaultNewCameraMatrix(intrinsicCameraMatrix, undistortedVideoSize, true);// for some reason, it's double type //getOptimalNewCameraMatrix(intrinsicCameraMatrix, params.distortionCoefficients, videoSize, 1, undistortedVideoSize);//, 0, true);
     initUndistortRectifyMap(intrinsicCameraMatrix, params.distortionCoefficients, Mat::eye(3,3, CV_32FC1) /* 0 ?*/, newIntrinsicCameraMatrix, undistortedVideoSize, CV_32FC1, map1, map2);
@@ -122,14 +126,14 @@
       ", height=" << undistortedVideoSize.height << endl;
   }
   
-  Mat mask = imread(params.maskFilename, 0);
+  Mat mask = imread(::getRelativeFilename(params.parentDirname, params.maskFilename), 0);
   if (mask.empty()) {
     cout << "Mask filename " << params.maskFilename << " could not be opened." << endl;
     mask = Mat::ones(videoSize, CV_8UC1);
   }
 
   std::shared_ptr<TrajectoryDBAccess<Point2f> > trajectoryDB = std::shared_ptr<TrajectoryDBAccess<Point2f> >(new TrajectoryDBAccessList<Point2f>());
-  trajectoryDB->connect(params.databaseFilename.c_str());
+  trajectoryDB->connect(::getRelativeFilename(params.parentDirname, params.databaseFilename).c_str());
   trajectoryDB->createTable("positions");
   trajectoryDB->createTable("velocities");
   trajectoryDB->beginTransaction();
@@ -264,7 +268,7 @@
 
 void groupFeatures(const KLTFeatureTrackingParameters& params) {
   std::shared_ptr<TrajectoryDBAccessList<Point2f> > trajectoryDB = std::shared_ptr<TrajectoryDBAccessList<Point2f> >(new TrajectoryDBAccessList<Point2f>());
-  bool success = trajectoryDB->connect(params.databaseFilename.c_str());
+  bool success = trajectoryDB->connect(::getRelativeFilename(params.parentDirname, params.databaseFilename).c_str());
   trajectoryDB->createObjectTable("objects", "objects_features");
   unsigned int savedObjectId=0;
 
@@ -339,7 +343,7 @@
 
 void loadingTimes(const KLTFeatureTrackingParameters& params) {
   std::shared_ptr<TrajectoryDBAccessList<Point2f> > trajectoryDB = std::shared_ptr<TrajectoryDBAccessList<Point2f> >(new TrajectoryDBAccessList<Point2f>());
-  bool success = trajectoryDB->connect(params.databaseFilename.c_str());
+  bool success = trajectoryDB->connect(::getRelativeFilename(params.parentDirname, params.databaseFilename).c_str());
   
   vector<std::shared_ptr<Trajectory<Point2f> > > trajectories;
   //cout << trajectories.size() << endl;
--- a/c/utils.cpp	Wed May 30 14:44:08 2018 -0400
+++ b/c/utils.cpp	Fri Jun 01 17:19:24 2018 -0400
@@ -1,10 +1,13 @@
 #include "utils.hpp"
 
 #include <boost/foreach.hpp>
+#include <boost/filesystem.hpp>
 
 #include <iostream>
 #include <fstream>
 
+namespace fs = boost::filesystem; // soon std
+
 using namespace std;
 
 std::vector<std::vector<float> > loadNumbers(const string& filename, const string& separator /* = " " */) {
@@ -27,6 +30,15 @@
   return result;
 }
 
+std::string getRelativeFilename(const std::string& parentDirname, const std::string& filename) {
+  fs::path parentPath(parentDirname);
+  fs::path filePath(filename);
+  if (filePath.is_absolute())
+    return filename;
+  else
+    return (parentPath/filePath).string();
+}
+
 int toInt(const std::string& s) { int i; fromString(i, s); return i;} //atoi
 
 float toFloat(const std::string& s) { float x; fromString(x, s); return x;}// lexical_cast<float>(s)
--- a/include/Parameters.hpp	Wed May 30 14:44:08 2018 -0400
+++ b/include/Parameters.hpp	Fri Jun 01 17:19:24 2018 -0400
@@ -18,6 +18,7 @@
   bool groupFeatures;
   bool loadingTime;
 
+  std::string parentDirname;
   std::string videoFilename;
   std::string databaseFilename;
   std::string homographyFilename;
--- a/include/utils.hpp	Wed May 30 14:44:08 2018 -0400
+++ b/include/utils.hpp	Fri Jun 01 17:19:24 2018 -0400
@@ -18,6 +18,9 @@
  * Warning: returns an empty string if all the lines to the end of the file are comments. */
 std::string getlineComment(std::ifstream& f);
 
+/** Get relative filename if not absolute */
+std::string getRelativeFilename(const std::string& parentDirname, const std::string& filename);
+
 /** Converts a string to an integer. */
 int toInt(const std::string& s);