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.

dvilela
  • 1,200
  • 12
  • 29
  • When you commented ```camera.set(cv::CAP_PROP_FPS,30);``` still giving 5? – Yunus Temurlenk Jun 01 '20 at 11:33
  • If I comment that line, it still gives 5 for 4.x, but 30 for 3.x. – dvilela Jun 01 '20 at 11:37
  • 1
    Interesting, did you try to measure the fps manually and then you may compare with the ```CAP_PROP_FPS```. Here is a [link](https://www.learnopencv.com/how-to-find-frame-rate-or-frames-per-second-fps-in-opencv-python-cpp/) to copy and try manually. – Yunus Temurlenk Jun 01 '20 at 11:42
  • Yes: with 3.x I get ~28fps and with 4.x, ~4.4fps – dvilela Jun 01 '20 at 11:47
  • Then the problem is not about the set function – Yunus Temurlenk Jun 01 '20 at 11:54
  • 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 Hardware* – dvilela Jun 01 '20 at 12:04
  • 1
    Yess the default value is 5 as like [here](https://docs.opencv.org/3.4/dd/d01/group__videoio__c.html#ggaf38129fd1797a5cfd7d746065784a44fa5f91733759094693912575b0e4e41f6b). 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? – Yunus Temurlenk Jun 01 '20 at 12:08
  • I'm trying in the same pc. Let me try in another one. – dvilela Jun 01 '20 at 12:09
  • 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 wrong – Yunus Temurlenk Jun 01 '20 at 12:19
  • 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. – dvilela Jun 01 '20 at 12:27
  • 1
    what is the result of the command on terminal ```pkg-config --modversion opencv``` – Yunus Temurlenk Jun 01 '20 at 12:56
  • pkg-config --modversion opencv -> 3.2.0 – dvilela Jun 01 '20 at 12:57
  • 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. – dvilela Jun 01 '20 at 13:50

1 Answers1

1

This is a known bug that affects OpenCV > 3.3

dvilela
  • 1,200
  • 12
  • 29