2

I'm using a webcam supporting 1280 x 720 @ 60 fps.

My computer environment is intel i5-4690K and Windows7, Visual studio 2015, opencv 3.1

When I run the webcam in Kinovea(0.85.15, https://www.kinovea.org/), the camera run at the 1280 x 720 @ 60fps.

But, In Visual studio with Opencv, it isn't work @ 60 fps.

It just work only 12~15 fps.

My code for checking the camera fps is below.

#include <stack>
#include <iostream>
#include <math.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include <opencv2/video.hpp>
#include "opencv2/imgcodecs.hpp"
#include <time.h>



using namespace cv;
using namespace std;


int keyboard;


int main(int argc, char** argv)
{

    VideoCapture cap(0); //capture the video from web cam

    if (!cap.isOpened())  // if not success, exit program
    {
        cout << "Cannot open the web cam" << endl;
        return -1;
    }
    cap.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
    cap.set(CV_CAP_PROP_FRAME_HEIGHT, 720);

    while ((char)keyboard != 'q' && (char)keyboard != 27)

    {
        Mat imgOriginal;
        Mat ROOI;

        clock_t a = clock();
        bool bSuccess = cap.read(imgOriginal); 

        if (!bSuccess)
        {
            cout << "Cannot read a frame from video stream" << endl;
            break;
        }
        printf("Captue Time : %f\n", double(clock() - a) / double(CLOCKS_PER_SEC));

        imshow("Original", imgOriginal);

        if (waitKey(1) == 27) 
        {
            cout << "esc key is pressed by user" << endl;
            break;
        }
    }

    return 0;

}

In above code. I check the "Capture Time" and it was usually records 0.07s ~ 0.09s.

So, I attempt to VideoCapture::set(CV_CAP_PROP_FPS, 60), but it isn't work. (When I get the FPS using the code VideoCapture::get(CV_CAP_PROP_FPS), it return value 0.)

How can I control the webcam FPS?

Thanks.

H.Ann
  • 61
  • 1
  • 1
  • 4
  • Have you seen this thread? ( http://stackoverflow.com/questions/19662193/opencv-videocapturegetcv-cap-prop-fps-returns-0-fps ) - webcams generally have a fixed framerate that cannot be changed. – Dai Dec 08 '16 at 18:22
  • @Dai Thank you for your reply. But, I can't understand how it works 60 fps in Kinovea program, if webcams have a fixed framerate. What is different between in Kinovea and in opencv? Thanks. – H.Ann Dec 09 '16 at 14:24
  • Strongly depends on whether the camera supports various input levels. I can give you an example how OpenCV+OpenNI allow to set VideeCapture (work with Kinect): `modeRes = m_capture.set(CAP_OPENNI_IMAGE_GENERATOR_OUTPUT_MODE, X);`, where `X` in that case is one of: `{CAP_OPENNI_VGA_30HZ, CAP_OPENNI_SXGA_15HZ, CAP_OPENNI_SXGA_30HZ}`. I suppose OpenNI encodes it within the resolution setting. – hauron Dec 10 '16 at 16:00
  • @hauron Thanks for reply. I think that your opinion is to try. I will try that. Thanks! – H.Ann Dec 13 '16 at 17:18

2 Answers2

1

When I modify my code like below, it works @ 60 fps.

#include <stack>
#include <iostream>
#include <math.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include <opencv2/video.hpp>
#include "opencv2/imgcodecs.hpp"
#include <time.h>



using namespace cv;
using namespace std;


int keyboard;


int main(int argc, char** argv)
{

    VideoCapture cap(0); //capture the video from web cam

    if (!cap.isOpened())  // if not success, exit program
    {
        cout << "Cannot open the web cam" << endl;
        return -1;
    }
    cap.set(CV_CAP_PROP_FOURCC, CV_FOURCC('M', 'J', 'P', 'G'));
    cap.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
    cap.set(CV_CAP_PROP_FRAME_HEIGHT, 720);

    while ((char)keyboard != 'q' && (char)keyboard != 27)

    {
        Mat imgOriginal;
        Mat ROOI;

        clock_t a = clock();
        bool bSuccess = cap.read(imgOriginal); 

        if (!bSuccess)
        {
            cout << "Cannot read a frame from video stream" << endl;
            break;
        }
        printf("Captue Time : %f\n", double(clock() - a) / double(CLOCKS_PER_SEC));

        imshow("Original", imgOriginal);

        if (waitKey(1) == 27) 
        {
            cout << "esc key is pressed by user" << endl;
            break;
        }
    }

    return 0;

}

The key for camera working @ 60 fps is

cap.set(CV_CAP_PROP_FOURCC, CV_FOURCC('M', 'J', 'P', 'G'));

My camera works @60 fps in MJPG mode. So I add above code, it works fine!

H.Ann
  • 61
  • 1
  • 1
  • 4
  • 2
    Very likely the camera needs to switch to M-JPEG to not hit the USB 2.0 limit or something. Please note, that this is only a solution for your camera, it won't help anyone with a different one, even if that camera supports 60 FPS. Also, `CV_CAP_PROP_FPS` _sometimes_ works... – Tomasz Gandor Jan 16 '18 at 21:35
0

You could try setting the camera's frame rate outside of OpenCV, e.g. on Linux you can control UVC cameras (Logitech, etc.) using libwebcam, and uvcdynctrl in particular.

szym
  • 5,606
  • 28
  • 34
  • Thanks for reply. I wonder that the libwebcam and uvcdynctrl are able to control the webcams's fps? Now, I'm using "Video4Linux Control Panel" in Linux. I can control Brightness, Exposure, and so on over Video4Linux, but it isn't support control webcam's fps. How are they? Thanks. – H.Ann Dec 09 '16 at 14:35
  • Some cameras might simply not support different fps As @Dai pointed in a comment above. – szym Dec 10 '16 at 07:56
  • Thanks for your reply. And I got the problem that the camera support 60 fps, but it isn't works 60 fps in opencv. The problem is camera connecting method. My camera support 60 fps over MJPG. When I debug above code, camera works over YUY2. So, I add a code like below. 'cap.set(CV_CAP_PROP_FOURCC, CV_FOURCC('M', 'J', 'P', 'G'));' 'cap.set(CV_CAP_PROP_FRAME_WIDTH, 1280);' And, it works fine @ 60 fps. Thanks! – H.Ann Dec 10 '16 at 15:45