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() { ; }
相关文章推荐
- 今天是我的第一次在github上发表博文 3ff8
- crossdomain.xml 文件安全配置 百度云观测、360网站监测 dz论坛crossdomain.xml
- sqlite3函数sqlite3_exec
- 数据库(二):高级检索
- Refactoring with Solr
- Java线程:Condition-线程通信更高效的方式
- 从头认识多线程-1.17 守护线程setDaemon()
- Java开发和运行环境的搭建
- 十进制数转化成N进制数(java实现)
- 自己给自己催眠
- 【一天一道LeetCode】#22. Generate Parentheses
- 【一天一道LeetCode】#22. Generate Parentheses
- java并发-Callable与Future
- OkHttp深入学习(四)——0kio
- tomcat线程池
- 2.基本概念之MBR硬盘、GPT硬盘、UEFI操作系统、ESP分区、UEFI支持FAT文件系统、文件系统
- 20141223--全局使用局部变量+静态变量-01
- 团队项目第一天
- Light OJ 1393 Crazy Calendar (尼姆博弈)
- [android] 帧动画和补间动画