Set Raspberry Camera mode using OpenCV / C++











up vote
0
down vote

favorite












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;
}
}









share|improve this question









New contributor




TonySoprano is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Have you tried increasing the FPS? I think this is a performance issue...
    – Ruks
    4 hours ago










  • 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
    2 hours ago















up vote
0
down vote

favorite












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;
}
}









share|improve this question









New contributor




TonySoprano is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Have you tried increasing the FPS? I think this is a performance issue...
    – Ruks
    4 hours ago










  • 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
    2 hours ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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;
}
}









share|improve this question









New contributor




TonySoprano is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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;
}
}






c++ performance opencv raspberry-pi screen-resolution






share|improve this question









New contributor




TonySoprano is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




TonySoprano is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 1 hour ago









Ruks

445111




445111






New contributor




TonySoprano is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 4 hours ago









TonySoprano

111




111




New contributor




TonySoprano is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





TonySoprano is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






TonySoprano is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Have you tried increasing the FPS? I think this is a performance issue...
    – Ruks
    4 hours ago










  • 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
    2 hours ago


















  • Have you tried increasing the FPS? I think this is a performance issue...
    – Ruks
    4 hours ago










  • 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
    2 hours ago
















Have you tried increasing the FPS? I think this is a performance issue...
– Ruks
4 hours ago




Have you tried increasing the FPS? I think this is a performance issue...
– Ruks
4 hours ago












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
2 hours ago




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
2 hours ago












2 Answers
2






active

oldest

votes

















up vote
1
down vote













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.






share|improve this answer








New contributor




