string file_path = "H:\\image data\\";
string search_path = file_path + "*.jpg";
vector<string> file_list;
if (!get_filelist_from_dir(search_path, file_list))
cout << "open file error!" << endl;
for (int i = 0; i < file_list.size(); i++)
{
string image_path = file_path + file_list[i];
Mat image = imread(image_path);
/*处理*/
}
get_filelist_from_dir()函数的定义如下:
#include <io.h>
bool get_filelist_from_dir(string path, vector<string>& files)
{
long hFile = 0;
struct _finddata_t fileinfo;
files.clear();
if((hFile = _findfirst(path.c_str(), &fileinfo)) != -1)
{
do
{
if(!(fileinfo.attrib & _A_SUBDIR))
files.push_back(fileinfo.name);
}while(_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
return true;
}
else
return false;
}