-
Notifications
You must be signed in to change notification settings - Fork 0
/
svm.h
78 lines (63 loc) · 2.1 KB
/
svm.h
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
#include<fstream>
using namespace std;
class svm
{
public:
svm(const vector<string> &_class_list) {}
// Nearest neighbor training. All this does is read in all the images, resize
// them to a common size, convert to greyscale, and dump them as vectors to a file
virtual void svmtrain(const Dataset &filenames)
{
ofstream myfile;
myfile.open ("train.dat");
int k=1;
for(Dataset::const_iterator c_iter=filenames.begin(); c_iter != filenames.end(); ++c_iter)
{
cout << "Processing " << c_iter->first << endl;
CImg<double> class_vectors(size*size*3, filenames.size(), 1);
// convert each image to be a row of this "model" image
for(int i=0; i<c_iter->second.size(); i++)
{
class_vectors = (CImg<double>(c_iter->second[i].c_str())).resize(size,size,1,3).unroll('x');
int ind=1;
myfile<<k<<" ";
for(CImg<double>::iterator it=class_vectors.begin()+1;it!=class_vectors.end();it++)
{
myfile<<ind<<":"<<*it<<" ";
ind++;
}
myfile<<"\n";
}
k++;
}
}
virtual void svmtest(const Dataset &filenames)
{
ofstream myfile;
myfile.open ("test.dat");
int k=1;
for(Dataset::const_iterator c_iter=filenames.begin(); c_iter != filenames.end(); ++c_iter)
{
cout << "Processing " << c_iter->first << endl;
CImg<double> class_vectors(size*size*3, filenames.size(), 1);
// convert each image to be a row of this "model" image
for(int i=0; i<c_iter->second.size(); i++)
{
class_vectors = (CImg<double>(c_iter->second[i].c_str())).resize(size,size,1,3).unroll('x');
int ind=1;
myfile<<k<<" ";
for(CImg<double>::iterator it=class_vectors.begin()+1;it!=class_vectors.end();it++)
{
myfile<<ind<<":"<<*it<<" ";
ind++;
}
myfile<<"\n";
}
k++;
}
}
protected:
static const int size=40; // subsampled image resolution
map<string, CImg<double> > models; // trained models
vector<string> class_list;
};