user9754798 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    up vote
    0
    down vote













    This is heavily dependent on the memory and what you are asking is dependent on optimization, so, out of the many ways to optimize your code, you could do something like:



    cv::Mat currentFrame;
    cv::Mat lastFrame;

    cap >> lastFrame;
    while(1)
    {
    cap >> currentFrame;
    // We skip the frame if it is equal to the last frame, minimally optimizing it to
    // some extent...
    if (currentFrame == lastFrame) continue;
    if (currentFrame.empty()) break;
    lastFrame = currentFrame;
    //do stuff
    if( cv::waitKey(1) == 27 ) break;
    }



    But remember, this doesn't mean that you don't have anything to do with your hardware, 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...






    share|improve this answer























    • Not sure I "see" your optimisation, it represents a bigger CPU load than the OP's question as you are comparing 62 megapixels per frame. Also, it is virtually certain that no two frames will be identical even in a still scene - lighting flickers, noise occurs...
      – Mark Setchell
      2 hours ago











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    TonySoprano is a new contributor. Be nice, and check out our Code of Conduct.










     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203345%2fset-raspberry-camera-mode-using-opencv-c%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    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.






    share|improve this answer








    New contributor




    user9754798 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      1
      down vote













      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.






      share|improve this answer








      New contributor




      user9754798 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















        up vote
        1
        down vote










        up vote
        1
        down vote









        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.






        share|improve this answer








        New contributor




        user9754798 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        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.







        share|improve this answer








        New contributor




        user9754798 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        user9754798 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 3 hours ago









        user9754798

        162




        162




        New contributor




        user9754798 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        user9754798 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        user9754798 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.
























            up vote
            0
            down vote













            This is heavily dependent on the memory and what you are asking is dependent on optimization, so, out of the many ways to optimize your code, you could do something like:



            cv::Mat currentFrame;
            cv::Mat lastFrame;

            cap >> lastFrame;
            while(1)
            {
            cap >> currentFrame;
            // We skip the frame if it is equal to the last frame, minimally optimizing it to
            // some extent...
            if (currentFrame == lastFrame) continue;
            if (currentFrame.empty()) break;
            lastFrame = currentFrame;
            //do stuff
            if( cv::waitKey(1) == 27 ) break;
            }



            But remember, this doesn't mean that you don't have anything to do with your hardware, 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...






            share|improve this answer























            • Not sure I "see" your optimisation, it represents a bigger CPU load than the OP's question as you are comparing 62 megapixels per frame. Also, it is virtually certain that no two frames will be identical even in a still scene - lighting flickers, noise occurs...
              – Mark Setchell
              2 hours ago















            up vote
            0
            down vote













            This is heavily dependent on the memory and what you are asking is dependent on optimization, so, out of the many ways to optimize your code, you could do something like:



            cv::Mat currentFrame;
            cv::Mat lastFrame;

            cap >> lastFrame;
            while(1)
            {
            cap >> currentFrame;
            // We skip the frame if it is equal to the last frame, minimally optimizing it to
            // some extent...
            if (currentFrame == lastFrame) continue;
            if (currentFrame.empty()) break;
            lastFrame = currentFrame;
            //do stuff
            if( cv::waitKey(1) == 27 ) break;
            }



            But remember, this doesn't mean that you don't have anything to do with your hardware, 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...






            share|improve this answer























            • Not sure I "see" your optimisation, it represents a bigger CPU load than the OP's question as you are comparing 62 megapixels per frame. Also, it is virtually certain that no two frames will be identical even in a still scene - lighting flickers, noise occurs...
              – Mark Setchell
              2 hours ago













            up vote
            0
            down vote










            up vote
            0
            down vote









            This is heavily dependent on the memory and what you are asking is dependent on optimization, so, out of the many ways to optimize your code, you could do something like:



            cv::Mat currentFrame;
            cv::Mat lastFrame;

            cap >> lastFrame;
            while(1)
            {
            cap >> currentFrame;
            // We skip the frame if it is equal to the last frame, minimally optimizing it to
            // some extent...
            if (currentFrame == lastFrame) continue;
            if (currentFrame.empty()) break;
            lastFrame = currentFrame;
            //do stuff
            if( cv::waitKey(1) == 27 ) break;
            }



            But remember, this doesn't mean that you don't have anything to do with your hardware, 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...






            share|improve this answer














            This is heavily dependent on the memory and what you are asking is dependent on optimization, so, out of the many ways to optimize your code, you could do something like:



            cv::Mat currentFrame;
            cv::Mat lastFrame;

            cap >> lastFrame;
            while(1)
            {
            cap >> currentFrame;
            // We skip the frame if it is equal to the last frame, minimally optimizing it to
            // some extent...
            if (currentFrame == lastFrame) continue;
            if (currentFrame.empty()) break;
            lastFrame = currentFrame;
            //do stuff
            if( cv::waitKey(1) == 27 ) break;
            }



            But remember, this doesn't mean that you don't have anything to do with your hardware, 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...







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 2 hours ago

























            answered 3 hours ago









            Ruks

            445111




            445111












            • Not sure I "see" your optimisation, it represents a bigger CPU load than the OP's question as you are comparing 62 megapixels per frame. Also, it is virtually certain that no two frames will be identical even in a still scene - lighting flickers, noise occurs...
              – Mark Setchell
              2 hours ago


















            • Not sure I "see" your optimisation, it represents a bigger CPU load than the OP's question as you are comparing 62 megapixels per frame. Also, it is virtually certain that no two frames will be identical even in a still scene - lighting flickers, noise occurs...
              – Mark Setchell
              2 hours ago
















            Not sure I "see" your optimisation, it represents a bigger CPU load than the OP's question as you are comparing 62 megapixels per frame. Also, it is virtually certain that no two frames will be identical even in a still scene - lighting flickers, noise occurs...
            – Mark Setchell
            2 hours ago




            Not sure I "see" your optimisation, it represents a bigger CPU load than the OP's question as you are comparing 62 megapixels per frame. Also, it is virtually certain that no two frames will be identical even in a still scene - lighting flickers, noise occurs...
            – Mark Setchell
            2 hours ago










            TonySoprano is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            TonySoprano is a new contributor. Be nice, and check out our Code of Conduct.













            TonySoprano is a new contributor. Be nice, and check out our Code of Conduct.












            TonySoprano is a new contributor. Be nice, and check out our Code of Conduct.















             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203345%2fset-raspberry-camera-mode-using-opencv-c%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Landwehr

            Reims

            Javascript gets undefined on array