OpenCV: Reading image series from a folder

2019-01-25 20:42发布

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.

4条回答
Rolldiameter
2楼-- · 2019-01-25 21:20

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

查看更多
迷人小祖宗
3楼-- · 2019-01-25 21:28

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");
                      ^^^
查看更多
祖国的老花朵
4楼-- · 2019-01-25 21:39

We need to declare like this

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

I declared like that and it worked fine for me, because my images names were drop1.bmp, drop2.bmp, drop3.bmp ……and so on

查看更多
混吃等死
5楼-- · 2019-01-25 21:43

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.

查看更多
登录 后发表回答