头文件
#include "atlstr.h"
函数:
bool GetImgNameFromDir(const std::string& path, const std::string &filetype, std::vector<std::string>& fileNameVec)
{
fileNameVec.clear();
string DirPath = path + filetype;
HANDLE file;
WIN32_FIND_DATA fileData;
file = FindFirstFile(DirPath.c_str(), &fileData);
std::string fileDataName = fileData.cFileName;
if (fileDataName != "." && fileDataName != "..")
{
fileNameVec.push_back(fileData.cFileName);
}
while (FindNextFile(file, &fileData))
{
std::string fileDataName = fileData.cFileName;
if (fileDataName != "." && fileDataName != "..")
{
fileNameVec.push_back(fileData.cFileName);
}
}
FindClose(file);
return true;
}
测试示例:opencv3.3 + 读取文件夹下bmp格式图像
void readImage(string &path, string &type, vector<cv::Mat> &TestImageData)
{
//从PC机本地读取bmp图片保存在内存中
cout << "Read TestImage!" << endl;
vector<string> test_vImageFileNames;
GetImgNameFromDir(path, type, test_vImageFileNames); //获取目录下所有文件名
int test_fileNum = test_vImageFileNames.size();
cout << "test_fileNum = " << test_fileNum << endl;
TestImageData.clear();
for (int i = 0; i < test_fileNum; i++)
{
string fname = path + test_vImageFileNames[i];
cv::Mat img = cv::imread(fname.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
TestImageData.push_back(img);
}
return;
}
int main()
{
string imagePath = "..\\Data\\Data\\";
string imageType = "*.bmp";
vector<cv::Mat> TestImageData;
readImage(imagePath, imageType, TestImageData);
return 0;
}