Set Raspberry Camera mode using OpenCV / C++

I'm trying to set the Raspberry Pi Cameras mode using OpenCVs VideoCapture class and setting it's properties with the code below. Setting it to 640x480x30fps works just fine, but 1920x1080x30 fps only delivers 3 or 4 frames per second.

Can anyone tell me what I'm missing? Thanks a lot.

#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>

int main (){
    int height(1080);
    int width(1920);

    cv::VideoCapture cap(0);
    cap.set(CV_CAP_PROP_FRAME_HEIGHT, height);
    cap.set(CV_CAP_PROP_FRAME_WIDTH, width);
    cap.set(cv::CAP_PROP_FOURCC, 0x21);
    cap.set(cv::CAP_PROP_FPS, 30);

    cv::Mat currentFrame;

    while(1){
        cap >> currentFrame;
        //do stuff 
        char c = (char)cv::waitKey(1);
        if (c == 27) break;
    }
}

2 answers

  • answered 2018-11-08 08:07 Ruks

    This is heavily dependent on the memory...

    Your processor is responsible for every kind of computation operation there is...

    So, in order to render in high resolution, your processor has to have a high-end capacity power too, optimizing code is just to reduce CPU load...

    See here,

    1920 * 1080 * 30 = 62208000 pixels (More resolution, more memory)

    640 * 480 * 30 = 9216000 pixles (Less resolution, less memory)

    Your device has to render these pixels one by one so it is normal for the frame rate to drop, your computer has to have a great memory to compute 62208000 pixels for 1920x1080 in one second...


    Edit: Also, I would like you to look at this article demonstrating why we prioritize frame rate over resolution...

  • answered 2018-11-08 08:26 user9754798

    Have you ever tried playing a relatively modern game on a $100 graphics card? Same difference.

    The Raspberry Pi doesn't have the processing power or memory capable of capturing high quality video. That's why 640x480 works fine, but as soon as you increase the resolution the FPS nosedives.

    Optimization of your code could help, but there's a finite amount of processing power capable from your Raspberry Pi.