Mercurial Hosting > traffic-intelligence
changeset 144:b32947b002da
added the code to read matrices from text files
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Fri, 26 Aug 2011 19:38:11 -0400 |
parents | 436b87d4b992 |
children | 7bf8084e720f |
files | c/Makefile c/cvutils.cpp c/feature-based-tracking.cpp c/utils.cpp include/cvutils.hpp include/utils.hpp |
diffstat | 6 files changed, 110 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/c/Makefile Wed Aug 24 19:43:44 2011 -0400 +++ b/c/Makefile Fri Aug 26 19:38:11 2011 -0400 @@ -65,7 +65,7 @@ test: echo "coucou $(HOME)" -feature-based-tracking: feature-based-tracking.o cvutils.o Motion.o Parameters.o +feature-based-tracking: feature-based-tracking.o cvutils.o Motion.o Parameters.o utils.o $(CXX) $(CFLAGS) $(LIBS) $^ -o $(BUILD_DIR)/$@ $(LDFLAGS) track-features.o: track-features.cpp
--- a/c/cvutils.cpp Wed Aug 24 19:43:44 2011 -0400 +++ b/c/cvutils.cpp Fri Aug 26 19:38:11 2011 -0400 @@ -1,4 +1,5 @@ #include "cvutils.hpp" +#include "utils.hpp" #include "opencv2/core/core.hpp" #include "opencv2/highgui/highgui.hpp" @@ -10,6 +11,21 @@ using namespace std; using namespace cv; +cv::Mat loadMat(const string& filename, const string& separator) { + vector<vector<float> > numbers = ::loadNumbers(filename, separator); + + Mat mat; + + if (!numbers.empty()) { + mat = Mat(numbers.size(),numbers[0].size(), CV_32FC1); + for (unsigned int i=0; i<numbers.size(); i++) + for (unsigned int j=0; j<numbers[0].size(); j++) + mat.at<float>(i,j) = numbers[i][j]; + } + + return mat; +} + void keyPoints2Points(const vector<KeyPoint>& kpts, vector<Point2f>& pts, const bool& clearPts /* = true */) { if (clearPts) pts.clear();
--- a/c/feature-based-tracking.cpp Wed Aug 24 19:43:44 2011 -0400 +++ b/c/feature-based-tracking.cpp Fri Aug 26 19:38:11 2011 -0400 @@ -70,6 +70,9 @@ KLTFeatureTrackingParameters params(argc, argv); cout << params.parameterDescription << endl; + Mat m = ::loadMat(params.homographyFilename, " "); + //cout << m << endl; + float minTotalFeatureDisplacement = params.nDisplacements*params.minFeatureDisplacement; Size window = Size(params.windowSize, params.windowSize); @@ -115,7 +118,7 @@ // cout << "capture device " << argv[1] << " failed to open!" << endl; // return 1; // } - + // database boost::shared_ptr<TrajectoryDBAccess<Point2f> > trajectoryDB = boost::shared_ptr<TrajectoryDBAccess<Point2f> >(new TrajectoryDBAccessList<Point2f>()); //TrajectoryDBAccess<Point2f>* trajectoryDB = new TrajectoryDBAccessBlob<Point2f>();
--- a/c/utils.cpp Wed Aug 24 19:43:44 2011 -0400 +++ b/c/utils.cpp Fri Aug 26 19:38:11 2011 -0400 @@ -1,10 +1,50 @@ #include "utils.hpp" +#include <boost/foreach.hpp> + #include <iostream> #include <fstream> +#include <sstream> using namespace std; +vector<vector<float> > loadNumbers(const string& filename, const string& separator /* = " " */) { + ifstream in(filename.c_str()); + ::openCheck(in, filename, "loadNumbers"); + vector<vector<float> > result; + + while (!in.eof()) { + string line = ::getlineComment(in); + vector<string> tokens; + ::split(tokens, line, separator); + vector<float> numbers; + BOOST_FOREACH(string s, tokens) + numbers.push_back(::toInt(s)); + if (!numbers.empty()) + result.push_back(numbers); + } + + return result; +} + +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) + +double toDouble(const std::string& s) { double x; fromString(x, s); return x;} + +string getlineComment(ifstream& f) { + string s; + getline(f, s); + while ((!f.eof()) && (s[0] == ::commentChar)) { + getline(f, s); + } + + if (s[0] == ::commentChar) + s.clear(); + return s; +} + void openWriteScientificPrecision(ofstream& out, const string& filename, const int& precision) { ::openWritePrecision(out, filename, precision); out.setf(ios::scientific);
--- a/include/cvutils.hpp Wed Aug 24 19:43:44 2011 -0400 +++ b/include/cvutils.hpp Fri Aug 26 19:38:11 2011 -0400 @@ -10,6 +10,9 @@ /// constant that indicates if the image should be flipped //static const int flipImage = CV_CVTIMG_FLIP; +/** Loads a cv mat from a text file where the numbers are saved line by line separated by separator */ +cv::Mat loadMat(const std::string& filename, const std::string& separator); + template<typename T> float scalarProduct(const cv::Point_<T>& v1, const cv::Point_<T>& v2) { return v1.x*v2.x+v1.y*v2.y;}
--- a/include/utils.hpp Wed Aug 24 19:43:44 2011 -0400 +++ b/include/utils.hpp Fri Aug 26 19:38:11 2011 -0400 @@ -2,9 +2,30 @@ #define UTILS_HPP #include <iosfwd> +#include <string> +#include <vector> static const double pi = 3.14159265358979323846; +static const std::string separator = " "; +const char commentChar = '#'; + +/** Loads lines of numbers from a text file, separated by some character */ +std::vector<std::vector<float> > loadNumbers(const std::string& filename, const std::string& separator = ::separator); + +/** Gets line in a file, skipping comments (starting with '#') (wrapper around getline). + * Warning: returns an empty string if all the lines to the end of the file are comments. */ +std::string getlineComment(std::ifstream& f); + +/** Converts a string to an integer. */ +int toInt(const std::string& s); + +/** Converts a string to a float. */ +float toFloat(const std::string& s); + +/** Converts a string to a double. */ +double toDouble(const std::string& s); + /** Opens file for writing with fixed scientific precision. */ void openWriteScientificPrecision(std::ofstream& out, const std::string& filename, const int& precision); @@ -30,4 +51,29 @@ inline bool skipKey(const int& pressedKey) { return (((char)pressedKey) == 'n' || ((char)pressedKey) == 'N');} + +// templates + +/** Splits a string on the separator. The resulting string items are added to the result (the list is not cleared). */ +template<class Seq> + inline void split(Seq& result, const std::string& text, const std::string& separator = ::separator) { + int n = text.length(); + + int start = text.find_first_not_of(separator); + while ((start >= 0) && (start < n)) { + int stop = text.find_first_of(separator, start); + if ((stop < 0) || (stop > n)) + stop = n; + result.push_back(text.substr(start, stop - start)); + start = text.find_first_not_of(separator, stop+1); + } +} + +/** Converts strings to numbers. */ +template<typename T> +bool fromString(T & result, const std::string & s) { + std::istringstream iss(s); + return iss >> result != 0; +} + #endif