//c++遍历文件夹下的所有图片
int readAllImgs()
{
struct _finddata_t file;
intptr_t If;
std::vector<string> imgFilesName;
std::string path, tempPath; //遍历文件夹中的所有图片
path = path.assign("E:\\candidate-faces"); //文件的路径
tempPath = path.assign("E:\\candidate-faces");
//if ((If = _findfirst(path.append("\\*").c_str(), &file)) == -1) //不加*也会报错
if ((If = _findfirst(path.append("\\*.jpg").c_str(), &file)) == -1) //不加*也会报错
{
std::cout << "Not find image file" << std::endl;
}
else
{
while (_findnext(If, &file) == 0)
{
std::cout << "file name: " << path.substr(0, path.length() - 1) << file.name << std::endl;
cv::Mat srcImage = cv::imread(tempPath + "\\" + file.name, 0);//第一个图片路径..是打不开的
if (!srcImage.data)
{
printf("picture read error");
continue;
}
cv::Mat vecImage(srcImage.rows, srcImage.cols, CV_8UC3, cv::Scalar::all(0));
/*
对图像进行处理操作
*/
std::string resFiles = "resluts/"; //将矢量化图片保存在reslut文件中
cv::imwrite(resFiles + file.name, vecImage); // 遍历所有图片并写出
}
}
_findclose(If);
system("PAUSE");
return 0;
}
//c读取文件并存入txt文件中
int imgfilesName()
{
const char *to_search = "G:\\positive\\*.jpg"; //欲查找的文件,支持通配符
FILE *fp;
long handle; //用于查找的句柄
struct _finddata_t fileinfo; //文件信息的结构体
fp = fopen("train_list2.txt", "w+");
if (fp == 0)
{
printf("can't open file\n");
return 0;
}
handle = _findfirst(to_search, &fileinfo); //第一次查找
if (-1 == handle)
return -1;
fprintf(fp, "positive/%s 0\r", fileinfo.name); //打印出找到的文件的文件名
while (!_findnext(handle, &fileinfo)) //循环查找其他符合的文件,直到找不到其他的为止
{
fprintf(fp, "positive/%s 0\r", fileinfo.name);
}
_findclose(handle); //关闭句柄
printf("output done.\n");
fclose(fp);
//system("pause");
return 0;
}