# How to set camera fps with opencv

**URL:** <https://forum.opencv.org/t/how-to-set-camera-fps-with-opencv/7881>\
**Category:** C++\
**Tags:** highgui, videoio\
**Created:** [March 10, 2022, 2:37pm UTC](https://forum.opencv.org/t/how-to-set-camera-fps-with-opencv/7881 "2022-03-10T14:37:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rap](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@rap](https://forum.opencv.org/u/rap)\
**Post date:** [March 10, 2022, 2:37pm UTC](https://forum.opencv.org/t/how-to-set-camera-fps-with-opencv/7881/1 "2022-03-10T14:37:50Z")

</div>

Hi, I want to set camera fps with opencv.  
I don’t know if using cv::waitKey() is good idea to set it.  
For example I have video 10fps and when I get the fps I have only 9fps.  
I want to set the camera to for example 30fps and capture image every 10fps for example.

```auto
      {
       cv::VideoCapture cap(0,200);
        int fps = 10;
        double fps2 = cap.get(cv::CAP_PROP_FPS);
        std::cout << "fps2 : " << fps2 << std::endl;

        if (!cap.isOpened())
        {
            cap.release();
            return -1;
        }

        std::chrono::time_point<std::chrono::high_resolution_clock>
                   prev_frame_time(std::chrono::high_resolution_clock::now());
        std::chrono::time_point<std::chrono::high_resolution_clock>
                           new_frame_time;

        cv::Mat frame;
        while (true)
        {
            if (videoCapture.read(frame) == false)
            {
                if (frame.empty())
                {
                	break;
                }
            }
            else
            {
            	imshow("Frame", frame);

            }

            int key = cv::waitKey(1000/fps);
            if (key % 256 == key_esc)
            {
            	break; // (27)escape key
            }

            new_frame_time = std::chrono::high_resolution_clock::now();
            std::chrono::duration<double> duration1(new_frame_time - prev_frame_time);
            int fps = int(1/duration1.count());
            std::cout << "fps : " << fps << std::endl;

            prev_frame_time = new_frame_time;
        }

        cap.release();
}

```

---

<div class="post-metadata">

**Author:** ![Moddingear](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/moddingear/32/4462_2.png) [@Moddingear](https://forum.opencv.org/u/Moddingear)\
**Post date:** [March 10, 2022, 9:38pm UTC](https://forum.opencv.org/t/how-to-set-camera-fps-with-opencv/7881/2 "2022-03-10T21:38:50Z")

</div>

use `cap->set(CAP_PROP_FPS, fps);`

---

<div class="post-metadata">

**Author:** ![rap](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@rap](https://forum.opencv.org/u/rap)\
**Post date:** [March 11, 2022, 10:03am UTC](https://forum.opencv.org/t/how-to-set-camera-fps-with-opencv/7881/3 "2022-03-11T10:03:38Z")

</div>

it seems that cap-\>set(CAP\_PROP\_FPS, fps); doesn’t work to set camera ?

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [March 11, 2022, 10:32am UTC](https://forum.opencv.org/t/how-to-set-camera-fps-with-opencv/7881/4 "2022-03-11T10:32:13Z")

</div>

exact brand and model of camera, and exact code you tried to use, including the concrete value for `fps`?

---

<div class="post-metadata">

**Author:** ![rap](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@rap](https://forum.opencv.org/u/rap)\
**Post date:** [March 11, 2022, 2:20pm UTC](https://forum.opencv.org/t/how-to-set-camera-fps-with-opencv/7881/5 "2022-03-11T14:20:51Z")

</div>

I’am using a webcam or my phone camera. My camera fps is 30fps  
But I want to set the framerate to 10 for example.

```auto
#include <chrono>
#include <ctime>
#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/videoio.hpp>
#include <iostream>

int main(int argc, const char ** argv)
{
       cv::VideoCapture cap(0,200);
        int fps = 10;
        double fps2 = cap.get(cv::CAP_PROP_FPS);
        std::cout << "fps2 : " << fps2 << std::endl;

        if (!cap.isOpened())
        {
            cap.release();
            return -1;
        }

        std::chrono::time_point<std::chrono::high_resolution_clock>
                   prev_frame_time(std::chrono::high_resolution_clock::now());
        std::chrono::time_point<std::chrono::high_resolution_clock>
                           new_frame_time;

        cv::Mat frame;
        while (true)
        {
            if (videoCapture.read(frame) == false)
            {
                if (frame.empty())
                {
                	break;
                }
            }
            else
            {
            	imshow("Frame", frame);

            }

            int key = cv::waitKey(1000/fps);
            if (key % 256 == key_esc)
            {
            	break; // (27)escape key
            }

            new_frame_time = std::chrono::high_resolution_clock::now();
            std::chrono::duration<double> duration1(new_frame_time - prev_frame_time);
            int fps = int(1/duration1.count());
            std::cout << "fps : " << fps << std::endl;

            prev_frame_time = new_frame_time;
        }

        cap.release();
 return 0;
}

```

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [March 11, 2022, 2:43pm UTC](https://forum.opencv.org/t/how-to-set-camera-fps-with-opencv/7881/6 "2022-03-11T14:43:42Z")

</div>

a camera might just _not support_ a specific frame rate. professional cameras are more flexible there. random webcams have a handful of frame rates they accept.

> [@rap](#):
>
> `int fps = int(1/duration1.count());`

integer rounding. that’s what gives you “9” when the rate should be something close to 10 fps, because the calculation might be exactly 9.99. use proper rounding instead, or print floating point values.

you can’t throttle cameras. they produce frames at a fixed rate. you must read them all. if you don’t, they queue up, and that will cause problems, ranging from high latency (picture is several seconds old) to the device driver giving up and crashing your program.

what you do with `waitKey` there is a bad idea _because of that_. it’s best to use the lowest wait time possible here, which is 1 (or use `pollKey()`). you don’t have to spend all idle time in `waitKey`. it only has to be entered periodically to keep the GUI window somewhat responsive.

the `cap.read()` call will wait anyway, if new frames aren’t yet available.

if you need to do that, run a thread. the thread only reads frames from the camera and puts them somewhere. such a thread could read three frames, and then put one of them into a queue. that would turn 30 fps into 10 fps. your other code could then read from that queue.

---

<div class="post-metadata">

**Author:** ![rap](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@rap](https://forum.opencv.org/u/rap)\
**Post date:** [March 15, 2022, 9:36am UTC](https://forum.opencv.org/t/how-to-set-camera-fps-with-opencv/7881/8 "2022-03-15T09:36:25Z")

</div>

Hi,  
Thank you very much for you answers.  
I read in other forum about the thread way but I want to try without thread.

> what you do with `waitKey` there is a bad idea _because of that_ . it’s best to use the lowest wait time possible here, which is 1 (or use `pollKey()` ). you don’t have to spend all idle time in `waitKey` . it only has to be entered periodically to keep the GUI window somewhat responsive.

Can you be more precise ?I did not understand everything this part cleary.

I modify my code to do something like this instead of using waitKey()

```auto
#include <chrono>
#include <ctime>
#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/videoio.hpp>
#include <iostream>

int main(int argc, const char ** argv)
{
       cv::VideoCapture cap(0,200);
        int fpsCamera = 30;
int fpsCapture = 10;
        double fps2 = cap.get(cv::CAP_PROP_FPS);
        std::cout << "fps2 : " << fps2 << std::endl;

        if (!cap.isOpened())
        {
            cap.release();
            return -1;
        }

        std::chrono::time_point<std::chrono::high_resolution_clock>
                   prev_frame_time(std::chrono::high_resolution_clock::now());
        std::chrono::time_point<std::chrono::high_resolution_clock>
                           new_frame_time;

        cv::Mat frame;
        while (true)
        {
            if (videoCapture.read(frame) == false)
            {
                if (frame.empty())
                {
                	break;
                }
            }

            new_frame_time = std::chrono::high_resolution_clock::now();
            std::chrono::duration<double> duration1(new_frame_time - prev_frame_time);
            double fps = 1/duration1.count();
            std::cout << "fps : " << fps << std::endl;

            if(duration1.count() > 1/fpsCapture)
              {
                        	prev_frame_time = new_frame_time;
                  
            	imshow("Frame", frame);
            }

            int key = cv::waitKey(1000/fpsCamera );
            if (key % 256 == key_esc)
            {
            	break; // (27)escape key
            }
        }

        cap.release();
 return 0;
}

```

I don’t know if it’s the write way.
