-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassifier.cpp
More file actions
288 lines (216 loc) · 8.02 KB
/
Copy pathclassifier.cpp
File metadata and controls
288 lines (216 loc) · 8.02 KB
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
//
// classifier.cpp
// colorDescriptor
//
// Copyright 2022 Grigorios Piperagkas. All rights reserved.
// 3-clause BSD License
//
//
#include<iostream>
#include <string>
#include <math.h>
#include <vector>
#include <stdlib.h>
#include <exception>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/core.hpp>
#include "imagergb.cpp"
#include "featurevector.cpp"
class classifier{
private:
int s,l,m,ll;
int sizen;
double **dists;
double **distse;
std::vector<imagergb*> base;
std::vector<featureVector*> fV;
std::string pathnew;
public:
classifier(const std::string path[], int nn )
{
sizen=nn;
fV.reserve(nn);
base.reserve(nn);
for (s=0;s<nn;s++)
{
imagergb* img= new imagergb(path, s );
base.push_back(img);
}
pathnew= "test.png"; //test image path
dists = new double*[3];
for (m=0;m<3;m++)
dists[m]= new double[nn];
distse = new double*[3];
for (m=0;m<3;m++)
distse[m]= new double[nn];
}
void calcfVec(){ //build a vector of feature vectors
try {
for(unsigned int ll=0; ll<base.size(); ll++)
{
base.at(ll)->produceDoG();
base.at(ll)->produceSDQG();
base.at(ll)->eliminateEdgeResp();
base.at(ll)->extremaLocalization();
featureVector* fVec= new featureVector(base.at(ll)->mins, base.at(ll)->maxs, base.at(ll)->mines, base.at(ll)->maxes, base.at(ll)->sizex, base.at(ll)->sizey);
fVec->buildfVector(base.at(ll)->mins, base.at(ll)->maxs, base.at(ll)->mines, base.at(ll)->maxes);
fV.push_back(fVec);
}
} catch (const std::exception& e) {
std::cerr << "exception calcfVec caught: " << e.what() << std::endl;
}
}
void calcDistances(){ //produce feature vector of new image and calculate euclidean distances
int c,ci,co;
int ssize;
double ddmin, ddmax, dmin, dmax, ff;
double tmp;
double tmp1, tmp2;
try {
imagergb* newimg = new imagergb(pathnew);
newimg->produceDoG();
newimg->produceSDQG();
newimg->eliminateEdgeResp();
newimg->extremaLocalization();
featureVector* newfV = new featureVector(newimg->mins, newimg->maxs, newimg->mines, newimg->maxes, newimg->sizex, newimg->sizey);
newfV->buildfVector(newimg->mins, newimg->maxs, newimg->mines, newimg->maxes);
newimg->printall();
co=0;
for(unsigned int ll=0; ll<fV.size(); ll++) //distances for rgb sift
{
ssize = fV.at(ll)->fsize;
if (newfV->fsize < ssize)
ssize = newfV->fsize;
for (c=0;c<3;c++)
{
dists[c][co]=0;
for (ci=0;ci<ssize;ci++)
{
ddmin = fV.at(ll)->fmins[c][ci];
ddmax = fV.at(ll)->fmaxs[c][ci];
dmin = newfV->fmins[c][ci];
dmax = newfV->fmaxs[c][ci];
if ((dmax+ddmax)==0)
{
tmp2=0;
if ((dmin+ddmin)!=0)
{
tmp1= std::abs(ddmin-dmin);
tmp= (0.5)*((tmp1*tmp1)/(ddmin+dmin));
}else{
tmp=0;
}
}
else if ((dmin+ddmin)==0)
{
tmp1=0;
if ((ddmax+dmax)!=0){
tmp2 = std::abs(ddmax-dmax);
tmp = (0.5)*((tmp2*tmp2)/(ddmax+dmax));
}else{
tmp=0;
}
}
else{
tmp1= std::abs(ddmin-dmin);
tmp2= std::abs(ddmax-dmax);
tmp = (0.5)*((tmp1*tmp1)/(ddmin+dmin))+(0.5)*((tmp2*tmp2)/(ddmax+dmax));
}
dists[c][co] = dists[c][co]+ tmp;
}
}
co++;
}
tmp=0;
tmp1=0;
tmp2=0;
co=0;
for(unsigned int ll=0; ll<fV.size(); ll++) //distances for csift
{
ssize = fV.at(ll)->fsizee;
if (newfV->fsizee < ssize)
ssize = newfV->fsizee;
for (c=0;c<3;c++)
{
distse[c][co]=0;
for (ci=0;ci<ssize;ci++)
{
ddmin = fV.at(ll)->fmines[c][ci];
ddmax = fV.at(ll)->fmaxes[c][ci];
dmin = newfV->fmines[c][ci];
dmax = newfV->fmaxes[c][ci];
if ((dmax+ddmax)==0)
{
tmp2=0;
if ((dmin+ddmin)!=0)
{
tmp1= std::abs(ddmin-dmin);
tmp= (0.5)*((tmp1*tmp1)/(ddmin+dmin));
}else{
tmp=0;
}
}
else if ((dmin+ddmin)==0)
{
tmp1=0;
if ((ddmax+dmax)!=0){
tmp2 = std::abs(ddmax-dmax);
tmp = (0.5)*((tmp2*tmp2)/(ddmax+dmax));
}else{
tmp=0;
}
}
else{
tmp1= std::abs(ddmin-dmin);
tmp2= std::abs(ddmax-dmax);
tmp = (0.5)*((tmp1*tmp1)/(ddmin+dmin))+(0.5)*((tmp2*tmp2)/(ddmax+dmax));
}
distse[c][co] = distse[c][co]+ tmp;
}
}
co++;
}
} catch (const std::exception& e) {
std::cerr << "exception calcDistances caught: " << e.what() << std::endl;
}
}
void printdists() // print distances of base images to new image
{
int c,co;
std::cout << "distances per color and image (rgb SIFT, 10 base images): " << std::endl;
std::cout << " "<< std::endl;
for (c=0;c<3;c++)
{
for (co=0;co<sizen;co++)
{
std::cout <<" " << dists[c][co];
}
std::cout <<" " <<std::endl;
}
std::cout << " "<< std::endl;
std::cout << "distances per Ex, Elx, Ellx and image (CSIFT - 10 base images):" <<std::endl;
std::cout << " "<< std::endl;
for (c=0;c<3;c++)
{
for (co=0;co<sizen;co++)
{
std::cout <<" " << distse[c][co];
}
std::cout <<" " <<std::endl;
}
}
~classifier()
{
fV.clear();
base.clear();
for (l=0;l<3;l++)
delete dists[l];
delete[] dists;
for (l=0;l<3;l++)
delete distse[l];
delete[] distse;
}
};