0

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;
    }
}
Ruks
  • 3,886
  • 1
  • 10
  • 22
  • Have you tried increasing the FPS? I think this is a performance issue... – Ruks Nov 08 '18 at 07:54
  • 1
    The first thing to do is to remove the all the *"stuff"* that you do in the middle of the loop to see if the hardware is capable of simply acquiring and discarding frames at that rate. The next thing to do is benchmark how long the *"stuff"* takes on its own without acquiring any new data. Only when you know where the time is being used up can you start to optimise. – Mark Setchell Nov 08 '18 at 09:16
  • @MarkSetchell: This is of course the first thing that I did, and it is shown [here](https://stackoverflow.com/questions/53174085/opencv-c-how-to-effectively-set-raspberry-cam-frame-rate). Regarding the answers here I'm not really getting it now, because assuming that the measurement is correct, CPU time for capturing is only around 0.025 seconds and should allow for almost 40fps. – TonySoprano Nov 08 '18 at 19:42
  • Ugh! Why have you got 2 questions about the same thing? You also appear to have `waitKey()` in your code - without any images being displayed. – Mark Setchell Nov 08 '18 at 19:46
  • Partly because I'm new to SO and still figuring out how it works, and partly because when I formulated the first thread I was still hooked up on the video recording problem, not entirely realizing that capture output might be the crucial problem. WaitKey is a residue of former experiments and I also used it to terminate longer loops without having to Ctrl-C the program. Shouldn't do any harm though? – TonySoprano Nov 09 '18 at 18:27

2 Answers2

1

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.

0

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...

Community
  • 1
  • 1
Ruks
  • 3,886
  • 1
  • 10
  • 22
  • @MarkSetchell Yes, I see that... Looks like it is already optimized to how much it can be... – Ruks Nov 08 '18 at 15:43
  • That makes a lot of sense. This is my first project in this configuration. I didn't give the numbers much thought and greenly presumed that a 2MP video stream isn't much of a wild thing these days... – TonySoprano Nov 08 '18 at 19:33