#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. pycharm连接mysql数据库的时区问题.

    登录到mysql的控制台, 执行: set global time_zone='+8:00'; 这设置的是全局时区,重启后失效. 自MySQL 8.0 GA版本开始支持将参数写入并且持久化: set ...

  2. 机器学习、深度学习中的信息熵、相对熵(KL散度)、交叉熵、条件熵

    信息熵 信息量和信息熵的概念最早是出现在通信理论中的,其概念最早是由信息论鼻祖香农在其经典著作<A Mathematical Theory of Communication>中提出的.如今 ...

  3. java 寒假作业

    寒假作业 现在小学的数学题目也不是那么好玩的. 看看这个寒假作业: □ + □ = □ □ - □ = □ □ × □ = □ □ ÷ □ = □ (如果显示不出来,可以参见[图1.jpg]) 每个方 ...

  4. 我的Grunt之旅-初识gruntfile文件

    时间:2018-03-06 18:23  事件:配置 gruntfile.js文件 首先,回忆一下之前的点,grunt项目下面必须有两个文件  ,第一个  package.json ,第二个  Gru ...

  5. 009.Delphi插件之QPlugins,服务的热插拔

    这个DEMO用来演示服务的替换,用起来总是怪怪的感觉,效果图如下 代码如下 unit Frm_Main; interface uses Winapi.Windows, Winapi.Messages, ...

  6. Hive Join优化经验

    大表x小表 这里可以利用mapjoin,SparkSQL中也有mapjoin或者使用广播变量能达到同样效果,此处描述HQL // 开启mapjoin并设定map表大小 set hive.auto.co ...

  7. leetcode1019 Next Greater Node In Linked List

    """ We are given a linked list with head as the first node. Let's number the nodes in ...

  8. 使用WinDbg分析蓝屏dump原因

    大多数人或许都经历过系统蓝屏问题,然而大多数人不清楚该怎么处理蓝屏问题,这里主要对系统蓝屏做一些解释,同时介绍下蓝屏问题分析工具WinDbg分析蓝屏问题的一般步骤. 微软官方对蓝屏的定义是,当系统遇到 ...

  9. JAVA 使用模板创建DOCX文档)(XDocService 使用报错条数过多报错链接不上服务器)

    详细解释https://xdoc.iteye.com/blog/2399451 https://xdoc.iteye.com/  导入 XDocService.jar   我说一下我遇到的问题 我从数 ...

  10. 吴裕雄--天生自然java开发常用类库学习笔记:LinkedList类

    import java.util.LinkedList ; public class LinkedListDemo01{ public static void main(String args[]){ ...