Realization of machine learning in Opencv3: using normal Bayesian classification

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.