Switching from OpenCV 3 to OpenCV 4 causes webcam to record at a max of 5 fps instead the usual 30 fps
Asked Answered
C

1

6

I'm having some trouble since I changed from OpenCV 3.x to 4.x (compiled from source) in my C++ project. I've replicated this behaviour in a small example that just opens a webcam and records for 5 seconds.

With 3.x I am able to set the webcam framerate to 30 at full hd, but the same code with 4.x just ignores the camera.set(cv::CAP_PROP_FPS,30) and sets it to 5 instead. If I use 720p, the fps is set to 10.

Maybe the code is not relevant here, as it's a classical example, but just in case I'll leave it here.

#include "opencv2/opencv.hpp"
#include "iostream"
#include "thread"
#include <unistd.h>

using namespace cv;

VideoCapture camera(0);
bool stop = false;
int fc = 0;

void saveFrames()
{
    while(!stop)
    {
        Mat frame;
        camera >> frame;
        cv::imwrite("/tmp/frames/frame" + std::to_string(fc) + ".jpg", frame);
        fc++;
    }
}

int main()
{
    if(!camera.isOpened())
        return -1;

    camera.set(cv::CAP_PROP_FRAME_WIDTH,1920);
    camera.set(cv::CAP_PROP_FRAME_HEIGHT,1080);
    camera.set(cv::CAP_PROP_FPS,30);

    double fps = camera.get(cv::CAP_PROP_FPS);
    std::cout << "FPS setting: " << fps << std::endl; // 5 with OCV4, 30 with OCV3

    std::thread tr(saveFrames);
    int waitSeconds = 5;
    usleep(waitSeconds * 1e6);
    stop = true;
    tr.join();

    std::cout << "Written " << fc << " frames of " << fps * waitSeconds << std::endl;
    return 0;
}

Edit: more tests with other computers yield the same result except in a Macbook Pro (but running the same distribution) where OpenCV 4.3 seems to work. The other 2 computers are desktops with usb webcams.

Edit 2: same problem with version 3.4 building from source code. For now, only 3.2 from the repo works ok in the two computers with usbcams.

Crumpton answered 1/6, 2020 at 11:18 Comment(13)
When you commented camera.set(cv::CAP_PROP_FPS,30); still giving 5?Anabiosis
If I comment that line, it still gives 5 for 4.x, but 30 for 3.x.Crumpton
Interesting, did you try to measure the fps manually and then you may compare with the CAP_PROP_FPS. Here is a link to copy and try manually.Anabiosis
Yes: with 3.x I get ~28fps and with 4.x, ~4.4fpsCrumpton
Then the problem is not about the set functionAnabiosis
Yes, it seems OpenCV is setting those values as default, but I don't know at what point of the chain the problem happens. According to the docs: Reading / writing properties involves many layers. Some unexpected result might happens along this chain. VideoCapture -> API Backend -> Operating System -> Device Driver -> Device HardwareCrumpton
Yess the default value is 5 as like here. You have 2 different pc and 2 different environments, right? One is opencv 3.x one is 4..x or you try to use 2 version on same environment?Anabiosis
I'm trying in the same pc. Let me try in another one.Crumpton
How its possible you have 2 version of opencv in same environment. There is something wrong with it. You cant have 2 version in the same one for c++, if I am not wrongAnabiosis
I installed version 3 from the repos (libopencv-dev) and after that, downloaded, built and installed version 4 from github. At least the meson build system finds the two of them specifying opencv or opencv4 in the linker dependencies.Crumpton
what is the result of the command on terminal pkg-config --modversion opencvAnabiosis
pkg-config --modversion opencv -> 3.2.0Crumpton
Ok, I've tried in two other computers where opencv was not installed previously. I built 4.3 from source, and I still got 5fps in one of the computers, while in the other (a macbook pro) the fps count seems right.Crumpton
C
1

This is a known bug that affects OpenCV > 3.3

Crumpton answered 1/6, 2020 at 15:13 Comment(1)
Thanks for ur efforts, i was also curiousAnabiosis

© 2022 - 2024 — McMap. All rights reserved.