#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;
}
|