-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsvm.cpp
88 lines (66 loc) · 2.16 KB
/
svm.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
/*
*http://www.csie.ntu.edu.tw/~cjlin/papers/libsvm.pdf
*http://docs.opencv.org/modules/ml/doc/support_vector_machines.html#cvsvm
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <opencv2/opencv.hpp>
//#include <opencv2/nonfree/features2d.hpp>
#include <opencv2/ml/ml.hpp>
#define DEBUG
const int imgNo = 25;
const int trainNo = 20;
const int testNo = 5;
int main(int argc, char **argv) {
//CvSVM::train 要求样本数据存储在float 类型的Mat中
cv::Mat feature;
for (int i = 0; i < imgNo; i++) {
std::stringstream index;
index << i;
std::string fileName = "hogData/" + index.str() + ".png";
#ifdef DEBUG
std::cout << fileName << std::endl;
#endif
cv::Mat img = cv::imread(fileName);
if (img.empty()) {
std::cerr << "no image..." << std::endl;
exit(1);
}
cv::Mat gray;
cv::cvtColor(img, gray, CV_RGB2GRAY);
//cv::HOGDescriptor hog(cv::Size(64, 64), cv::Size(16, 16), cv::Size(8,8), cv::Size(8, 8), 9);
cv::HOGDescriptor hog;
std::vector<float> desc;
std::vector<cv::Point> loc;
hog.compute(gray, desc, cv::Size(0, 0), cv::Size(0, 0), loc);
#ifdef DEBUG
std::cout << "hog descriptor size is " << hog.getDescriptorSize() << std::endl;
#endif
cv::Mat row = cv::Mat(desc, true).reshape(1, 1);
if (feature.empty()) {
feature = row.clone();
}
else {
feature.push_back(row);
}
}
#ifdef DEBUG
std::cout << "feature size: " << feature.size() << std::endl;
#endif
float lb[trainNo] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
cv::Mat labels = cv::Mat(trainNo, 1, CV_32FC1, lb);
cv::SVMParams params;
params.svm_type = cv::SVM::C_SVC;
params.kernel_type = cv::SVM::LINEAR;
params.term_crit = cv::TermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);
cv::SVM svm;
svm.train(feature(cv::Range(0, trainNo), cv::Range::all()), labels, cv::Mat(), cv::Mat(), params);
std::cout << "predict result: " << std::endl;
std::cout << svm.predict(feature.row(20)) << std::endl;
std::cout << svm.predict(feature.row(21)) << std::endl;
std::cout << svm.predict(feature.row(22)) << std::endl;
std::cout << svm.predict(feature.row(23)) << std::endl;
std::cout << svm.predict(feature.row(24)) << std::endl;
return 0;
}