我需要对文件样本进行训练,有10*50=500个样本,所以训练之前写个存入和读取程序,方便操作。文件格式规律如下:
charSamples\0\0 (1).png
charSamples\0\0 (2).png
*
*
*
charSamples\9\9 (50).png
#include <vector>
#include <iostream>
#include <fstream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include <opencv\ml.h>
#include <string>
using namespace cv;
using namespace std;
vector<string> readfiles(string FilePath)
{
char buffer[256];
vector<string> bufstr;
fstream out;
out.open(FilePath,ios::in);
cout<<"com.txt"<<" 的内容如下:"<<endl;
while(!out.eof())
{
out.getline(buffer,256,'\n');
bufstr.push_back(buffer);
cout<<buffer<<endl;
}
out.close();
return bufstr;
}
int write2txt(string savename)
{
char buf[50];
vector<string> savevec;
ofstream save(savename,ios::out);
int counts = 0;
for(int a =0;a<10;a++)
{
for(int j =1;j<51;j++)
{
sprintf(buf,"charSamples\\%d\\%d (%d).png\n",a,a,j); save<<buf;
savevec.push_back(buf);
counts+=1;
}
}
save.close();
return counts;
}
int main()
{
string savename = "4-11.txt";
int n = write2txt(savename);
readfiles(savename);
cout<<"successful write files num:"<<n<<endl;
waitKey();
return 0;
}
___________________________________________________________________________________
输出保存的TXT文件: