OpenCV: reading a series of images from a folder

I am trying to read a series of images from a folder using the OpenCV VideoCapture function. After doing some searching on the Internet, my current code is as follows:

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

I expected to see that the VideoCapture function should read all the images in this folder with the names of two serial numbers, for example 01.jpg, 02.jpg, ..., 30.jpg . Someone said on the Internet that the VideoCapture function must be ale to catch all these images as soon as I give the first location and image name. Therefore, I also tried to do it as follows:

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

But still this does not work, at least not for my case. These images have different sizes, so I will first read them, resize them and then process each of them. How can i do this? I am using Windows7 with VisualStudio. Thanks.

+4
source share
4 answers

From my experience, VideoCapture can read a sequence of images even without specifying a format. For instance. The following works just fine:

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

images are read sequentially:

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

HOWEVER: This only works if the images are in the executable folder. I could not figure out how to specify a directory like this:

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

This seems to be a mistake. An abundant example can be found here:

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

+5
source

In accordance with this link should be:

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

i.e. just one : after C and %2d for a two-digit sequence of file names.

Similarly, your second example should be:

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

use glob as in glob (pathpath, vectorOfimages) then refer to each of the images as vectorOfimages [i].

vectorOfimages

  vector<String> 

and the folder path is a string.

+1
source

We need to declare like this.

 VideoCapture vcap("C:\\pathDirectory\\folderName\\drop%d.bmp"); 

I stated so, and it worked fine for me, because my image names were drop1.bmp, drop2.bmp, drop3.bmp ...... etc.

0
source

Source: https://habr.com/ru/post/1482372/


All Articles