annotate c/utils.cpp @ 962:64259b9885bf

verbose option to print classification information (more to add)
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 06 Nov 2017 21:25:41 -0500
parents bee0e7407af7
children 6c5ce3ec497e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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>
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
4
20
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
5 #include <iostream>
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
6 #include <fstream>
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
7
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
8 using namespace std;
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
9
230
bc4ea09b1743 compatibility modifications for visual studio compilation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 153
diff changeset
10 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
11 ifstream in(filename.c_str());
230
bc4ea09b1743 compatibility modifications for visual studio compilation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 153
diff changeset
12 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
13
145
7bf8084e720f removed bug with loadMat and added diagnosis code for empty frames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 144
diff changeset
14 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
15 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
16 string line = ::getlineComment(in);
230
bc4ea09b1743 compatibility modifications for visual studio compilation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 153
diff changeset
17 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
18 ::split(tokens, line, separator);
230
bc4ea09b1743 compatibility modifications for visual studio compilation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 153
diff changeset
19 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
20 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
21 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
22 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
23 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
24 }
144
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
25 }
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
26
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
27 return result;
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 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
31
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
32 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
33
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
34 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
35
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
36 string getlineComment(ifstream& f) {
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
37 string s;
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
38 getline(f, s);
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
39 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
40 getline(f, s);
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
41 }
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
42
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
43 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
44 s.clear();
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
45 return s;
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
46 }
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
47
20
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
48 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
49 ::openWritePrecision(out, filename, precision);
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
50 out.setf(ios::scientific);
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
51 }
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
52
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
53 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
54 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
55 ::openCheck(out, filename, "openWritePrecision");
20
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
56 out.precision(precision);
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
57 }
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
58
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
59 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
60 if (!f.is_open()) {
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
61 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
62 return false;
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
63 } else
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
64 return true;
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
65 }
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
66
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
67 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
68 if (!f.is_open()) {
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
69 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
70 return false;
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
71 } else
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
72 return true;
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
73 }