有没有办法从文件中读取一组图像,这些图像的名称各不相同,即根本没有连续性?
因此,如果您在同一个文件夹中有 4 张图片,文件名为:
- head.jpg
- shoulders.png
- 膝盖.tiff
- 脚趾.bmp
如果不直接对文件名进行硬编码,那么您可以将 shoulders.png 更改为 arms.gif,有没有办法加载它们?
我目前有 OpenCV 和 Boost 可用
最佳答案
对于其他想知道的人:
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
std::vector<cv::Mat> imageVec;
fs::path p (".");
fs::directory_iterator end_itr;
// cycle through the directory
for (fs::directory_iterator itr(p); itr != end_itr; ++itr){
// If it's not a directory, list it. If you want to list directories too, just remove this check.
if (fs::is_regular_file(itr->path())) {
if (fs::is_regular_file(itr->path())){
cv::Mat img;
img = cv::imread(itr->path().string());
if(img.data) imagesVecc.push_back(img);
}
// assign current file name to current_file and echo it out to the console.
std::string current_file = itr->path().string();
std::cout << current_file << std::endl;
}
}
关于c++读取具有不同文件名的图像集而无需硬编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30918237/