#include<io.h>
#include<iostream>
#include<vector>
#include<opencv2/opencv.hpp>
#include <opencv2\imgproc\types_c.h>
using namespace std;
using namespace cv;
void get_image_names(std::string file_path,std::string root_path,::string tar_path, std::vector<std::string>& file_names)
{
intptr_t hFile = 0;
_finddata_t fileInfo;
cv::Mat img;
hFile = _findfirst(file_path.c_str(), &fileInfo);
if (hFile != -1) {
do {
//如果为文件夹,可以通过递归继续遍历,此处我们不需要
if ((fileInfo.attrib & _A_SUBDIR)) {
continue;
}
//如果为单个文件直接push_back
else {
file_names.push_back(fileInfo.name);
cout << fileInfo.name << endl;
string name = fileInfo.name;
img = cv::imread(root_path+name);
cv::cvtColor(img, img, CV_BGR2GRAY);
cv::imwrite(tar_path + name, img);
}
} while (_findnext(hFile, &fileInfo) == 0);
_findclose(hFile);
}
}
int main()
{
std::vector<std::string> file_names;
//图片文件夹以及图片类型
std::string path = "E:/test/7/*.png";
//图片根目录
std::string root_path = "E:/test/7/";
//图片转换后放置目录
std::string tar_path = "E:/test/";
get_image_names(path,root_path,tar_path, file_names);
return 0;
}
|