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

#include<iostream>
#include<fstream>
#include<opencv2/opencv.hpp>
#include<io.h>                          //api和结构体
#include<string>


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<uchar>(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<iostream>
#include<fstream>
#include<opencv2/opencv.hpp>
#include<io.h>                          //api和结构体
#include<string>


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<uchar>(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 <<endl;
	file.close();
	cv::namedWindow("readImg");
	cv::imshow("readImg", img);
	cv::waitKey(0);
}

int main1()
{
	int label;
	string train_path = "D:/code/cv/课程数据集/手写数字/训练集";
	string filepath = "D:/code/cv/datasets/课程数据集/手写数字";

	for (label = 0; label < 10; label++)
	{  
		string path;
		ostringstream os; //构造一个输出字符串流,流内容为空
		os << label;
		string str = os.str();
		path = train_path +"/"+ str;
		cout <<"label转为字符" << str << endl;
		cout << "训练集路径" << endl;
		cout << path << endl;
		readTrainImg(path,train_path,label);
	}
	return 0;
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值