支持向量机的介绍(3)

2019-03-23 12:09

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

---------------------------------------

Mat trainData(2*NTRAINING_SAMPLES, 2, CV_32FC1); Mat labels (2*NTRAINING_SAMPLES, 1, CV_32FC1);

RNG rng(100); // Random value generation class

// Set up the linearly separable part of the training data intnLinearSamples= (int) (FRAC_LINEAR_SEP * NTRAINING_SAMPLES);

// Generate random points for the class 1

Mat trainClass=trainData.rowRange(0, nLinearSamples); // The x coordinate of the points is in [0, 0.4) Mat c =trainClass.colRange(0, 1);

rng.fill(c, RNG::UNIFORM, Scalar(1), Scalar(0.4* WIDTH)); // The y coordinate of the points is in [0, 1) c =trainClass.colRange(1,2);

rng.fill(c, RNG::UNIFORM, Scalar(1), Scalar(HEIGHT));

// Generate random points for the class 2

trainClass=trainData.rowRange(2*NTRAINING_SAMPLES-nLinearSamples, 2*NTRAINING_SAMPLES);

// The x coordinate of the points is in [0.6, 1] c =trainClass.colRange(0 , 1);

rng.fill(c, RNG::UNIFORM, Scalar(0.6*WIDTH), Scalar(WIDTH)); // The y coordinate of the points is in [0, 1) c =trainClass.colRange(1,2);

rng.fill(c, RNG::UNIFORM, Scalar(1), Scalar(HEIGHT));

//------------------ Set up the non-linearly separable part of the training data ---------------

// Generate random points for the classes 1 and 2 trainClass=trainData.rowRange( nLinearSamples, 2*NTRAINING_SAMPLES-nLinearSamples);

// The x coordinate of the points is in [0.4, 0.6)

c =trainClass.colRange(0,1);

rng.fill(c, RNG::UNIFORM, Scalar(0.4*WIDTH), Scalar(0.6*WIDTH)); // The y coordinate of the points is in [0, 1) c =trainClass.colRange(1,2);

rng.fill(c, RNG::UNIFORM, Scalar(1), Scalar(HEIGHT));

//------------------------- Set up the labels for the classes --------------------------------- labels.rowRange( 0,

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

NTRAINING_SAMPLES).setTo(1); // Class 1 labels.rowRange(NTRAINING_SAMPLES,

2*NTRAINING_SAMPLES).setTo(2); // Class 2

//------------------------ 2. Set up the support vector machines parameters --------------------

CvSVMParamsparams;

params.svm_type= SVM::C_SVC; params.C=0.1;

params.kernel_type= SVM::LINEAR;

params.term_crit=TermCriteria(CV_TERMCRIT_ITER, (int)1e7, 1e-6);

//------------------------ 3. Train the svm ---------------------------------------------------- cout<<\<

svm.train(trainData, labels, Mat(), Mat(), params); cout<<\<

//------------------------ 4. Show the decision regions ----------------------------------------

Vec3b green(0,100,0), blue (100,0,0); for (int i =0; i

Mat sampleMat= (Mat_(1,2) << i, j); float response =svm.predict(sampleMat);

if (response ==1) I.at(j, i) = green; elseif (response ==2) I.at(j, i) = blue; }

//----------------------- 5. Show the training data --------------------------------------------

int thick =-1; intlineType=8; floatpx, py; // Class 1

for (int i =0; i < NTRAINING_SAMPLES; ++i) {

px= trainData.at(i,0); py= trainData.at(i,1);

circle(I, Point( (int) px, (int) py ), 3, Scalar(0, 255, 0), thick, lineType);

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

}

// Class 2

for (int i = NTRAINING_SAMPLES; i <2*NTRAINING_SAMPLES; ++i) {

px= trainData.at(i,0); py= trainData.at(i,1);

circle(I, Point( (int) px, (int) py ), 3, Scalar(255, 0, 0), thick, lineType); }

//------------------------- 6. Show support vectors --------------------------------------------

thick =2; lineType=8;

int x =svm.get_support_vector_count();

for (int i =0; i < x; ++i) {

constfloat* v =svm.get_support_vector(i); circle( I, Point( (int) v[0], (int) v[1]), 6, Scalar(128, 128, 128), thick, lineType); }

imwrite(\, I); // save the Image imshow(\, I); // show it to the user

waitKey(0); }

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

解释


支持向量机的介绍(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:SDS-PAGE电泳实验步骤

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: