Currently, we are creating a program that captures still images at the fps specified by the Web camera.
A method is adopted in which the elapsed time is manually acquired and the image is taken every time a certain amount of time elapses.

Error message

It seems that it takes more time than necessary to shoot the camera image, so I can't shoot with FPS as I wanted because of the delay.
The purpose of this program is to be able to shoot with 20FPS with as little error as possible.
I thought that the shutter time might be long, but how can I set the shutter time in OpenCV?

Applicable source code
# include "main.h"
int main (void) {
    int fps = 10;// FPS specified
    double spf = 1.0/fps;
    const int maxCount = 100;
    int i;
    int key;
    LARGE_INTEGER start, now;
    double time;
    double count;
    char name [256];
    cv :: Mat frame;
    cv :: VideoCapture cap (0);
    if (! cap.isOpened ()) {
        return -1;
    cap.set (CV_CAP_PROP_FPS, 20);// What are you setting?
    cap.set (CV_CAP_PROP_FRAME_WIDTH, 640);
    cap.set (CV_CAP_PROP_FRAME_HEIGHT, 480);

    if (! QueryPerformanceFrequency (&freq)) {// Get the unit of elapsed time
        return -1;// get failed
    if (! QueryPerformanceCounter (&start)) {// Get the first elapsed time
        return -1;
    count = 1.00;
    while (1) {
        if (! QueryPerformanceCounter (&now)) {// Get current elapsed time
            return -1;
        time = (double) (now.QuadPart-start.QuadPart)/freq.QuadPart;// Elapsed time calculation
        if (time>= count) {// Shoot if more than count has elapsed.
            // printf ("count =% f:% .3f:", count, time);
            if (! QueryPerformanceCounter (&now)) {
                return -1;
            time = (double) (now.QuadPart-start.QuadPart)/freq.QuadPart;// Calculate elapsed time after shooting
            //printf("%.3f\n ", time);
            sprintf_s (name, "% .3f.jpg", time);
            cv :: imwrite (name, frame);
            cv :: imshow ("Capture", frame);
            count + = spf;
        key = cv :: waitKey (1);
        if (key == 0x1b)
    getchar ();
    return 0;
Supplemental information (FW/tool version etc.)

OpenCV 3.4.1

  • Answer # 1

    Does cv :: imwrite () take too long?

  • Answer # 2

    I think so too.

    time-count should give you an error for the time you want to release the shutter, so if you print it out and comment out cv :: imwrite, you can see it.
    If so, why not put it in memory instead of a file?

  • Answer # 3


    cap.set (CV_CAP_PROP_FPS, 20);// What are you setting?

    I think the camera is set to 20fps here. (It may not be possible to set depending on the camera)
    If it is set to 20fps, because it is 20fps at the maximum, it may be 20fps + α time if you put the processing in between, so I think that it is better to test out here by commenting out or deleting the whole timer.

    As a hardware problem, there is a web camera that can lower the fps by itself to increase the amount of light in a dark place.
    So if you shoot in a dark place, you can also test it in a bright place.

  • Answer # 4

    As a practical example of using 40FPS with a webcam in practice, OpenCV's VideoLibrary is intended to be used for general purposes, so the actual FPS is absolutely slower than the theoretical value. In practice, you should be able to create a DirectX library, capture it with 32Bits, convert it to 24Bits, and set it to compress the webcam.

    I have answered from the past in the actual example, but I will try to sample with EWCLIB (it will come out as soon as I googled, a very helpful implementation example that is defined entirely in the header). In addition, you can set each resolution, compression format, FPS and display the result of actual execution in the example.