1. 程式人生 > >C++影象批處理:讀取資料夾中全部影象的方法

C++影象批處理:讀取資料夾中全部影象的方法

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; }