The use of neural network in "pattern recognition" OpenCV CVANN_MLP_OPENCV

Source: Internet
Author: User
Tags scalar
The OpenCV ml module implements the most typical multilayer perceptron (multi-layer perceptrons, MLP) model of the Artificial neural network (Artificial neural Networks, ANN). Since the algorithm implemented by ML model inherits from the unified Cvstatmodel base class, its training and prediction interfaces are train (), predict (), very simple.

Here's a look at the use of neural network CVANN_MLP to define neural networks and parameters:

[cpp]  View Plain copy//setup the bpnetwork       CvANN_MLP  bp;        // set up bpnetwork ' s parameters       CvANN_MLP_TrainParams params;       params.train_ method=cvann_mlp_trainparams::backprop;       params.bp_dw_scale=0.1;       params.bp_moment_scale=0.1;       //params.train_method= cvann_mlp_trainparams::rprop;       //params.rp_dw0 = 0.1;         //params.rp_dw_plus = 1.2;        // params.rp_dw_minus = 0.5;       //params.rp_dw_min = flt_ epsilon;        //params.rp_dw_max = 50.;   

You can define the CVANN_MLP neural network directly and set its parameters. Backprop represents the use of back-propagation training methods, Rprop is the simplest propagation training methods.

There are two related parameters for using Backprop: Bp_dw_scale is Bp_moment_scale:


The use of Prpop has four related parameters: Rp_dw0, Rp_dw_plus, Rp_dw_minus, Rp_dw_min, Rp_dw_max:


The default value is in the code above. Set up network layer, training data:

[cpp]  View Plain Copy// set up training data       float  labels[3][5] = {{0,0,0,0,0},{1,1,1,1,1},{0,0,0,0,0}};       mat  labelsmat (3, 5, cv_32fc1, labels);          float  trainingData[3][5] = { {1,2,3,4,5},{111,112,113,114,115}, {21,22,23,24,25} };        mat trainingdatamat (3, 5, cv_32fc1, trainingdata);        mat layersizes= (mat_<int> (1,5)  << 5,2,2,2,5);        bp.create (layersizes,cvann_mlp::sigmoid_sym);//cvann_mlp::sigmoid_sym                                                     //cvann_mlp::gaussian                                                     //CvANN_MLP::IDENTITY        Bp.train (Trainingdatamat, labelsmat, mat (), Mat (),  params);  
Layersizes has three hidden layers of network structure: input layer, three hidden layer, output layer. The input layer and output layer nodes are all 5, and the middle hidden layer has two nodes per layer.

Create the second parameter can set the activation function of each neural node, default is Cvann_mlp::sigmoid_sym, that is, the sigmoid function, and the other activation functions provided are Gauss and step functions.


Use a trained network structure to classify new data:

You can then use the Predict function directly to predict the new node:

[CPP] View plain copy Mat Samplemat = (mat_<float> (1,5) << i,j,0,0,0);               Mat Responsemat; Bp.predict (Samplemat,responsemat);
Full program code:

[cpp]  View Plain copy//the example of using bpnetwork in opencv  // coded by l. wei   #include  <opencv2/core/core.hpp>   #include  <opencv2/highgui/highgui.hpp>   #include  <opencv2/ml/ml.hpp>   # include <iostream>   #include  <string>      using  namespace std;   using namespace cv;      int main ()     {       //Setup the BPNetwork        cvann_mlp bp;        // set up bpnetwork ' s  parameters       CvANN_MLP_TrainParams params;        params.train_method=CvANN_MLP_TrainParams::BACKPROP;       params.bp_dw_ Scale=0.1; &nbSp     params.bp_moment_scale=0.1;       //params.train_method= cvann_mlp_trainparams::rprop;       //params.rp_dw0 = 0.1;         //params.rp_dw_plus = 1.2;        // params.rp_dw_minus = 0.5;       //params.rp_dw_min = flt_ epsilon;        //params.rp_dw_max = 50.;           // Set up training data        float labels[3][5] = {{0,0,0,0,0},{1,1,1,1,1},{0,0,0,0,0}};        mat labelsmat (3, 5, cv_32fc1, labels);           float trainingdata[3][5] = { {1,2,3,4,5},{111,112,113,114,115}, { 21,22,23,24,25} }; &nbSp     mat trainingdatamat (3, 5, cv_32fc1, trainingdata);       mat layersizes= (mat_<int> (1,5)  << 5,2,2,2,5);       bp.create (Layersizes,cvann_mlp::sigmoid_sym);//cvann_mlp::sigmoid_sym                                                    //CvANN_MLP::GAUSSIAN                                                     //CvANN_MLP::IDENTITY        bp.train (TrainingdAtamat, labelsmat, mat (), Mat (),  params);              // Data for visual representation       int width  = 512, height = 512;       Mat image =  Mat::zeros (HEIGHT,&NBSP;WIDTH,&NBSP;CV_8UC3);       vec3b green (0,255,0),  blue  (255,0,0);       // show the decision regions  given by the SVM       for  (int i = 0;  i < image.rows; ++i)            for   (int j = 0; j < image.cols; ++j)             {                Mat samplemat =  (mat_<float> (1,5)  << i,j,0,0,0);                Mat responseMat;                bp.predict (Samplemat,responsemat);                float* p=responseMat.ptr<float> (0 );               float response=0.0f ;               for (int k=0;k<5;i + +) {               //  cout< <p[k]<< " ";                    response+=p[k];                }               if  (response >2 )                     Image.at<vec3b> (j, i)   = green;                else                      image.at<Vec3b> (j, i)   = blue;            }               // Show the training data            int thickness = -1;            int lineType = 8;           circle ( image, pOint (501,  10),  5, scalar (  0,   0,   0),  Thickness, linetype);           circle ( image,  point (255,  10),  5, scalar (255, 255, 255),  thickness, linetype);            circle ( image, point (501, 255),  5, scalar (255, 255, 255),  thickness, linetype);            circle ( image, point ( 10, 501),  5, Scalar (255,  255, 255),  thickness, linetype);               imwrite ("Result.png",  image);        //  save the image               imshow (" Bp simple examplE ",  image); // show it to the user            waitkey (0);     }  
Results:




(reprint please indicate author and origin: Http://blog.csdn.net/xiaowei_cqu do not use for commercial use without permission)

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.