OpenCV: Reading image series from a folder

18,212

Solution 1

From my experiences the VideoCapture can read a sequence of images even without specifing the format. E.g. the following works fine:

std::string pathToData("cap_00000000.bmp");
cv::VideoCapture sequence(pathToData);

the images are sequentially read:

Mat image;
sequence >> image; // Reads cap_00000001.bmp

HOWEVER: This only works if the images are located within the folder of the executable file. I could not figure out to specify a directory like this:

std::string pathToData("c:\\path\\cap_00000000.bmp");
std::string pathToData("c://path//cap_00000000.bmp");
// etc.

Seems to be a bug. An offical example can be found here:

http://kevinhughes.ca/tutorials/reading-image-sequences-with-opencv/ https://github.com/Itseez/opencv/pull/823/files

Solution 2

According to this link it needs to be:

cv::VideoCapture cap("C:/Users/Admin/Documents/Images/%2d.jpg");
                      ^^^                             ^^^

i.e. just a single : after the C and %2d for a 2 digit file name sequence.

Similarly your second example should probably be:

cv::VideoCapture cap("C:/Users/Admin/Documents/Images/01.jpg");
                      ^^^

Solution 3

use glob as in glob(folderpath, vectorOfimages) then access each of the images as vectorOfimages[i].

vectorOfimages is

 vector<String>

and folderpath is a String.

Share:
18,212
E_learner
Author by

E_learner

Updated on June 09, 2022

Comments

  • E_learner
    E_learner almost 2 years

    I am trying to read a series of images from a folder using OpenCV's VideoCapture function. After some search on the internet, my current code is like this:

    cv::VideoCapture cap ( "C:\\Users\\Admin\\Documents\\Images\\%02d.jpg");
    

    I was expecting to see that VideoCapture function should read all the images in that folder with names of two serial digits, like 01.jpg, 02.jpg, ..., 30.jpg. Someone told on the internet that the VideoCapture function should be ale to catch all of these images once I give the first image's location and name. So I also tried to do it like this:

    cv::VideoCapture cap ("C:\\Users\\Admin\\Documents\\Images\\01.jpg");
    

    But still this is not working, at least not for my case here. These images are of different sizes, so I am going to read them first, resize them, and then do further processing on each of them. How can I do this? I am using Windows7, with VisualStudio. Thank you.

  • Paul R
    Paul R almost 11 years
    What have you tried, specifically - did you fix both problems in the first example above ? Did you try the second example ?
  • E_learner
    E_learner almost 11 years
    For the double ':', actually it was my typo, sorry for that. In my code, I used only one single ':' actually. But seems like this is not the reason for the error. I've edited this mistake in my question.