Opencv--ann Neural Network

Source: Internet
Author: User

Ann--artificial Neural Networks Artificial Neural network

//defining artificial Neural networksCVANN_MLP BP; //Set up bpnetwork ' s parametersCvann_mlp_trainparamsparams; 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 =.;

Two methods of training: Backprop and Rprop

Two parameters of Backprop:

Four parameters of Rprop:

//Training Data    floatlabels[3][5] = {{0,0,0,0,0},{1,1,1,1,1},{0,0,0,0,0}}; Mat Labelsmat (3,5, CV_32FC1, labels); floattrainingdata[3][5] = { {1,2,3,4,5},{111, the,113, the, the}, { +, A, at, -, -} }; Mat Trainingdatamat (3,5, CV_32FC1, trainingdata);//layersizes set up a network structure with three hidden layers: input layer, three hidden layer, output layer. Both input and output layer nodes are 5, with two nodes per layer in the middle of the hidden layerMat layersizes= (mat_<int> (1,5) <<5,2,2,2,5);//Create the second parameter can set the activation function of each nerve node, the default is Cvann_mlp::sigmoid_sym, that is, the SIGMOID function//Other activation functions provided are Gauss (Cvann_mlp::gaussian) and step Functions (cvann_mlp::identity). 
Bp.create (layersizes,cvann_mlp::sigmoid_sym);   //
params);
// predicting new nodes Mat Samplemat = (mat_<float> (1,5) << i,j,0,0,0 );              Mat Responsemat;              Bp.predict (Samplemat,responsemat);  

Full code:

#include <opencv2/core/core.hpp>#include<opencv2/highgui/highgui.hpp>#include<opencv2/ml/ml.hpp>#include<iostream>#include<string>using namespacestd; using namespaceCV; intMain () {CVANN_MLP BP; Cvann_mlp_trainparamsparams; params. Train_method=cvann_mlp_trainparams::backprop;//(back PROPAGATION,BP) reverse propagation algorithm    params. bp_dw_scale=0.1; params. bp_moment_scale=0.1;     floatlabels[Ten][2] = {{0.9,0.1},{0.1,0.9},{0.9,0.1},{0.1,0.9},{0.9,0.1},{0.9,0.1},{0.1,0.9},{0.1,0.9},{0.9,0.1},{0.9,0.1}}; //Here the sample is labeled 0.1 and 0.9 instead of 0 and 1, mainly considering that the output of the sigmoid function is usually between 0 and 1, and only when the input approaches-∞ and +∞ is gradually approaching 0 and 1, and it is impossible to achieve. Mat Labelsmat (Ten,2, CV_32FC1, labels); floattrainingdata[Ten][2] = { { One, A},{111, the}, { +, A}, {211,212},{Wuyi, +}, { in, the}, {441,412},{311,312}, { A, +}, {Bayi, the} }; Mat Trainingdatamat (Ten,2, CV_32FC1, Trainingdata); Mat layersizes= (mat_<int> (1,5) <<2,2,2,2,2);//Layer 5: Input layer, 3 layer hidden layer and output layer, each layer is two perceptronBp.create (LAYERSIZES,CVANN_MLP::SIGMOID_SYM); Bp.train (Trainingdatamat, Labelsmat, Mat (), Mat (),params);intwidth = +, height = +; Mat Image=Mat::zeros (height, width, cv_8uc3); VEC3B Green (0,255,0), Blue (255,0,0);      for(inti =0; i < image.rows; ++i) { for(intj =0; J < Image.cols; ++j) {Mat Samplemat= (mat_<float> (1,2) <<i,j);              Mat Responsemat;              Bp.predict (Samplemat,responsemat); float* p=responsemat.ptr<float> (0); //            if(p[0] > p[1]) {image.at<Vec3b> (j, i) =Green; }             Else{image.at<Vec3b> (j, i) =Blue; }        }      }    //Show the training data    intThickness =-1; intLinetype =8; Circle (Image, point (111, the),5, Scalar (0,0,0), thickness, linetype); Circle (Image, point (211,212),5, Scalar (0,0,0), thickness, linetype); Circle (Image, point (441,412),5, Scalar (0,0,0), thickness, linetype); Circle (Image, point (311,312),5, Scalar (0,0,0), thickness, linetype); Circle (Image, point ( One, A),5, Scalar (255,255,255), thickness, linetype); Circle (Image, point ( +, A),5, Scalar (255,255,255), thickness, linetype); Circle (Image, point (Wuyi, +),5, Scalar (255,255,255), thickness, linetype); Circle (Image, point ( in, the),5, Scalar (255,255,255), thickness, linetype); Circle (Image, point ( A, +),5, Scalar (255,255,255), thickness, linetype); Circle (Image, point (Bayi, the),5, Scalar (255,255,255), thickness, linetype); Imwrite ("Result.png", image);//Save the imageImshow ("BP Simple Example", image);//Show it to the userWaitkey (0); return 0;} 

Opencv--ann Neural Network

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.