Reading several images from sub folders.. C++/opencv - c++

I would like to know how to implement a code to read images in folders, for example; I have a folder with name "images" and this folder has sub folders which every sub folder contains many images. I want to read all these images by using c++/opencv in order to use these images to extract features from them all.

You should use a folder and file enumeration API.
Some portable APIs include Boost Filesystem and STLSoft.
This is unrelated to OpenCV.

On Linux, you use the opendir, readdir and closedir functions. See http://pubs.opengroup.org/onlinepubs/007908799/xsh/readdir.html. You need to do this recursively to walk the directory tree.

The simplest way might be for you to use nftw. It will do all the directory traversal for you (with options to control how) and execute your callback function. There is a small code example on the man page.

You can also use filesystem of boost to iterate through folders for reading. Use back_iterator.

Related

Benchmarking on the 2006 Middlebury Stereo Dataset

my problem is the following:
I have developed a superpixel segmentation algorithm and i want to test how the superpixel behave in stereo imagery. For this i use the Middlebury Stereo Dataset 2006 (http://vision.middlebury.edu/stereo/data/scenes2006/), right now i load one pair of images segment them and then compute my metrics (basically a fancy IOU) on it. This now works properly and now i want to extend it that it not only uses one pair of stereo images but the whole dataset.
Programming language is C++.
Here lies the problem:
How would i efficiently load all images? Because the pairs are all in independent folders (for the structure of the folder see below).
My idea would be to have a list of paths to the folders and then import all images from one folder, compute everything and then load the next folder.
How would i do that?
Structure of each stereo pair is like that:
Folder with the name of the item (like cat, wood, baby, ...)
disp1.png
disp5.png
view1.png
view5.png
Right now at the start of the my program i load images like that:
String pathImageLeft = "/Users/Stereo/Left/view1.png";
String pathImageRight = "/Users/Stereo/Right/view5.png";
String pathDisparityLeft = "/Users/Stereo/DisparityMap/disp1.png";
String pathDisparityRight = "/Users/Stereo/DisparityMap/disp5.png";
Thanks for your ideas.
If I understood OP's question right, it can be reduced to
How can I access directories?
From C++17, there is a Filesystem libary available which provides access to directories in a portable way.
Namely, it provides a std::filesystem::directory_entry which
Represents a directory entry. The object stores a path as a member and may also store additional file attributes (hard link count, status, symlink status file size, and last write time) during directory iteration.
and a std::filesystem::directory_iterator
that iterates over the directory_entry elements of a directory (but does not visit the subdirectories). The iteration order is unspecified, except that each directory entry is visited only once. The special pathnames dot and dot-dot are skipped.
The provided links provide sample codes.
Before C++17, you either have to use boost::filesystem (which is actually an anchestor of the std::filesystem) or you have to use the OS specific functions which are usually of limited portability.
Concerning the latter, there are already existing questions in SO:
How to list files in a directory using the Windows API?
How can I get the list of files in a directory using C or C++?
How do I get a list of files in a directory in C++?
to list only a few.

Getting specfic files in C++ code from *nix system

I want to get all the files of type A*.txt present in current directory via C++ code. My OS is *Nix system. Also, I want to get the names of such files.
Please suggest how can this be done.
I tried using system command but system doesn't return anything apart a integer which says if command was executed properly or not.
Thanks
There are basically three ways you can go about this.
One is to use basically what you tried before, but using the popen function, which allows you to read the output of the command(s) you run.
The second solution is to use e.g. opendir and readdir or scandir to manually filter and find the files you look for.
The third and easiest way is to use the glob function.
There is actually a fourth way as well, one which is platform independent and more C++-ish than the above methods: Using the Boost filesystem library.

Getting list of files and folders on the user's computer with the filename filtered by the text line

Currently I'm developing a project that should do the thing described above on Windows. I have the idea to recurcively go through all user's drives and collect all information on then, but it seems to be really time consuming. So is there a better way to do such thing (maybe to use OS's index file or NTFS MFT)?
I use C++/Qt.
You can search for any of the many code examples for this and use one.
The library finctions which you use FindFirstFile and FindNextFile are optimized and will go firectly to the FAT. They are coded by microsoft & I doubt that there is a faster way.
Btw, what do mean by "filtered by the text line"? Do you mean you want only filenames matching a certain pattern (use teh above) or files containing a string?

How to create a program that can read all the images in folder using Boost and OpenCV?

What I want is simple: I need to open all images in a folder (I dont care for subfolders but it would be great to be able to search subfolders) and turn them into a vector of IplImage*s so I can use Boost.Foreach to loop through each image. How can I achieve this?
Take a look at boost filesystem to find all the images files in a directory.
Then cvLoadImage or cv:imread to read each file

Is there any method to know whether a directory contain a sub directory?

I am woking in c++.
Is there any method to know whether a directory contain a sub directory?
CFileFind seems have to search through total files.
It is time consuming if the only subdirectory is at the end of the list and the there are lots of files.
for example: directory A contains 99995 files and one subdirectory at the end of FindNextFile List. had I try 99995 times, then say: yes, it contains subdirectory?
Raymond Chen from Microsoft has written a post that probably applies here: Computing the size of a directory is more than just adding file sizes. In essence, he explains that information like the size of a dir cannot be stored in the dir's entry, because different users might have different permissions, possibly making some of the files invisible to them. Therefore, the only way to get the size the user should see is to calculate it upon request from the user.
In your case, the answer probably stems from the same reasoning. The list of directories available to your app can only be determined when your app asks for it, as its view of the root directory might be different than another app's, running with different credentials. Why Windows store directories along with files I don't know, but that's a given.
Since Win32 is as close as you'll get to the file system in user mode, I'd avoid any higher level solutions such as .NET, as it might only simplify the interface. A driver might work quicker, but that out of the scope of my knowledge.
If you are using the .Net framework you could use Directory.GetDirectories and check is the size of the array is 0. Do not know how if this will give you speed.
If you have control over the directories you could apply a naming convention so that directories that have sub directories are named one way and directories with out sub directories are named another.
You can try using the boost filesystem library.
A class by name directory_iterator [ declared in boost/filesystem/operations.hpp ] has many functions which can be used for listing files, finding whether the file is a sub-directory ( is_directory -- I guess this is what you are looking for ) etc..
Refer the following link for more information.
link text
It seems you are using MFC [ just saw that you are using CFileFind ], didn't see that earlier.
Sorry, Didn't have much info. You may have to use FindFirstFile/FindNextFile.
Whether this can be done very fast is entirely platform-dependent.
On Win32 you use FindFirstFile/FindNextFile or wrappers on top of those like MFC CFileFind and they list items in some order that can't be forced to list directories first.