#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. Nginx 的优势

    Nginx 的优势 在 Java 开发中,Nginx 有着非常广泛的使用,随便举几点: 使用 Nginx 做静态资源服务器:Java 中的资源可以分为动态和静态,动态需要经过 Tomcat 解析之后, ...

  2. R语言 scale()函数

    1.scale() 函数 #Usage scale(x, center = TRUE, scale = TRUE) #center中心化,scale标准化 #Arguments x :a numeri ...

  3. 主机ping虚拟机失败。虚拟机ping主机,可以ping通。

    原文:https://blog.csdn.net/ww1473345713/article/details/51490525 今天打开虚拟机,然后用Xshell远程连接,发现连接不上.按照以下顺序检查 ...

  4. 7.1 Varnish VCL

    根据以上的配置增加集群,修改default.vcl # This ) # man page for details on VCL syntax and semantics. # # Default b ...

  5. B. RGB plants

    B. RGB plants time limit per test 2.0 s memory limit per test 64 MB input standard input output stan ...

  6. Nature

    1.主干 (1)<河图+洛书>:启发伏羲作八卦. (2)<三坟+五典>:失传:伏羲.神农.轩辕.少昊.颛顼.帝喾.唐尧.虞舜. (3)<八索+九丘>:失传:八卦之书 ...

  7. Adapter之自定义Adapter

    前言: 在我们写程序是经常会用到适配器,当系统自带的适配器不够用时即可自己定义适配器 正文: 因为我们用到的ListView视图所以我们先初始化ListView,在我们的主活动中添加如下代码 < ...

  8. DNS原理和智能DNS及CDN

    CDN是什么 CDN(Content Delivery Network)是内容分发网络. 作用: 使用户可以就近取的所需要的内容,加速用户访问网站资源的速度.物理距离更近,响应速度更快.拦截部分网络请 ...

  9. 安装npm install时,长时间停留在fetchMetadata: sill 解决方法——换npm的源

    安装npm install时,长时间停留在fetchMetadata: sill mapToRegistry uri http://registry.npmjs.org/whatwg-fetch处, ...

  10. ROS常用库(一) fake_localization

    wiki是最好的学习资料,以下直接参考了wiki官网.另外po出官网网址,建议英语较好的朋友之接看原版 http://wiki.ros.org/fake_localization 概述 fake_lo ...