Ask Your Question

How to read sequentially image files in OpenCv and C++ [closed]

asked 2016-12-05 05:56:01 -0500

KirtiSwagat gravatar image

updated 2016-12-05 06:17:07 -0500

Suppose I have 7 numbers of ".jpg" files in a folder. All the names are numeric that is 0.jpg, 4.jpg, 1.jpg etc. How to read the files sequentially according to there name incrementally. That is first 0.jpg then 1.jpg then 4.jpg will be read.

I have tried with glob( ) function but image read is not sequentially..

#include <opencv2/opencv.hpp>
#include <dirent.h> 
using namespace std;
using namespace cv;
int main()
String folder = "*.jpg";
 vector<String> filenames;
 glob(folder, filenames);
 cout<<filenames.size()<<endl;//to display no of files

  for(size_t i=0; i<filenames.size(),++i)


and the output is as follows 7 // no of files in the folder ./0.jpg ./11.jpg ./14.jpg ./2.jpg ./6.jpg ./8.jpg ./9.jpg

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by KirtiSwagat
close date 2016-12-19 05:44:21.250606


apart from VideoCapture(), - if you'dsave your files with leading zeros, like 00007.jpg, you could sort them properly

berak gravatar imageberak ( 2016-12-05 06:22:03 -0500 )edit

@berak how to do??? is there any modification required in my code.plz suggest

KirtiSwagat gravatar imageKirtiSwagat ( 2016-12-05 06:29:21 -0500 )edit

you'd need to modify your saving code, so it eg. uses format("%05d".jpg) for the filename, then you can just std::sort() the filenames vector from glob.

but again, try with VideoCapture first !

berak gravatar imageberak ( 2016-12-05 06:37:22 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2016-12-05 06:02:15 -0500

mshabunin gravatar image

Use cv::VideoCapture class, it supports described functionality - documentation, tutorial

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2016-12-05 05:56:01 -0500

Seen: 5,285 times

Last updated: Dec 05 '16