简体   繁体   中英

Can i capture at 20 fps with C++ and Opencv?

i have a Raspberry Pi and installed on it the OpenCV and Guvcview. When i open Guvcview, i get ~ 17-21 fps but when i run a simple program (only capture from webcam and display frame) in C++ with Opencv, i get only 6 fps.

What is wrong? i need to configure Opencv to use Guvcview's configuration? why guvcview get 20 fps? What can i do?

thanks.

PD I've done the same in my computer and i get 29 fps in both cases.

// * ** * ** * ** * ** * ** * ** * ** * ** * ** * **** *this is the code C++ :

 #include <iostream>
    #include "opencv2/opencv.hpp"
    using namespace std;
    using namespace cv;

    time_t start, end; //variabile di tipo time_t , contiene tempo in sec. 
    // inizializzo contatore nella dichiarazione 
    int counter=0;

    int main()
    { time(&start);
    VideoCapture cap(1);
    cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
    cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480); 

    if (!cap.isOpened())
    { cout << "could not capture";
    return 0; }

    Mat frame;
    namedWindow("camera", 1);
    char key = 'a';

    while(key != 27)
    {   cap.read( frame);
    imshow("camera", frame);

    //##################
    //time at the end of 1 show, Stop the clock and show FPS
    time(&end);
    ++counter;
    cout <<"fps: "<< counter/ difftime(end,start) <<endl <<endl;
    //##################

    key = waitKey(3); }

    destroyAllWindows();
    return 0;
    }

OpenCV is a heavy weight API and following tips may introduce minor improvements:

you can disable RGB conversion:

 cap.set(CV_CAP_PROP_CONVERT_RGB , false);

you can increase frame rate if its default frame rate is low:

cap.set(CV_CAP_PROP_FPS , 60);

I'd suggest to do direct video capture via V4L, since OpenCV may do YUYV to RGB transformations and other stuff that involves floating point calculations, that are expensive on this kind of hardware. We have done many robotics projects on embedded systems and the rule of the thumb is that you will be always better of either directly using V4L or small 3rd party libraries like CMVision ( http://www.cs.cmu.edu/~jbruce/cmvision/ ) to do image processing on embedded systems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM