-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrainsSVM_plate.cpp
104 lines (78 loc) · 2.2 KB
/
TrainsSVM_plate.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
//NumberPlateRecognition using SVM
//TrainSVM.cpp
//Michal Půlpán
/*
standalone program for creating XML file used for training SVM.
*/
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
using namespace cv;
int main ( int argc, char** argv )
{
cout << "Creating XML for plate detection" << endl;
vector<string> args;
for (size_t i = 0; i < argc; i++)
{
cout << argv[i] << endl;
args.push_back(argv[i]);
}
string pathPlates;
string pathNoPlates;
int numPlates;
int numNoPlates;
int imageWidth=144;
int imageHeight=33;
if(argc >= 5){
numPlates= atoi(argv[1]);
numNoPlates= atoi(argv[2]);
pathPlates= argv[3];
pathNoPlates= argv[4];
} else {
cout << "no enough arguments" << endl;
}
Mat classes;
Mat trainingData;
Mat trainingImages;
vector<int> trainingLabels;
for(int i=1; i<= numPlates; i++)
{
stringstream ss(stringstream::in | stringstream::out);
ss << pathPlates << i << ".jpg";
Mat img=imread(ss.str());
if (img.empty()){
cout <<"emptyimg"<<endl;
return 0;
}
img= img.reshape(1, 1);
trainingImages.push_back(img);
trainingLabels.push_back(1);
//cout << "Plate was pushed" <<endl;
}
for(int i=1; i<= numNoPlates; i++)
{
stringstream ss(stringstream::in | stringstream::out);
ss << pathNoPlates << i << ".jpg";
Mat img=imread(ss.str());
if (img.empty()){
cout <<"emptyimg"<<endl;
cout<<"image: " << i <<endl;
return 0;
}
img= img.reshape(1, 1);
trainingImages.push_back(img);
trainingLabels.push_back(0);
//cout << "no_plate was pushed" <<endl;
}
Mat(trainingImages).copyTo(trainingData);
trainingData.convertTo(trainingData, CV_32F);
Mat(trainingLabels).copyTo(classes);
FileStorage fs("SVM.xml", FileStorage::WRITE);
fs << "TrainingData" << trainingData;
fs << "classes" << classes;
fs.release();
}