In opencv3.0 version, the implementation of the normal Bayesian classifier (normal Bayes Classifier) Classification instance
#include"stdafx.h"#include"opencv2/opencv.hpp"using namespaceCV;using namespacecv::ml;intMainint,Char**){ intwidth = +, height = +; Mat Image= Mat::zeros (height, width, cv_8uc3);//Create a window visualization//set up training data intlabels[Ten] = {1, -1,1,1,-1,1,-1,1,-1,-1 }; Mat Labelsmat (Ten,1, CV_32SC1, labels); floattrainingdata[Ten][2] = { {501, Max}, {255,Ten}, {501,255}, {Ten,501}, { -, the }, { Max, -}, { the, $} , { -, -} , { $, -} , { $, $ } }; Mat Trainingdatamat (Ten,2, CV_32FC1, Trainingdata); //creating a Bayesian classifierPtr<normalbayesclassifier> model=normalbayesclassifier::create (); //set up training dataptr<traindata> Tdata =traindata::create (Trainingdatamat, Row_sample, Labelsmat); //Training ClassifierModel->train (Tdata); VEC3B Green (0,255,0), Blue (255,0,0); //Show The decision regions given by the SVM for(inti =0; i < image.rows; ++i) for(intj =0; J < Image.cols; ++j) {Mat Samplemat= (mat_<float> (1,2) << J, I);//Generate test Data floatResponse = Model->predict (Samplemat);//make predictions, return 1 or 1 if(Response = =1) image.at<Vec3b> (i, j) =Green; Else if(Response = =-1) image.at<Vec3b> (i, j) =Blue; } //Show Training Data intThickness =-1; intLinetype =8; Scalar C1= Scalar::all (0);//display marked as 1 as black dotsScalar C2 = Scalar::all (255);//marked as-1 of the display as white dots//when drawing, the first width after the high, corresponding to the first column and then for(inti =0; i < labelsmat.rows; i++) { Const float* v = trainingdatamat.ptr<float> (i);//Remove the head pointer for each linePoint pt = Point (int) v[0], (int) v[1]); if(Labels[i] = =1) Circle (Image, PT,5, C1, thickness, linetype); ElseCircle (Image, PT,5, c2, thickness, linetype); } imshow ("normal Bayessian classifier simple Example", image);//Show it to the userWaitkey (0);}
If you convert the data to a pixel value of a picture, you can classify the picture.
Realization of machine learning in Opencv3: using normal Bayesian classification