Trying to make a time lapse but camera capturing too fast

My images are coming in a few milliseconds after another even with a long delay. I am using a for loop with a delay and then I am using other software to convert the type.

#include <opencv2/core.hpp>

#include <opencv2/highgui.hpp>
#include <opencv2/videoio.hpp>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
cv::namedWindow(“Capture Example”,cv::WINDOW_AUTOSIZE);
cv::VideoCapture capture(0);


cout << “helloworld” << endl;

struct timespec start;
struct timespec delay_time = {1,0};
struct timespec remaining_time;
clock_gettime(CLOCK_MONOTONIC, &start);

string filename;
string jpg = ".jpg";
string ppm =  ".ppm";
string space = " ";
string command;
cv::Mat frame;

for(int i=0; i<5; i++)

bool ret =;

if(!ret) break;

cv::imshow("Capture Example", frame);
char c = cv::waitKey(33);
if( c == 27 ) break;

filename = "image_";
filename += to_string(i);
cout << "hell world" << filename << endl;

imwrite(filename + jpg, frame);

command = "convert " + filename + jpg + space + filename + ppm;

cout << "shell world" << command << endl;

//nanosleep(&delay_time, &remaining_time);


struct timespec end;

clock_gettime(CLOCK_MONOTONIC, &end);

long time = end.tv_nsec - start.tv_nsec;

cout << “helloworld” << time << endl;


discard frames or blend them. for a timelapse, I would highly recommend blending frames. that simulates a long exposure time. blending means adding/averaging. create a matrix of type CV_32FC3 or CV_32UC3 or similar, add frames on top, divide by number at the end.

the camera makes frames to its own fixed frequency (frames per second).

it doesn’t care if you try to throttle that. the frames will just queue up.

as you see, you can’t simply delay this.

Thank you for the feedback. The difficult part is the requirement that I have to take an image every second with little error. I can fill a buffer and see how fast they are coming in and only take every (ig) 30th sample maybe?

you can set the frame rate (FPS), same as you set the width and height properties of the VideoCapture.

read it back (get) to make sure the device accepted the value.