-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathConversion.cpp
219 lines (146 loc) · 4.44 KB
/
Conversion.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
Conversion.cpp
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*
* This file is part of: freeture
*
* Copyright: (C) 2014-2015 Yoan Audureau
* FRIPON-GEOPS-UPSUD-CNRS
*
* License: GNU General Public License
*
* FreeTure is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* FreeTure is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with FreeTure. If not, see <http://www.gnu.org/licenses/>.
*
* Last modified: 20/07/2015
*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
/**
* \file Conversion.cpp
* \author Yoan Audureau -- FRIPON-GEOPS-UPSUD
* \version 1.0
* \date 13/06/2014
* \brief Various conversion tools.
*/
#include "Conversion.h"
string Conversion::matTypeToString(int type) {
string r;
uchar depth = type & CV_MAT_DEPTH_MASK;
uchar chans = 1 + (type >> CV_CN_SHIFT);
switch ( depth ) {
case CV_8U: r = "8U"; break;
case CV_8S: r = "8S"; break;
case CV_16U: r = "16U"; break;
case CV_16S: r = "16S"; break;
case CV_32S: r = "32S"; break;
case CV_32F: r = "32F"; break;
case CV_64F: r = "64F"; break;
default: r = "User"; break;
}
r += "C";
r += (chans+'0');
return r;
}
string Conversion::intToString(int nb){
ostringstream oss;
oss << nb;
string result = oss.str();
return result;
}
float Conversion::roundToNearest(float value, float precision) {
float fractpart1 = 0.0, intpart1 = 0.0, fractpart2 = 0.0, intpart2 = 0.0;
fractpart1 = modf (value , &intpart1);
float d = fractpart1/precision;
fractpart2 = modf (d , &intpart2);
return intpart1 + intpart2*precision;
}
string Conversion::floatToString(float nb){
std::ostringstream ss;
ss << nb;
std::string s(ss.str());
return s;
}
string Conversion::doubleToString(double nb) {
std::ostringstream strs;
strs << nb;
std::string str = strs.str();
return str;
}
void Conversion::stringTok(list<string> &container, string const &in, const char * const delimiters = "_"){
const string::size_type len = in.length();
string::size_type i = 0;
while (i < len){
// Eat leading whitespace
i = in.find_first_not_of(delimiters, i);
if (i == string::npos)
return; // Nothing left but white space
// Find the end of the token
string::size_type j = in.find_first_of(delimiters, i);
// Push token
if (j == string::npos){
container.push_back(in.substr(i));
return;
}else
container.push_back(in.substr(i, j-i));
// Set up for next loop
i = j + 1;
}
}
Mat Conversion::convertTo8UC1(Mat &img) {
Mat tmp;
img.copyTo(tmp);
double min, max;
minMaxLoc(tmp, &min, &max);
tmp.convertTo(tmp, CV_8UC1, 255.0/(max - min), -min * 255.0/(max - min));
return tmp;
}
int Conversion::countNumberDigit(int n){
int nbDigit = 0;
while(n!=0){
n/=10;
++nbDigit;
}
return nbDigit;
}
int Conversion::roundToUpperRange(int n) {
int nbDigit = 0;
int last = 0;
while(n!=0){
last = n;
n/=10;
++nbDigit;
}
int f = 1;
for(int i = 1; i < nbDigit; i++)
f *=10;
return (last+1) * f;
}
string Conversion::numbering(int totalDigit, int n) {
int cpt = 0;
int nbZeroToAdd = 0;
string ch = "";
if(n<10){
nbZeroToAdd = totalDigit - 1;
for(int i = 0; i < nbZeroToAdd; i++){
ch += "0";
}
}else{
while(n > 0){
n/=10;
cpt ++;
}
nbZeroToAdd = totalDigit - cpt;
for(int i = 0; i < nbZeroToAdd; i++){
ch += "0";
}
}
return ch;
}