#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ml/ml.hpp>
#include <iostream>

using namespace std;
using namespace cv;

int main()
{
CvANN_MLP bp; //bp网络
CvANN_MLP_TrainParams params; //bp网络参数
params.train_method = CvANN_MLP_TrainParams::BACKPROP;//使用简单的BP算法,还可使用RPROP
params.bp_dw_scale = 0.1;
params.bp_moment_scale = 0.1;

float labels[4][2] = { { 0,0 },{ 0,0 },{ 1,1 },{ 1,1 } }; //训练标签数据,前两个表示男生,后两个表示女生
Mat labelsMat(4, 2, CV_32FC1, labels);

float trainingData[4][2] = { {186,80},{185,81},{160,50},{161,48} }; //训练数据,两个维度,表示身高和体重
Mat trainingDataMat(4, 2, CV_32FC1, trainingData);

Mat layerSizes = (Mat_<int>(1, 4) << 2, 2, 2, 2);//含有两个隐含层的网络结构,输入、输出层各两个节点,每个隐含层含两个节点
bp.create(layerSizes, CvANN_MLP::SIGMOID_SYM);//激活函数为SIGMOID函数,还可使用高斯函数(CvANN_MLP::GAUSSIAN),阶跃函数(CvANN_MLP::IDENTITY)
bp.train(trainingDataMat, labelsMat, Mat(), Mat(), params);

//bp.save("bp.xml");//存储模型
//bp.load("bp.xml");//读取模型

Mat sampleMat = (Mat_<float>(1, 2) << 184, 79); //测试数据,为一男生
Mat responseMat;
bp.predict(sampleMat, responseMat);
Point maxLoc;
minMaxLoc(responseMat, NULL, NULL, NULL, &maxLoc); //response为1行(1个测试数据),2列(共两种类别),每列表示该数据与该类相似的可能性,这里取最大的一类
if (maxLoc.x == 0)
cout << "Boy" << endl;
if (maxLoc.x == 1)
cout << "Girl" << endl;
return 0;
}

参考:

【模式识别】OpenCV中使用神经网络

opencv中使用bp神经网络

【opencv】神经网络CvANN_MLP分类

multi-layer perceptrons, MLP)模型,CvANN_MLP。的更多相关文章

  1. 初识spark的MLP模型

    初识Spark的MLP模型 1. MLP介绍 Multi-layer Perceptron(MLP),即多层感知器,是一个前馈式的.具有监督的人工神经网络结构.通过多层感知器可包含多个隐藏层,实现对非 ...

  2. Theano Multi Layer Perceptron 多层感知机

    理论 机器学习技法:https://www.coursera.org/course/ntumltwo 假设上述网址不可用的话,自行度娘找别人做好的种子.或者看这篇讲义也能够:http://www.cn ...

  3. 一目了然卷积神经网络 - An Intuitive Explanation of Convolutional Neural Networks

    An Intuitive Explanation of Convolutional Neural Networks 原文地址:https://ujjwalkarn.me/2016/08/11/intu ...

  4. (转)Introductory guide to Generative Adversarial Networks (GANs) and their promise!

    Introductory guide to Generative Adversarial Networks (GANs) and their promise! Introduction Neural ...

  5. (zhuan) How to Train Neural Networks With Backpropagation

    this blog from: http://blog.demofox.org/2017/03/09/how-to-train-neural-networks-with-backpropagation ...

  6. [转]An Intuitive Explanation of Convolutional Neural Networks

    An Intuitive Explanation of Convolutional Neural Networks https://ujjwalkarn.me/2016/08/11/intuitive ...

  7. An Intuitive Explanation of Convolutional Neural Networks

    https://ujjwalkarn.me/2016/08/11/intuitive-explanation-convnets/ An Intuitive Explanation of Convolu ...

  8. Keras下的文本情感分析简介。与MLP,RNN,LSTM模型下的文本情感测试

    # coding: utf-8 # In[1]: import urllib.request import os import tarfile # In[2]: url="http://ai ...

  9. 【转】OpenCV中使用神经网络 CvANN_MLP

    原文见:http://blog.csdn.net/xiaowei_cqu/article/details/9027617 OpenCV的ml模块实现了人工神经网络(Artificial Neural ...

随机推荐

  1. AngularJS1.X指令

    <!DOCTYPE html> <html ng-app='myApp'> <head> <meta charset="utf-8"> ...

  2. java字符集编码乱码问题

    博客分类: web javajspservlet  最近做网页这块时碰到了正文字符乱码问题.别看这小小的一个问题,对我来说却花费了好长一段时间.现在让我慢慢分析它吧(说实话.这些有部分是从网上找的,但 ...

  3. JSTL1.0和JSTL1.1的区别

    这要从一个异常说起 According to TLD or attribute directive in tag file, attribute value does not accept any e ...

  4. 小程序mdns+udpSocket实现电视推送

    起因:公司以前小程序推送架构为:小程序->接口->后台->socket->机顶盒client->socket->后台->接口->小程序,一系列接口才得到 ...

  5. eot文件

    *.eot文件 是一种压缩字库,目的是解决在网页中嵌入特殊字体的难题2.在网页中嵌入的字体只能是 OpenType 类型,其他类型的字体只有转换成 OpenType 类型(eot格式)的字体才能在网页 ...

  6. Windows环境下的32位汇编语言程序设计

    一个逆向的告诉我可以尝试学一下8086处理器,再回头看一看自己学过的会有提高学呗,8086处理器怎么学....然后就学了8086的汇编, 好友就分享了琢石成器——Windows环境下的32汇编语言设计 ...

  7. linux 域名

    Linux 安装好后,其默认的主机名是 localhost.   1.修改 /etc/sysconfig/network  配置文件 vi  /etc/sysconfig/network 修改HOST ...

  8. php截取指定两个字符之间的字符串

    //截取指定两个字符之间的字符串 public function cut($begin,$end,$str){ $b = mb_strpos($str,$begin) + mb_strlen($beg ...

  9. python 文本文件操作

    文件操作三步走:打开.读写.关闭. open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, close ...

  10. dmp文件自动分析

    dmp文件的分析,可以借助各种工具,比如WinDbg, CDB , NTSD,KD等.Windbg提供了窗口接口,而CDB , NTSD是基于命令行的工具,它们都使用了同样的调试引擎Dbgeng.dl ...