# HG changeset patch # User Nicolas Saunier # Date 1527887964 14400 # Node ID 6c5ce3ec497edc8470851f1fcecc75acf3943200 # Parent cc7c6b821ae6f88bb30ab1388c42ca40bb8c6461 improved handling of path for feature based tracking diff -r cc7c6b821ae6 -r 6c5ce3ec497e c/Makefile --- 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 diff -r cc7c6b821ae6 -r 6c5ce3ec497e c/Parameters.cpp --- 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 +#include #include #include 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); diff -r cc7c6b821ae6 -r 6c5ce3ec497e c/feature-based-tracking.cpp --- 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 +#include #include #include @@ -24,6 +25,8 @@ #include #include +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(round(videoSize.width*params.undistortedImageMultiplication)), static_cast(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 > trajectoryDB = std::shared_ptr >(new TrajectoryDBAccessList()); - 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 > trajectoryDB = std::shared_ptr >(new TrajectoryDBAccessList()); - 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 > trajectoryDB = std::shared_ptr >(new TrajectoryDBAccessList()); - bool success = trajectoryDB->connect(params.databaseFilename.c_str()); + bool success = trajectoryDB->connect(::getRelativeFilename(params.parentDirname, params.databaseFilename).c_str()); vector > > trajectories; //cout << trajectories.size() << endl; diff -r cc7c6b821ae6 -r 6c5ce3ec497e c/utils.cpp --- 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 +#include #include #include +namespace fs = boost::filesystem; // soon std + using namespace std; std::vector > 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(s) diff -r cc7c6b821ae6 -r 6c5ce3ec497e include/Parameters.hpp --- 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; diff -r cc7c6b821ae6 -r 6c5ce3ec497e include/utils.hpp --- 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);