您的位置:首页 > 编程语言 > C语言/C++

C++ 读取目录下的所有图片

2017-11-22 09:43 204 查看

C++ 读取目录下的所有图片

#include <string>
#include "utils.h"
#include <stdio.h>

#include <direct.h>
#include <iostream>
#include <io.h>

using namespace std;
using namespace cv;

string trainPath = "F://vido and images fro test//UIUC texture database";
//获取目录下的所有文件夹
void getClassName(string path, vector<string>& classNames)
{
intptr_t hFile = 0;
struct _finddata_t fileinfo;
string p;

hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo);

if (hFile != -1) {
while (_findnext(hFile, &fileinfo) == 0) {
if (fileinfo.attrib & _A_SUBDIR) {
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) {
//classNames.push_back(p.assign(path).append("/").append(fileinfo.name));//保存文件夹名字
classNames.push_back(fileinfo.name);//保存类名
}
}
}
}
}

//遍历每个文件夹下的图片,并保存为fileList
void getImages(string path, vector<string>& imagesList)
{
intptr_t hFile = 0;
struct _finddata_t fileinfo;
string p;

hFile = _findfirst(p.assign(path).append("\\*.jpg").c_str(), &fileinfo);

if (hFile != -1) {
do {
imagesList.push_back(fileinfo.name);//保存类名
} while (_findnext(hFile, &fileinfo) == 0);
}
}
//获取所有图片
void getAllImages(string path, vector<vector<string>>& fileLists)
{
vector<string> paths;
vector<string > fileList;
string classPath;

getClassName(path, paths);
for (vector<string>::const_iterator p = paths.begin(); p != paths.end(); p++)
{
fileList.clear();
classPath = path;
classPath.append("/").append(*p);
getImages(classPath, fileList);

fileLists.push_back(fileList);
}
}

void test()
{
vector<vector<string>> fileLists;
getAllImages(trainPath, fileLists);

for (int i = 0; i < fileLists.size(); i++) {
for (vector<string>::const_iterator p = fileLists[i].begin(); p != fileLists[i].end(); p++)
cout << *p << endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++