栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++实现基于模板匹配的手写数字识别

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++实现基于模板匹配的手写数字识别

#include
#include
#include
#include                          //api和结构体
#include


using namespace std;
using namespace cv;


void readTestImg(const cv::String& imgpath,int num)
{
	cv::String train_path = "D:/code/cv/课程数据集/手写数字/训练集";
	ostringstream newos;
	newos << num;
	train_path = train_path + "/" + newos.str() + "_train.txt";
	ifstream myfile(train_path, ios::in);
	string temp;
	string res;
	while (getline(myfile, temp)) {
		res += temp;
		res += 'n';
	}
	//cout << res;
	myfile.close();
	//1-125
	//一些比较
	int faslenums = 0, truenums = 0;
	double sums = 0;
	for (int label = 1; label < 126; label++)
	{
		cv::String path;
		cv::Mat img;
		ostringstream os; //构造一个输出字符串流,流内容为空
		os << label;
		path = imgpath + "/"+os.str()+".bmp";
		os.clear();
		//cout <<"图片路径: "+path << endl;
		img = cv::imread(path);
		cvtColor(img, img, COLOR_BGR2GRAY);
		int rows = img.rows;
		int cols = img.cols;
		cv::String img_str = "";
		for (int i = 0; i < rows; i += 5)
		{
			for (int j = 0; j < cols; j += 5)
			{
				int pixel;
				pixel = img.at(i, j);
				if (pixel < 127)
				{
					img_str += "1";
				}
				else
					img_str += "0";
			}
		}
		for (int len = 0; len < img_str.length(); len++)
		{
			if (img_str[len] == res[len + 2])
				truenums++;
			else
				faslenums++;
		}
		sums = truenums + faslenums;
		cout << "第";
		cout << label;
		cout << "张的相似度";
		cout <<  truenums / sums << endl;
	}
	
}

int main()
{
	int label;
	string test_path = "D:/code/cv/课程数据集/手写数字";
	for (label = 2; label < 3; label++)
	{
		string path;
		ostringstream os; //构造一个输出字符串流,流内容为空
		os << label;
		string str = os.str();
		path = test_path + "/" + str;
		//cout << "label转为字符" << str << endl;
		//cout << label<<"测试集路径" << endl;
		cout << path << endl;
		readTestImg(path, label);
	}
	return 0;
}

上面的代码是测试集

下面的代码是训练集

#include
#include
#include
#include                          //api和结构体
#include


using namespace std;
using namespace cv;

void readTrainImg(const cv::String& imgpath,const cv::String &train_path,int num)
{
	cv::Mat img;
	cv::String path;
	int label = 126;
	ostringstream os; //构造一个输出字符串流,流内容为空
	os << label;
	path = imgpath + "/"+os.str()+".bmp";
	cout << path << endl;
	img = cv::imread(path);
	cvtColor(img, img, COLOR_BGR2GRAY);
	int rows = img.rows;
	int cols = img.cols;
	cv::String img_str = "";
	for (int i = 0; i < rows; i+=5)
	{
		for (int j = 0; j < cols; j+=5)
		{
			int pixel;
			pixel = img.at(i, j);
			if (pixel < 127)
			{
				img_str += "1";
			}
			else
				img_str += "0";
		}
	}
	ostringstream newos;
	//ostringstream os; //构造一个输出字符串流,流内容为空
	newos << num;
	cv::String rout = train_path+"/"+newos.str()+"_train.txt";
	ofstream file;
	file.open(rout, ios::out);
	file << num << ":" << img_str < 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/887720.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号