annotate c/utils.cpp @ 398:3399bd48cb40

Ajout d'une méthode pour obtenir le nombre de FPS Méthode de capture des trames vidéos plus résistante aux erreur Utilisation d'un dictionnaire pour les fichier de configuration afin de garder le nom des sections
author Jean-Philippe Jodoin <jpjodoin@gmail.com>
date Mon, 29 Jul 2013 13:46:07 -0400
parents bc4ea09b1743
children c389fae9689a
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>
144
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
7 #include <sstream>
20
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
8
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
9 using namespace std;
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
10
230
bc4ea09b1743 compatibility modifications for visual studio compilation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 153
diff changeset
11 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
12 ifstream in(filename.c_str());
230
bc4ea09b1743 compatibility modifications for visual studio compilation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 153
diff changeset
13 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
14
145
7bf8084e720f removed bug with loadMat and added diagnosis code for empty frames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 144
diff changeset
15 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
16 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
17 string line = ::getlineComment(in);
230
bc4ea09b1743 compatibility modifications for visual studio compilation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 153
diff changeset
18 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
19 ::split(tokens, line, separator);
230
bc4ea09b1743 compatibility modifications for visual studio compilation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 153
diff changeset
20 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
21 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
22 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
23 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
24 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
25 }
144
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
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
28 return result;
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
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
31 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
32
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
33 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
34
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
35 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
36
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
37 string getlineComment(ifstream& f) {
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
38 string s;
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
39 getline(f, s);
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
40 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
41 getline(f, s);
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
42 }
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 if (s[0] == ::commentChar)
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
45 s.clear();
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
46 return s;
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
20
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
49 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
50 ::openWritePrecision(out, filename, precision);
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
51 out.setf(ios::scientific);
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
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
54 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
55 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
56 ::openCheck(out, filename, "openWritePrecision");
20
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
57 out.precision(precision);
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
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
60 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
61 if (!f.is_open()) {
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
62 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
63 return false;
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
64 } else
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
65 return true;
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
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
68 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
69 if (!f.is_open()) {
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
70 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
71 return false;
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
72 } else
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
73 return true;
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
74 }