Mercurial Hosting > traffic-intelligence
annotate c/utils.cpp @ 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 | bee0e7407af7 |
children |
rev | line source |
---|---|
20
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
3
diff
changeset
|
1 #include "utils.hpp" |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
3
diff
changeset
|
2 |
144
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
3 #include <boost/foreach.hpp> |
1002
6c5ce3ec497e
improved handling of path for feature based tracking
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
703
diff
changeset
|
4 #include <boost/filesystem.hpp> |
144
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
5 |
20
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
3
diff
changeset
|
6 #include <iostream> |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
3
diff
changeset
|
7 #include <fstream> |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
3
diff
changeset
|
8 |
1002
6c5ce3ec497e
improved handling of path for feature based tracking
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
703
diff
changeset
|
9 namespace fs = boost::filesystem; // soon std |
6c5ce3ec497e
improved handling of path for feature based tracking
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
703
diff
changeset
|
10 |
20
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
3
diff
changeset
|
11 using namespace std; |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
3
diff
changeset
|
12 |
230
bc4ea09b1743
compatibility modifications for visual studio compilation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
153
diff
changeset
|
13 std::vector<std::vector<float> > loadNumbers(const string& filename, const string& separator /* = " " */) { |
144
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
14 ifstream in(filename.c_str()); |
230
bc4ea09b1743
compatibility modifications for visual studio compilation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
153
diff
changeset
|
15 std::vector<std::vector<float> > result; |
144
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
16 |
145
7bf8084e720f
removed bug with loadMat and added diagnosis code for empty frames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
144
diff
changeset
|
17 if (::openCheck(in, filename, "loadNumbers")) { |
7bf8084e720f
removed bug with loadMat and added diagnosis code for empty frames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
144
diff
changeset
|
18 while (!in.eof()) { |
7bf8084e720f
removed bug with loadMat and added diagnosis code for empty frames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
144
diff
changeset
|
19 string line = ::getlineComment(in); |
230
bc4ea09b1743
compatibility modifications for visual studio compilation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
153
diff
changeset
|
20 std::vector<string> tokens; |
145
7bf8084e720f
removed bug with loadMat and added diagnosis code for empty frames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
144
diff
changeset
|
21 ::split(tokens, line, separator); |
230
bc4ea09b1743
compatibility modifications for visual studio compilation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
153
diff
changeset
|
22 std::vector<float> numbers; |
145
7bf8084e720f
removed bug with loadMat and added diagnosis code for empty frames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
144
diff
changeset
|
23 BOOST_FOREACH(string s, tokens) |
153
c8a149fccfda
corrected bug in reading floats from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
145
diff
changeset
|
24 numbers.push_back(::toFloat(s)); |
145
7bf8084e720f
removed bug with loadMat and added diagnosis code for empty frames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
144
diff
changeset
|
25 if (!numbers.empty()) |
7bf8084e720f
removed bug with loadMat and added diagnosis code for empty frames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
144
diff
changeset
|
26 result.push_back(numbers); |
7bf8084e720f
removed bug with loadMat and added diagnosis code for empty frames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
144
diff
changeset
|
27 } |
144
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
28 } |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
29 |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
30 return result; |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
31 } |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
32 |
1002
6c5ce3ec497e
improved handling of path for feature based tracking
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
703
diff
changeset
|
33 std::string getRelativeFilename(const std::string& parentDirname, const std::string& filename) { |
6c5ce3ec497e
improved handling of path for feature based tracking
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
703
diff
changeset
|
34 fs::path parentPath(parentDirname); |
6c5ce3ec497e
improved handling of path for feature based tracking
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
703
diff
changeset
|
35 fs::path filePath(filename); |
6c5ce3ec497e
improved handling of path for feature based tracking
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
703
diff
changeset
|
36 if (filePath.is_absolute()) |
6c5ce3ec497e
improved handling of path for feature based tracking
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
703
diff
changeset
|
37 return filename; |
6c5ce3ec497e
improved handling of path for feature based tracking
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
703
diff
changeset
|
38 else |
6c5ce3ec497e
improved handling of path for feature based tracking
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
703
diff
changeset
|
39 return (parentPath/filePath).string(); |
6c5ce3ec497e
improved handling of path for feature based tracking
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
703
diff
changeset
|
40 } |
6c5ce3ec497e
improved handling of path for feature based tracking
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
703
diff
changeset
|
41 |
144
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
42 int toInt(const std::string& s) { int i; fromString(i, s); return i;} //atoi |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
43 |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
44 float toFloat(const std::string& s) { float x; fromString(x, s); return x;}// lexical_cast<float>(s) |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
45 |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
46 double toDouble(const std::string& s) { double x; fromString(x, s); return x;} |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
47 |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
48 string getlineComment(ifstream& f) { |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
49 string s; |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
50 getline(f, s); |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
51 while ((!f.eof()) && (s[0] == ::commentChar)) { |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
52 getline(f, s); |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
53 } |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
54 |
399
c389fae9689a
Added a class to read list of image instead of video. This is controlled by the use of the database-filename and folder-data parameters in the config file.
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents:
230
diff
changeset
|
55 if (!s.empty() && s[0] == ::commentChar) |
144
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
56 s.clear(); |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
57 return s; |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
58 } |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
59 |
20
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
3
diff
changeset
|
60 void openWriteScientificPrecision(ofstream& out, const string& filename, const int& precision) { |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
3
diff
changeset
|
61 ::openWritePrecision(out, filename, precision); |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
3
diff
changeset
|
62 out.setf(ios::scientific); |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
3
diff
changeset
|
63 } |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
3
diff
changeset
|
64 |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
3
diff
changeset
|
65 void openWritePrecision(ofstream& out, const string& filename, const int& precision) { |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
3
diff
changeset
|
66 out.open(filename.c_str(), ios::binary); |
70
a52653dca25d
got track features to compile, updated paths to headers and libraries for OpenCV 2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
20
diff
changeset
|
67 ::openCheck(out, filename, "openWritePrecision"); |
20
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
3
diff
changeset
|
68 out.precision(precision); |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
3
diff
changeset
|
69 } |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
3
diff
changeset
|
70 |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
3
diff
changeset
|
71 bool openCheck(ifstream& f, const string& filename, const string& callingFunctionName) { |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
3
diff
changeset
|
72 if (!f.is_open()) { |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
3
diff
changeset
|
73 cerr << "Pb opening file " << filename << " in " << callingFunctionName << endl; |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
3
diff
changeset
|
74 return false; |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
3
diff
changeset
|
75 } else |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
3
diff
changeset
|
76 return true; |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
3
diff
changeset
|
77 } |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
3
diff
changeset
|
78 |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
3
diff
changeset
|
79 bool openCheck(ofstream& f, const string& filename, const string& callingFunctionName) { |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
3
diff
changeset
|
80 if (!f.is_open()) { |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
3
diff
changeset
|
81 cerr << "Pb opening file " << filename << " in " << callingFunctionName << endl; |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
3
diff
changeset
|
82 return false; |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
3
diff
changeset
|
83 } else |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
3
diff
changeset
|
84 return true; |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
3
diff
changeset
|
85 } |