#include <cv.h>
#include <highgui.h>
#include <ml.h>
#include <time.h>
int main (int argc, char **argv)
{
const int s = 1000; //定义1000个点
int size = 400;
int i, j, sv_num;
IplImage *img;
CvSVM svm = CvSVM ();
CvSVMParams param;
CvTermCriteria criteria;
CvRNG rng = cvRNG (time (NULL));
CvPoint pts[s];
float data[s * 2];
int res[s];
CvMat data_mat, res_mat;
CvScalar rcolor;
const float *support;
// (1)画像領域の確保と初期化
img = cvCreateImage (cvSize (size, size), IPL_DEPTH_8U, 3);
cvZero (img);
// (2)学習データの生成
for (i = 0; i < s; i++) {
pts[i].x = cvRandInt (&rng) % size;
pts[i].y = cvRandInt (&rng) % size;
if (pts[i].y > 50 * cos (pts[i].x * CV_PI / 100) + 200) {
cvLine (img, cvPoint (pts[i].x - 2, pts[i].y - 2), cvPoint (pts[i].x + 2, pts[i].y + 2), CV_RGB (255, 0, 0));
cvLine (img, cvPoint (pts[i].x + 2, pts[i].y - 2), cvPoint (pts[i].x - 2, pts[i].y + 2), CV_RGB (255, 0, 0));
res[i] = 1;
}
else {
if (pts[i].x > 200) {
cvLine (img, cvPoint (pts[i].x - 2, pts[i].y - 2), cvPoint (pts[i].x + 2, pts[i].y + 2), CV_RGB (0, 255, 0));
cvLine (img, cvPoint (pts[i].x + 2, pts[i].y - 2), cvPoint (pts[i].x - 2, pts[i].y + 2), CV_RGB (0, 255, 0));
res[i] = 2;
}
else {
cvLine (img, cvPoint (pts[i].x - 2, pts[i].y - 2), cvPoint (pts[i].x + 2, pts[i].y + 2), CV_RGB (0, 0, 255));
cvLine (img, cvPoint (pts[i].x + 2, pts[i].y - 2), cvPoint (pts[i].x - 2, pts[i].y + 2), CV_RGB (0, 0, 255));
res[i] = 3;
}
}
}
// (2)训练数据的生成
//生成2维随机训练数据,并将其值放在CvPoint数据类型的数组pts[]中。
// (3)学习数据的显示
// (3)学習データの表示
cvNamedWindow ("SVM", CV_WINDOW_AUTOSIZE);
cvShowImage ("SVM", img);
cvWaitKey (0);
// (4)学习参数的生成
// (4)学習パラメータの生成
for (i = 0; i < s; i++) {
data[i * 2] = float (pts[i].x) / size; //size=400 图像的长度和宽度
data[i * 2 + 1] = float (pts[i].y) / size;
}
cvInitMatHeader (&data_mat, s, 2, CV_32FC1, data);
cvInitMatHeader (&res_mat, s, 1, CV_32SC1, res);
criteria = cvTermCriteria (CV_TERMCRIT_EPS, 1000, FLT_EPSILON);
param = CvSVMParams (CvSVM::C_SVC, CvSVM::RBF, 10.0, 8.0, 1.0, 10.0, 0.5, 0.1, NULL, criteria);
/* SVM种类:CvSVM::C_SVC
Kernel的种类:CvSVM::RBF
degree:10.0(此次不使用)
gamma:8.0
coef0:1.0(此次不使用)
C:10.0
nu:0.5(此次不使用)
p:0.1(此次不使用)
成员变量degree针对多项式核函数degree的设置,
gamma针对多项式/rbf/sigmoid核函数的设置,
coef0针对多项式/sigmoid核函数的设置,
Cvalue为损失函数,在C-SVC、e-SVR、v-SVR中有效,
nu设置v-SVC、一类SVM和v-SVR参数,
p为设置e-SVR中损失函数的值,class_weightsC_SVC的权重,
term_crit为SVM训练过程的终止条件。
其中默认值degree = 0,gamma = 1,coef0 = 0,Cvalue = 1,nu = 0,p = 0,class_weights = 0
然后对训练数据正规化处理,并放在CvMat型的数组里。*/
// (5)SVMの学習
svm.train (&data_mat, &res_mat, NULL, NULL, param);
//调用CvSVM::train函数建立SVM模型,第一个参数为训练数据,第二个参数为分类结果,最后一个参数即CvSVMParams
// (6)学習結果の描画 学习结果的绘图
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
CvMat m;
float ret = 0.0;
float a[] = { float (j) / size, float (i) / size };
cvInitMatHeader (&m, 1, 2, CV_32FC1, a);
ret = svm.predict (&m); //调用函数CvSVM::predict实现分类
switch ((int) ret) {
case 1:
rcolor = CV_RGB (100, 0, 0);
break;
case 2:
rcolor = CV_RGB (0, 100, 0);
break;
case 3:
rcolor = CV_RGB (0, 0, 100);
break;
}
cvSet2D (img, i, j, rcolor);
}
}
// (7)トレーニングデータの再描画
for (i = 0; i < s; i++) {
CvScalar rcolor;
switch (res[i]) {
case 1:
rcolor = CV_RGB (255, 0, 0);
break;
case 2:
rcolor = CV_RGB (0, 255, 0);
break;
case 3:
rcolor = CV_RGB (0, 0, 255);
break;
}
cvLine (img, cvPoint (pts[i].x - 2, pts[i].y - 2), cvPoint (pts[i].x + 2, pts[i].y + 2), rcolor);
cvLine (img, cvPoint (pts[i].x + 2, pts[i].y - 2), cvPoint (pts[i].x - 2, pts[i].y + 2), rcolor);
}
// (8)サポートベクターの描画
sv_num = svm.get_support_vector_count ();
for (i = 0; i < sv_num; i++) {
support = svm.get_support_vector (i);
cvCircle (img, cvPoint ((int) (support[0] * size), (int) (support[1] * size)), 5, CV_RGB (200, 200, 200));
}
// (9)画像の表示
cvNamedWindow ("SVM", CV_WINDOW_AUTOSIZE);
cvShowImage ("SVM", img);
cvWaitKey (0);
cvDestroyWindow ("SVM");
cvReleaseImage (&img);
return 0;
}