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_ if (response ==1) I.at //----------------------- 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 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 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 解释 百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库支持向量机的介绍(3)在线全文阅读。
相关推荐: