-
Notifications
You must be signed in to change notification settings - Fork 0
/
funcoesArquivo.cpp
executable file
·317 lines (277 loc) · 9.97 KB
/
funcoesArquivo.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/**
* Authors:
* Luciana Calixta Escobar
* Gabriela Thumé
*
* Universidade de São Paulo / ICMC
**/
#include "funcoesArquivo.h"
/* Read the features and save them in Mat data */
Mat readFeatures(const string& filename, Mat &classes, int &nClasses){
int i, j;
float features;
Mat data;
size_t n, d;
ifstream myFile(filename.c_str());
string line, infos, numImage, classe, numFeatures, numClasses, objetos;
if(!myFile)
throw exception();
getline(myFile, infos);
if (infos == "")
return Mat();
stringstream info(infos);
getline(info, objetos, '\t');
getline(info, numClasses, '\t');
nClasses = atoi(numClasses.c_str());
getline(info, numFeatures, '\t');
n = atoi(objetos.c_str());
d = atoi(numFeatures.c_str());
data.create(n, d, CV_32FC1);
classes.create(n, 1, CV_32FC1);
while (getline(myFile, line)) {
stringstream vector_features(line);
getline(vector_features, numImage, '\t');
getline(vector_features, classe, '\t');
i = atoi(numImage.c_str());
j = 0;
while(vector_features >> features) {
data.at<float>(i, j) = (float)features;
j++;
}
classes.at<float>(i, 0)=atoi(classe.c_str());
}
myFile.close();
return data;
}
int qtdArquivos(char *directory){
int count = 0;
struct dirent *sDir = NULL;
DIR *dir = NULL;
dir = opendir(directory);
if(dir == NULL) {
return 0;
}
while((sDir = readdir(dir))) {
if( (strcmp(sDir->d_name, ".") != 0) &&
(strcmp(sDir->d_name, "..") != 0) &&
(strcmp(sDir->d_name, ".directory") != 0) &&
(strcmp(sDir->d_name, ".DS_Store") != 0) &&
(strcmp(sDir->d_name, ".directory") != 0)) {
count++;
}
}
closedir(dir);
return count;
}
int qtdImagensTotal(const char *base, int qtdClasses, int *objClass, int *maxs){
int i, count = 0, currentSize;
char *directory = (char*)calloc(256, sizeof(char));
*maxs = 0;
for (i = 1; i <= qtdClasses; i++){
sprintf(directory, "%s/%d/treino/", base, i);
currentSize = qtdArquivos(directory);
sprintf(directory, "%s/%d/teste/", base, i);
currentSize += qtdArquivos(directory);
if (currentSize == 0){
sprintf(directory, "%s/%d/", base, i);
currentSize = qtdArquivos(directory);
if (currentSize == 0){
fprintf(stderr,"Erro! There is no directory named %s\n", directory);
}
}
objClass[i-1] = currentSize;
count += currentSize;
if (currentSize > *maxs || *maxs == 0)
*maxs = currentSize;
}
return count;
}
int descriptor(char const *baseImagem, char const *featuresDirectory, int method, int numberColor, double nRes, int oNorm, int *param, int nparam, int oZero, int quantMethod, char const *id = ""){
int i, j, k, qtdImagem = 0, qtdClasses = 0, qtdImgTotal = 0, imgTotal = 0;
int featureVectorSize = 0, resizingFactor = (int)(nRes*100), maxc = 0;
float min, max, normFactor;
char directory[128], nome[256];
double **Cm = NULL;
Mat img, featureVector, features, labels, newimg;
FILE *arq;
char *quantMethodsNames[4] = {(char *)"Intensity", (char *)"Gleam", (char *)"Luminance", (char *)"MSB"};
cout << "\n---------------------------------------------------------------------------------------" << endl;
cout << "Image feature extraction" << endl;
cout << "---------------------------------------------------------------------------------------" << endl;
switch (method) {
case 1:
sprintf(nome,"%s/BIC_%s_%dc_%dr_%s.txt", featuresDirectory, quantMethodsNames[quantMethod-1], numberColor, resizingFactor, id);
featureVectorSize = numberColor*2;
cout << "BIC and " << quantMethodsNames[quantMethod-1] << ":";
break;
case 2:
sprintf(nome,"%s/GCH_%s_%dc_%dr_%s.txt", featuresDirectory, quantMethodsNames[quantMethod-1], numberColor, resizingFactor, id);
featureVectorSize = numberColor;
cout << "GCH and " << quantMethodsNames[quantMethod-1] << ":";
break;
case 3:
sprintf(nome,"%s/CCV_%s_%dc_%dr_%s.txt", featuresDirectory, quantMethodsNames[quantMethod-1], numberColor, resizingFactor, id);
featureVectorSize = numberColor*2;
cout << "CCV and " << quantMethodsNames[quantMethod-1] << ":";
break;
case 4:
sprintf(nome,"%s/Haralick6_%s_%dc_%dr_%s.txt", featuresDirectory, quantMethodsNames[quantMethod-1], numberColor, resizingFactor, id);
Cm = (double **)calloc(numberColor, sizeof(double*));
for (i=0; i<numberColor; i++) {
Cm[i]= (double *)calloc(numberColor, sizeof(double));
}
featureVectorSize = 6;
cout << "Haralick-6 and " << quantMethodsNames[quantMethod-1] << ":";
break;
case 5:
sprintf(nome,"%s/ACC_%s_%dc_%dd_%dr_%s.txt", featuresDirectory, quantMethodsNames[quantMethod-1], numberColor, nparam, resizingFactor, id);
featureVectorSize = (numberColor*nparam);
cout << "ACC and " << quantMethodsNames[quantMethod-1] << " c/ " << nparam << " distancias : ";
break;
default:
break;
}
featureVector.create(1, featureVectorSize, CV_64F);
featureVector = Scalar::all(0);
cout << numberColor << " cores, tamanho " << nRes << endl;
cout << "File: " << nome << endl;
arq = fopen(nome, "w+");
sprintf(directory, "%s/", baseImagem);
qtdClasses = qtdArquivos(directory);
int *objperClass = (int *)malloc(qtdClasses*sizeof(int));
qtdImgTotal = qtdImagensTotal(baseImagem, qtdClasses, objperClass, &maxc);
fprintf(arq,"%d\t%d\t%d\n", qtdImgTotal, qtdClasses, featureVectorSize);
cout << "Objects: " << qtdImgTotal << " - Classes: " << qtdClasses << " - Features: " << featureVectorSize << endl;
for (i = 0; i < qtdClasses; i++) {
int bars = (int)(((float)objperClass[i]/(float)qtdImgTotal)*50.0);
cout << (i+1) << " ";
for (j = 0; j < bars; j++){
cout << "|";
}
float porc = (float)objperClass[i]/(float)qtdImgTotal;
cout << " " << porc*100 << "%" << " (" << objperClass[i] << ")" <<endl;
}
features = Mat::zeros(qtdImgTotal, featureVectorSize, CV_32F);
labels = Mat::zeros(qtdImgTotal, 1, CV_8U);
for(i = 1; i <= qtdClasses; i++) {
sprintf(directory,"%s/%d/treino/", baseImagem, i);
qtdImagem = qtdArquivos(directory);
int treino = qtdImagem;
sprintf(directory,"%s/%d/teste/", baseImagem, i);
qtdImagem += qtdArquivos(directory);
if (qtdImagem == 0){
sprintf(directory,"%s/%d/", baseImagem, i);
qtdImagem = qtdArquivos(directory);
if (qtdImagem == 0){
fprintf(stderr,"Erro! There is no directory named %s\n", directory);
}
}
cout << "classe " << i << " : " << directory << " imagens " << qtdImagem << endl;
for(j = 0; j < qtdImagem; j++) {
sprintf(directory,"%s/%d/%d.jpg", baseImagem, i, j);
img = imread(directory, CV_LOAD_IMAGE_COLOR);
if (img.empty()) {
sprintf(directory,"%s/%d/treino/%d.jpg", baseImagem, i, j);
img = imread(directory, CV_LOAD_IMAGE_COLOR);
if (img.empty()){
sprintf(directory,"%s/%d/teste/%d.jpg", baseImagem, i, j-treino);
img = imread(directory, CV_LOAD_IMAGE_COLOR);
if (img.empty()){
cout << "Erro ao abrir imagem " << directory << endl;
exit(1);
}
}
}
if (nRes < 1) {
resize(img, newimg, Size(), nRes, nRes, INTER_AREA);
if (imgTotal==0)
imwrite("out.jpg", newimg);
}
else
img.copyTo(newimg);
switch(quantMethod){
case 1:
QuantizationIntensity(newimg, newimg, numberColor);
break;
case 2:
QuantizationGleam(newimg, newimg, numberColor);
break;
case 3:
QuantizationLuminance(newimg, newimg, numberColor);
break;
case 4:
QuantizationMSB(newimg, newimg, numberColor);
break;
default:
cout << "ERRO: metodo de quantizacao nao existe!!!" << endl;
exit(-1);
}
switch(method){
/* BIC: image, descriptor, number of colors, normalization */
case 1:
BIC(newimg, featureVector, numberColor, oNorm);
break;
/* GCH: image, descriptor, number of colors, normalization */
case 2:
GCH(newimg, featureVector, numberColor, oNorm);
break;
/* CCV: image, descriptor, number of colors, normalization,
limiar coerente/incoerente */
case 3:
CCV(newimg, featureVector, numberColor, oNorm, param[0]);
break;
/* HARALICK: image, co-occurrence matriz, descriptor
numero de cores, normalization */
case 4:
HARALICK(newimg, Cm, featureVector, numberColor, oNorm);
break;
/* ACC: image, descriptor, number of colors, normalization,
distance vector, distance number */
case 5:
ACC(newimg, featureVector, numberColor, oNorm, param, nparam);
break;
}
labels.at<uchar>(imgTotal,0) = (uchar)i;
for(k = 0; k < (featureVectorSize); k++) {
features.at<float>(imgTotal,k) = featureVector.at<float>(0, k);
}
imgTotal++;
}
}
if(method == 4 && oNorm != 0){
normFactor = (oNorm == 1) ? 1.0 : 255.0;
for(j = 0; j < features.cols; ++j){
min = features.at<float>(0,j);
max = features.at<float>(0,j);
for(i = 1; i < features.rows; ++i){
if(features.at<float>(i,j) > max)
max = features.at<float>(i,j);
if (features.at<float>(i,j) < min)
min = features.at<float>(i,j);
}
for(i = 0; i < features.rows; ++i)
features.at<float>(i,j) = normFactor * ((features.at<float>(i,j) - min) / (max - min));
}
}
cout << "Wrote on file " << nome << endl;
cout << "---------------------------------------------------------------------------------------" << endl;
for (i = 0; i < imgTotal; i++) {
fprintf(arq, "%d\t%d\t", i, labels.at<uchar>(i,0));
for(k = 0; k < featureVectorSize; k++) {
if (oNorm == 2) {
fprintf(arq,"%.f ", features.at<float>(i, k));
}
else
fprintf(arq,"%.5f ", features.at<float>(i, k));
}
fprintf(arq,"\n");
}
if (method == 4) {
for (i=0; i<numberColor; i++) {
free(Cm[i]);
}
free(Cm);
}
fclose(arq);
return 1;
}