目录
前言
本级环境:win10/vs2017/opencv3.4.0/c++
训练样本:数字0-9 共十组 每组50张图片
一.模型训练
代码:
注意:win7与win10句柄的定义类型有所区别,具体的代码中已注释
#include <io.h>
#include <string>
#include <iostream>
#include <opencv2\opencv.hpp>
#include <opencv2\ml.hpp>
using namespace cv;
using namespace ml;
using namespace std;
int main()
{
//////******************遍历文件夹下的图片读取训练样本******************//////
//将图片大小转化为8*16越大训练时间越长
const int imageRows = 8;
const int imageCols = 16;
//图片共有10类 数字0-9
const int classSum = 10;//******201907111731******
//每类共50张图片
const int imagesSum = 50;
//每一行一个训练图片
float trainingData[classSum*imagesSum][imageRows*imageCols] = { {0} };
//训练样本标签
float labels[classSum*imagesSum][classSum] = { {0} };
Mat src, resizeImg, trainImg;
for (int i = 0; i < classSum; i++)
{
//目标文件夹路径
std::string inPath = "E:\\333333神经网络ann\\test\\Numpicture\\";
char temp[256];
int k = 0;
sprintf_s(temp, "%d", i);
inPath = inPath + temp + "\\*.png";
//用于查找的句柄
//lon