1 | initial version |
this is what I am using so far with success:
// you need these includes for the function
//#include <windows.h> // for windows systems
#include <dirent.h> // for linux systems
#include <sys/stat.h> // for linux systems
/* Returns a list of files in a directory (except the ones that begin with a dot) */
void readFilenames(std::vector<string> &filenames, const string &directory)
{
#ifdef WINDOWS
HANDLE dir;
WIN32_FIND_DATA file_data;
if ((dir = FindFirstFile((directory + "/*").c_str(), &file_data)) == INVALID_HANDLE_VALUE)
return; /* No files found */
do {
const string file_name = file_data.cFileName;
const string full_file_name = directory + "/" + file_name;
const bool is_directory = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
if (file_name[0] == '.')
continue;
if (is_directory)
continue;
filenames.push_back(full_file_name);
} while (FindNextFile(dir, &file_data));
FindClose(dir);
#else
DIR *dir;
class dirent *ent;
class stat st;
dir = opendir(directory.c_str());
while ((ent = readdir(dir)) != NULL) {
const string file_name = ent->d_name;
const string full_file_name = directory + "/" + file_name;
if (file_name[0] == '.')
continue;
if (stat(full_file_name.c_str(), &st) == -1)
continue;
const bool is_directory = (st.st_mode & S_IFDIR) != 0;
if (is_directory)
continue;
// filenames.push_back(full_file_name); // returns full path
filenames.push_back(file_name); // returns just filename
}
closedir(dir);
#endif
} // GetFilesInDirectory
int main()
{
string folder = "/documents/images/";
vector<string> filenames;
readFilenames(filenames, folder);
for(size_t i = 0; i < filenames.size(); ++i)
{
Mat src = imread(folder + filenames[i]);
if(!src.data)
cerr << "Problem loading image!!!" << endl;
/* do whatever you want with your images here */
}
}
2 | No.2 Revision |
this is what I am using so far with success:
// you need these includes for the function
//#include <windows.h> // for windows systems
#include <dirent.h> // for linux systems
#include <sys/stat.h> // for linux systems
/* Returns a list of files in a directory (except the ones that begin with a dot) */
void readFilenames(std::vector<string> &filenames, const string &directory)
{
#ifdef WINDOWS
HANDLE dir;
WIN32_FIND_DATA file_data;
if ((dir = FindFirstFile((directory + "/*").c_str(), &file_data)) == INVALID_HANDLE_VALUE)
return; /* No files found */
do {
const string file_name = file_data.cFileName;
const string full_file_name = directory + "/" + file_name;
const bool is_directory = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
if (file_name[0] == '.')
continue;
if (is_directory)
continue;
filenames.push_back(full_file_name);
} while (FindNextFile(dir, &file_data));
FindClose(dir);
#else
DIR *dir;
class dirent *ent;
class stat st;
dir = opendir(directory.c_str());
while ((ent = readdir(dir)) != NULL) {
const string file_name = ent->d_name;
const string full_file_name = directory + "/" + file_name;
if (file_name[0] == '.')
continue;
if (stat(full_file_name.c_str(), &st) == -1)
continue;
const bool is_directory = (st.st_mode & S_IFDIR) != 0;
if (is_directory)
continue;
// filenames.push_back(full_file_name); // returns full path
filenames.push_back(file_name); // returns just filename
}
closedir(dir);
#endif
} // GetFilesInDirectory
int main()
{
string folder = "/documents/images/";
vector<string> filenames;
readFilenames(filenames, folder);
for(size_t i = 0; i < filenames.size(); ++i)
{
Mat src = imread(folder + filenames[i]);
if(!src.data)
cerr << "Problem loading image!!!" << endl;
/* do whatever you want with your images here */
}
}
an alternative is to use the boost library check below:
// files you need to include, do not forget to include the -lboost_filesystem and -lboost_system libraries otherwise you will get quite some errors/complaints during the compilation
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
void readFilenamesBoost(vector<string> &filenames, const string &folder)
{
path directory(folder);
directory_iterator itr(directory), end_itr;
string current_file = itr->path().string();
for(;itr != end_itr; ++itr)
{
// If it's not a directory, list it. If you want to list directories too, just remove this check.
if (is_regular_file(itr->path()))
{
// assign current file name to current_file and echo it out to the console.
// string full_file_name = itr->path().string(); // returns full path
string filename = itr->path().filename().string(); // returns just filename
filenames.push_back(filename);
}
}
}
int main()
{
string folder = "/documents/images/";
vector<string> filenames;
readFilenamesBoost(filenames, folder);
for(size_t i = 0; i < filenames.size(); ++i)
{
Mat src = imread(folder + filenames[i]);
if(!src.data)
cerr << "Problem loading image!!!" << endl;
/* do whatever you want with your images here */
}
}
3 | No.3 Revision |
edit: It seems that there is an embedded function (many thanks to @berak for pointing that out) that does this job, have a look on the example below:
int main()
{
vector<String> filenames; // notice here that we are using the Opencv's embedded "String" class
String folder = "<some_folder_path>"; // again we are using the Opencv's embedded "String" class
glob(folder, results);
for(size_t i = 0; i < filenames.size(); ++i)
{
Mat src = imread(filenames[i]);
if(!src.data)
cerr << "Problem loading image!!!" << endl;
/* do whatever you want with your images here */
}
}
this is what I am using so far with success:
// you need these includes for the function
//#include <windows.h> // for windows systems
#include <dirent.h> // for linux systems
#include <sys/stat.h> // for linux systems
/* Returns a list of files in a directory (except the ones that begin with a dot) */
void readFilenames(std::vector<string> &filenames, const string &directory)
{
#ifdef WINDOWS
HANDLE dir;
WIN32_FIND_DATA file_data;
if ((dir = FindFirstFile((directory + "/*").c_str(), &file_data)) == INVALID_HANDLE_VALUE)
return; /* No files found */
do {
const string file_name = file_data.cFileName;
const string full_file_name = directory + "/" + file_name;
const bool is_directory = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
if (file_name[0] == '.')
continue;
if (is_directory)
continue;
filenames.push_back(full_file_name);
} while (FindNextFile(dir, &file_data));
FindClose(dir);
#else
DIR *dir;
class dirent *ent;
class stat st;
dir = opendir(directory.c_str());
while ((ent = readdir(dir)) != NULL) {
const string file_name = ent->d_name;
const string full_file_name = directory + "/" + file_name;
if (file_name[0] == '.')
continue;
if (stat(full_file_name.c_str(), &st) == -1)
continue;
const bool is_directory = (st.st_mode & S_IFDIR) != 0;
if (is_directory)
continue;
// filenames.push_back(full_file_name); // returns full path
filenames.push_back(file_name); // returns just filename
}
closedir(dir);
#endif
} // GetFilesInDirectory
int main()
{
string folder = "/documents/images/";
vector<string> filenames;
readFilenames(filenames, folder);
for(size_t i = 0; i < filenames.size(); ++i)
{
Mat src = imread(folder + filenames[i]);
if(!src.data)
cerr << "Problem loading image!!!" << endl;
/* do whatever you want with your images here */
}
}
an alternative is to use the boost library check below:
// files you need to include, do not forget to include the -lboost_filesystem and -lboost_system libraries otherwise you will get quite some errors/complaints during the compilation
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
void readFilenamesBoost(vector<string> &filenames, const string &folder)
{
path directory(folder);
directory_iterator itr(directory), end_itr;
string current_file = itr->path().string();
for(;itr != end_itr; ++itr)
{
// If it's not a directory, list it. If you want to list directories too, just remove this check.
if (is_regular_file(itr->path()))
{
// assign current file name to current_file and echo it out to the console.
// string full_file_name = itr->path().string(); // returns full path
string filename = itr->path().filename().string(); // returns just filename
filenames.push_back(filename);
}
}
}
int main()
{
string folder = "/documents/images/";
vector<string> filenames;
readFilenamesBoost(filenames, folder);
for(size_t i = 0; i < filenames.size(); ++i)
{
Mat src = imread(folder + filenames[i]);
if(!src.data)
cerr << "Problem loading image!!!" << endl;
/* do whatever you want with your images here */
}
}
4 | No.4 Revision |
edit: It seems that there is an embedded function (many thanks to @berak for pointing that out) that does this job, have a look on the example below:
int main()
{
vector<String> filenames; // notice here that we are using the Opencv's embedded "String" class
String folder = "<some_folder_path>"; // again we are using the Opencv's embedded "String" class
glob(folder, results);
filenames); // new function that does the job ;-)
for(size_t i = 0; i < filenames.size(); ++i)
{
Mat src = imread(filenames[i]);
if(!src.data)
cerr << "Problem loading image!!!" << endl;
/* do whatever you want with your images here */
}
}
this is what I am using so far with success:
// you need these includes for the function
//#include <windows.h> // for windows systems
#include <dirent.h> // for linux systems
#include <sys/stat.h> // for linux systems
/* Returns a list of files in a directory (except the ones that begin with a dot) */
void readFilenames(std::vector<string> &filenames, const string &directory)
{
#ifdef WINDOWS
HANDLE dir;
WIN32_FIND_DATA file_data;
if ((dir = FindFirstFile((directory + "/*").c_str(), &file_data)) == INVALID_HANDLE_VALUE)
return; /* No files found */
do {
const string file_name = file_data.cFileName;
const string full_file_name = directory + "/" + file_name;
const bool is_directory = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
if (file_name[0] == '.')
continue;
if (is_directory)
continue;
filenames.push_back(full_file_name);
} while (FindNextFile(dir, &file_data));
FindClose(dir);
#else
DIR *dir;
class dirent *ent;
class stat st;
dir = opendir(directory.c_str());
while ((ent = readdir(dir)) != NULL) {
const string file_name = ent->d_name;
const string full_file_name = directory + "/" + file_name;
if (file_name[0] == '.')
continue;
if (stat(full_file_name.c_str(), &st) == -1)
continue;
const bool is_directory = (st.st_mode & S_IFDIR) != 0;
if (is_directory)
continue;
// filenames.push_back(full_file_name); // returns full path
filenames.push_back(file_name); // returns just filename
}
closedir(dir);
#endif
} // GetFilesInDirectory
int main()
{
string folder = "/documents/images/";
vector<string> filenames;
readFilenames(filenames, folder);
for(size_t i = 0; i < filenames.size(); ++i)
{
Mat src = imread(folder + filenames[i]);
if(!src.data)
cerr << "Problem loading image!!!" << endl;
/* do whatever you want with your images here */
}
}
an alternative is to use the boost library check below:
// files you need to include, do not forget to include the -lboost_filesystem and -lboost_system libraries otherwise you will get quite some errors/complaints during the compilation
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
void readFilenamesBoost(vector<string> &filenames, const string &folder)
{
path directory(folder);
directory_iterator itr(directory), end_itr;
string current_file = itr->path().string();
for(;itr != end_itr; ++itr)
{
// If it's not a directory, list it. If you want to list directories too, just remove this check.
if (is_regular_file(itr->path()))
{
// assign current file name to current_file and echo it out to the console.
// string full_file_name = itr->path().string(); // returns full path
string filename = itr->path().filename().string(); // returns just filename
filenames.push_back(filename);
}
}
}
int main()
{
string folder = "/documents/images/";
vector<string> filenames;
readFilenamesBoost(filenames, folder);
for(size_t i = 0; i < filenames.size(); ++i)
{
Mat src = imread(folder + filenames[i]);
if(!src.data)
cerr << "Problem loading image!!!" << endl;
/* do whatever you want with your images here */
}
}