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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
#include "opencv2/imgcodecs.hpp" #include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc.hpp> #include <opencv2/ml/ml.hpp> using namespace cv::ml;
#define NTRAINING_SAMPLES 100 #define FRAC_LINEAR_SEP 0.9f
using namespace cv; using namespace std;
int main() { const int WIDTH = 512, HEIGHT = 512; Mat I = Mat::zeros(HEIGHT, WIDTH, CV_8UC3); Mat trainData(2 * NTRAINING_SAMPLES, 2, CV_32FC1); Mat labels(2 * NTRAINING_SAMPLES, 1, CV_32SC1);
RNG rng(100);
int nLinearSamples = (int)(FRAC_LINEAR_SEP * NTRAINING_SAMPLES);
Mat trainClass = trainData.rowRange(0, nLinearSamples); Mat c = trainClass.colRange(0, 1); rng.fill(c, RNG::UNIFORM, Scalar(1), Scalar(0.4 * WIDTH)); c = trainClass.colRange(1, 2); rng.fill(c, RNG::UNIFORM, Scalar(1), Scalar(HEIGHT));
trainClass = trainData.rowRange(2 * NTRAINING_SAMPLES - nLinearSamples, 2 * NTRAINING_SAMPLES); c = trainClass.colRange(0, 1); rng.fill(c, RNG::UNIFORM, Scalar(0.6 * WIDTH), Scalar(WIDTH)); c = trainClass.colRange(1, 2); rng.fill(c, RNG::UNIFORM, Scalar(1), Scalar(HEIGHT));
trainClass = trainData.rowRange(nLinearSamples, 2 * NTRAINING_SAMPLES - nLinearSamples); c = trainClass.colRange(0, 1); rng.fill(c, RNG::UNIFORM, Scalar(0.4 * WIDTH), Scalar(0.6 * WIDTH)); c = trainClass.colRange(1, 2); rng.fill(c, RNG::UNIFORM, Scalar(1), Scalar(HEIGHT));
labels.rowRange(0, NTRAINING_SAMPLES).setTo(1); labels.rowRange(NTRAINING_SAMPLES, 2 * NTRAINING_SAMPLES).setTo(2);
Ptr<ml::SVM> svm = ml::SVM::create(); svm->setC(0.1); svm->setType(SVM::C_SVC); svm->setKernel(SVM::LINEAR); svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER,(int)1e7,1e-6));
cout << "Starting training process" << endl; Ptr<ml::TrainData> tdata = ml::TrainData::create(trainData, ROW_SAMPLE,labels); svm->train(tdata); cout << "Finished training process" << endl;
Vec3b green(0, 100, 0), blue(100, 0, 0); for (int i = 0; i < I.rows; ++i) for (int j = 0; j < I.cols; ++j) { Mat sampleMat = (Mat_<float>(1, 2) << i, j); float response = svm->predict(sampleMat);
if (response == 1) I.at<Vec3b>(j, i) = green; else if (response == 2) I.at<Vec3b>(j, i) = blue; }
int thick = -1; int lineType = 8; float px, py; for (int i = 0; i < NTRAINING_SAMPLES; ++i) { px = trainData.at<float>(i, 0); py = trainData.at<float>(i, 1); circle(I, Point((int)px, (int)py), 3, Scalar(0, 255, 0), thick, lineType); } for (int i = NTRAINING_SAMPLES; i < 2 * NTRAINING_SAMPLES; ++i) { px = trainData.at<float>(i, 0); py = trainData.at<float>(i, 1); circle(I, Point((int)px, (int)py), 3, Scalar(255, 0, 0), thick, lineType); }
thick = 2; lineType = 8; Mat sv = svm->getSupportVectors();
for (int i = 0; i < sv.rows; ++i) { const float *v = sv.ptr<float>(i); circle(I, Point((int)v[ 0 ], (int)v[ 1 ]), 6, Scalar(128, 128, 128), thick, lineType); }
imwrite("result.png", I); imshow("SVM for Non-Linear Training Data", I); waitKey(0); }
|