1. 文档简单介绍

参考【opencv2refman.pdf】

ANN原理及响应函数设置如上所述;

ANN与其他分类算法有一个不同点是,构建和训练分开进行;

2.OpenCV实现代码

2.1//从XML文件载入训练数据和类别标签,并进行训练;

OCR::OCR(string trainFile){DEBUG=false;trained=false;saveSegments=false;//字符块的宽高charSize=20;//Read file storage.FileStorage fs;fs.open("OCR.xml", FileStorage::READ);//新建两个矩阵,用于存储训练数据和类别标签;Mat TrainingData;Mat Classes;fs["TrainingDataF15"] >> TrainingData;fs["classes"] >> Classes;//10层ANN,训练train(TrainingData, Classes, 10);}

2.2训练

void OCR::train(Mat TrainData, Mat classes, int nlayers){//矩阵,用于创建ANN;1行3列;训练数据的列数(特征属性),ANN的层数,字符数(类别标签);Mat layers(1,3,CV_32SC1);layers.at<int>(0)= TrainData.cols;layers.at<int>(1)= nlayers;layers.at<int>(2)= numCharacters;//使用sigmoid创建ANNann.create(layers, CvANN_MLP::SIGMOID_SYM, 1, 1);//Prepare trainClases//Create a mat with n trained data by m classesMat trainClasses;//TrainData.rows表示训练数据的数量;trainClasses.create( TrainData.rows, numCharacters, CV_32FC1 );for( int i = 0; i <  trainClasses.rows; i++ ){for( int k = 0; k < trainClasses.cols; k++ ){//If class of data i is same than a k class//trainClasses的第i个row存储第i个样本的标签类别,而此标签存储在class的第i个元素下;//class(numCharacters)-->trainClasses(TrainData.rows, numCharacters)矩阵转换;//(i,j),遍历j;在样本i对应标签位置置为1;//比如第5个样本对应第3个类别,则设置trainClasses(5,3)=1;if( k == classes.at<int>(i) )trainClasses.at<float>(i,k) = 1;elsetrainClasses.at<float>(i,k) = 0;}}//所有的训练样本权重都为1;Mat weights( 1, TrainData.rows, CV_32FC1, Scalar::all(1) );if (DEBUG){//存储训练前的类别标签;FileStorage classBefore;classBefore.open("classBefore.xml", FileStorage::WRITE);classBefore << "before" << trainClasses;classBefore.release();}//Learn classifier,训练数据,类别标签(大矩阵形式),权重;//类别标签并不会变化,此API仅训练ANN;ann.train( TrainData, trainClasses, weights );if (DEBUG){//存储训练后的类别标签;FileStorage classAfter;classAfter.open("classAfter.xml", FileStorage::WRITE);classAfter << "after" << trainClasses;classAfter.release();}trained=true;
}

2.3 分类测试

ann.predict(f, output);

2.4 错误备注

在进行LED数字识别项目时,使用ANN和KNN对比,当然还是ANN的效果更好些;出现一些需要强调的错误:

2.4.1 训练类别需要使用整型 int

//set the class of train feature;vector<int> numberLabel;for (int colsIdx = 0; colsIdx<10; ++colsIdx)<span style="white-space:pre"> </span>numberLabel.push_back(colsIdx);Mat(numberLabel).copyTo(ftrClass);

按上述方式新建类别矩阵;

2.4.2 矩阵copy时类型准确
for (int rowsIdx = 0, toFtrIdx = 0; rowsIdx < tempFtr.rows; ++rowsIdx)for (int colsIdx = 0; colsIdx < tempFtr.cols; ++colsIdx, ++toFtrIdx)ftrNumberTa.at<float>(nuIdex, toFtrIdx) = tempFtr.at<float>(rowsIdx, colsIdx);

矩阵copy取值时,一般将rows作为第一层,cols作为第二层;

也可不强制按照此模式,理解
MatName.at<dataType>(rowsIdx,colsIdx);

另外Mat新建

//cols即image的宽;rows即image的高Mat ftrNumberTa(cv::Size(ftrNumber_cols, ftrNumber_rows), CV_32F);

此文仅简单记载ANN识别在opencv中的使用流程;

Artificial Neural Networks FileStorage of OpenCV相关推荐

  1. AdaNet: Adaptive Structural Learning of Artificial Neural Networks

    AdaNet: Adaptive Structural Learning of Artificial Neural Networks tf的轻量级框架AdaNet,该框架可以使用少量专家干预来自动学习 ...

  2. 机器学习之Artificial Neural Networks

    人类通过模仿自然界中的生物,已经发明了很多东西,比如飞机,就是模仿鸟翼,但最终,这些东西会和原来的东西有些许差异,artificial neural networks (ANNs)就是模仿动物大脑的神 ...

  3. 机器学习笔记-人工神经网络(artificial neural networks)

    目录 人工神经网络(ANN) 引言 感知器 什么是感知器? 感知器的例子 权重和阈值 多层前馈神经网络 网络结构 正向传播 反向传播(Error Back Propagation,BP算法) ​ 后话 ...

  4. Paper:《Graph Neural Networks: A Review of Methods and Applications》翻译与解读

    Paper:<Graph Neural Networks: A Review of Methods and Applications>翻译与解读 目录 <Graph Neural N ...

  5. Mapping Spiking Neural Networks的论文汇总以及思考

    首先感谢CSDN平台,发现不是我一个人在SNN Mapping方面纠结着.去年看了Mapping方面的内容后感觉想创新还是有点难度的,毕竟优化就是生物进化算法类似的套路,可是你会发现自己实现的结果就是 ...

  6. 论文---overcoming catastrophic forgetting in neural networks

    不定期更新--论文 overcoming catastrophic forgetting in neural networks 出处:2017 Jan 25 PNAS(proceedings of t ...

  7. Neural Networks and Deep Learning - 神经网络与深度学习 - Overfitting and regularization - 过拟合和正则化

    Neural Networks and Deep Learning - 神经网络与深度学习 - Overfitting and regularization - 过拟合和正则化 Neural Netw ...

  8. Deep Neural Networks are Easily Fooled: High Confidence Predictions for Unrecognizable Images

    论文原文:Deep Neural Networks are Easily Fooled: High Confidence Predictions for Unrecognizable Images  ...

  9. [转载]Deep Neural Networks are Easily Fooled: High Confidence Predictions for Unrecognizable Images

    论文内容总结 首次提出假反例攻击,即:生成人类无法识别的图片,却能够让神经网络以高置信度分类成某个类别 使用了多种不同的方法生成图片,称为"fooling images" 普通的E ...

最新文章

  1. 计算机平均数的函数,Excel2010
  2. JAVA---AWT 图形绘制
  3. sqlite3数据存储最多存储多少条数据?达到上限如何处理?_在数据爆炸的当下,教你设计一个能实现9个9数据可靠性的存储系统...
  4. if的作用域问题 *输出1~6的随机数*
  5. java 固定长度队列_如何彻底搞懂 Java 数据结构?|CSDN 博文精选
  6. CS190.1x Scalable Machine Learning
  7. 存储类、链接、内存管理
  8. 一文读懂前端与HTML5技术的区别!
  9. “OpAfrica”行动:匿名者攻击南非武器采购机构
  10. c 语言编程文档下载,C语言编程规范
  11. OFD文件解析流程(转)
  12. 如何让exe以管理员权限运行
  13. python echarts接口_GitHub - jllan/pyecharts: Python Echarts Plotting Library
  14. 知识星球<我们谈论数据科学>-python30天打卡训练营Day1
  15. 第一章:计算机的软硬件基本结构_3
  16. MATLAB实现雅可比与高斯塞德尔迭代
  17. Python图片下载器(单线程PK多线程)_一蓑烟雨任平生
  18. 如何迁移GitGitolite服务器
  19. Dev-C++安装OpenCV
  20. 在 Pages文稿中怎样给文本添加阴影、外框?

热门文章

  1. kali linux无线驱动安装,Kali Linux 安装WIFI无线网卡驱动:rtl8822bu 教程
  2. 用户在图片上点选并标记位置,js实现
  3. <JVM上篇:内存与垃圾回收篇>01-JVM与Java体系结构
  4. MyEclipse在启动了诸如Tomcat等web服务后,控制台信息不显示的问题
  5. 解决springboot无法访问此网站,localhost 拒绝了我们的连接请求的问题。
  6. php微信段子,微信朋友圈有趣的段子 朋友圈配图
  7. HTTP协议与Tomcat服务器
  8. 转行AI产品经理的学习过程
  9. 用switch语句根据消费金额计算折扣 (Java经典编程案例)
  10. 【网络--实验】华三防火墙命令行调试实例