码迷,mamicode.com
首页 > 其他好文 > 详细

OpenCV——ANN神经网络

时间:2016-11-04 20:44:37      阅读:558      评论:0      收藏:0      [点我收藏+]

标签:open   oat   user   ict   方法   imshow   response   int   poi   

ANN—— Artificial Neural Networks 人工神经网络

//定义人工神经网络
    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.;

两种训练方法:BACKPROP 与 RPROP

BACKPROP的两个参数:

技术分享

RPROP的四个参数:

技术分享

 

//  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);
// layerSizes设置了有三个隐含层的网络结构:输入层,三个隐含层,输出层。输入层和输出层节点数均为5,中间隐含层每层有两个节点

    Mat layerSizes=(Mat_<int>(1,5) << 5,2,2,2,5);

//create第二个参数可以设置每个神经节点的激活函数,默认为CvANN_MLP::SIGMOID_SYM,即Sigmoid函数
//同时提供的其他激活函数有Gauss(CvANN_mlp::GAUSSIAN)和阶跃函数(CvANN_MLP::IDENTITY)。
 bp.create(layerSizes,CvANN_MLP::SIGMOID_SYM);   //CvANN_MLP::SIGMOID_SYM  
bp.train(trainingDataMat, labelsMat, Mat(),Mat(), params);
//预测新节点
Mat sampleMat = (Mat_<float>(1,5) << i,j,0,0,0);  
            Mat responseMat;  
            bp.predict(sampleMat,responseMat);  

完整代码:

#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()  
{  
    CvANN_MLP bp;   
    
    CvANN_MLP_TrainParams params;  
    params.train_method=CvANN_MLP_TrainParams::BACKPROP;  //(Back Propagation,BP)反向传播算法
    params.bp_dw_scale=0.1;  
    params.bp_moment_scale=0.1;  

    float labels[10][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}};  
    //这里对于样本标记为0.1和0.9而非0和1,主要是考虑到sigmoid函数的输出为一般为0和1之间的数,只有在输入趋近于-∞和+∞才逐渐趋近于0和1,而不可能达到。
    Mat labelsMat(10, 2, CV_32FC1, labels);  

    float trainingData[10][2] = { {11,12},{111,112}, {21,22}, {211,212},{51,32}, {71,42}, {441,412},{311,312}, {41,62}, {81,52} };  
    Mat trainingDataMat(10, 2, CV_32FC1, trainingData);  
    Mat layerSizes=(Mat_<int>(1,5) << 2, 2, 2, 2, 2);                   //5层:输入层,3层隐藏层和输出层,每层均为两个perceptron
    bp.create(layerSizes,CvANN_MLP::SIGMOID_SYM);
    bp.train(trainingDataMat, labelsMat, Mat(),Mat(), params);  
    int width = 512, height = 512;  
    Mat image = Mat::zeros(height, width, CV_8UC3);  
    Vec3b green(0,255,0), blue (255,0,0);  

    for (int i = 0; i < image.rows; ++i)
    {
        for (int j = 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  
    int thickness = -1;  
    int lineType = 8;  
    circle( image, Point(111,  112), 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(11,  12), 5, Scalar(255, 255, 255), thickness, lineType);  
    circle( image, Point(21, 22), 5, Scalar(255, 255, 255), thickness, lineType);       
    circle( image, Point(51,  32), 5, Scalar(255, 255, 255), thickness, lineType);  
    circle( image, Point(71, 42), 5, Scalar(255, 255, 255), thickness, lineType);       
    circle( image, Point(41,  62), 5, Scalar(255, 255, 255), thickness, lineType);  
    circle( image, Point(81, 52), 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);  

    return 0;
}  

 

OpenCV——ANN神经网络

标签:open   oat   user   ict   方法   imshow   response   int   poi   

原文地址:http://www.cnblogs.com/farewell-farewell/p/6027888.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!