IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++实现基于模板匹配的手写数字识别 -> 正文阅读

[C++知识库]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;
}

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章           查看所有文章
加:2022-05-19 11:50:18  更:2022-05-19 11:51:47 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/13 0:09:42-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码