c ++读取具有不同文件名的图像集而无需硬编码
c++ read in image set with different file names without hardcoding
有没有办法从文件中读取一组图像,这些图像的名称各不相同,即根本没有连续性?
因此,如果您在同一文件夹中有 4 张图片,文件名为:
- head.jpg
- shoulders.png
- knees.tiff
- toes.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;
}
}
有没有办法从文件中读取一组图像,这些图像的名称各不相同,即根本没有连续性?
因此,如果您在同一文件夹中有 4 张图片,文件名为:
- head.jpg
- shoulders.png
- knees.tiff
- toes.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;
}
}