您的位置:首页 > 其它

windows下批量读取文件夹及子文件夹下的文件名字,方便制作训练样本

2016-04-20 22:01 489 查看
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h> /*用到了time函数,所以要有这个头文件*/
#include <fstream>
#include <sstream>
#include <exception>
#include <vector>
#include <io.h>

#include <highgui.h>
#include <cv.h>

#define showSteps 0

using namespace std;

//config.txt为要处理的跟目录,在训练model中,config.txt中的内容为训练是的样本库,1-13分类好了。--
//在样本筛选model中,config.txt里的内容为要筛选的样本库,未分类。---
char * configFile = "config.txt";

//读取config文件里的内容--
char* trainSetPosPath = (char *)malloc(200*sizeof(char));
void readConfig(char* configFile, char* trainSetPosPath){
fstream f;
char cstring[1000];
int readS=0;
f.open(configFile, fstream::in);
char param1[200]; strcpy(param1,"");
char param2[200]; strcpy(param2,"");
char param3[200]; strcpy(param3,"");

//--读取第一行:--
f.getline(cstring, sizeof(cstring));
readS=sscanf (cstring, "%s %s %s", param1,param2, param3);
strcpy(trainSetPosPath,param3);
}

//遍历config.txt里的根目录下的所有的文件,包括子目录。--
// 其中子目录的名字就是label,子目录里的文件为label对于的训练测试样本---
vector<string> imgNames;
int labelTemp = 0;

void dfsFolder(string folderPath){
_finddata_t FileInfo;
string strfind = folderPath + "\\*";
long Handle = _findfirst(strfind.c_str(), &FileInfo);
if (Handle == -1L)
{
cerr << "can not match the folder path" << endl;
exit(-1);
}
do{
//判断是否有子目录--
if (FileInfo.attrib & _A_SUBDIR)        {
//  cout<<FileInfo.name<<" "<<FileInfo.attrib<<endl;
//这个语句很重要--
if( (strcmp(FileInfo.name,".") != 0 ) &&(strcmp(FileInfo.name,"..") != 0))          {
string newPath = folderPath + "\\" + FileInfo.name;
cout<<FileInfo.name<<" "<<newPath<<endl;
//根目录下下的子目录名字就是label名,如果没有子目录则其为根目录下
labelTemp = atoi(FileInfo.name);
//  printf("%d\n",labelTemp);
dfsFolder(newPath);
}
}else  {
string finalName = folderPath + "\\" + FileInfo.name;
//将所有的文件名写入一个txt文件--
//  cout << FileInfo.name << "\t";
//  printf("%d\t",label);
//  cout << folderPath << "\\" << FileInfo.name  << " " <<endl;
//将文件名字和label名字(子目录名字赋值给向量)--

imgNames.push_back(finalName);

}
}while (_findnext(Handle, &FileInfo) == 0);
_findclose(Handle);

}

void initTrainImage(){
readConfig(configFile, trainSetPosPath);

string folderPath = trainSetPosPath;
dfsFolder(folderPath);

}

//批量读取config.txt里面的文件识别
void processingTotal(){
initTrainImage();

int imgNum = imgNames.size();
for(int iNum=0;iNum<imgNum;iNum++){

cout<<endl<<iNum<<endl;
cout<<imgNames[iNum].c_str()<<endl;
IplImage * src=cvLoadImage(imgNames[iNum].c_str(),1);
if(!src) continue;

//processing-------------

}
}

void main()
{
;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: