Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast extraction of frames from webcam: C++ & OpenCV vs. Matlab

I've been developing a real-time image analysis project using C++ and OpenCV that requires frames to be extracted from a webcam. I'm running into problems trying to extract these frames at any kind of speed - currently I can only manage approximately 18 fps. Here is the simple code I'm using to extract frames from webcam:

#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <ctime>

using namespace std;
using namespace cv;

int main (int argc, char* argv[])
{

    VideoCapture cap(0);
    if(!cap.isOpened()) return -1;
    namedWindow("video", CV_WINDOW_AUTOSIZE);

    clock_t start = clock();

    for (int i = 0; i < 101; ++i)
    {
        Mat frame;
        cap >> frame;
        imshow("video", frame);
        waitKey(1);
    }

    clock_t finish = clock();

    double time_elapsed = (finish - start) / 1000.0;
    double fps = 100 / time_elapsed;

    cout << "\n\nTIME: " << time_elapsed << "\n\nFPS: " << fps << "\n\n";

    return 0;
}

I've tried other codes but none allow me to extract the frames faster than 18 fps. I'm hoping to reach speeds similar to what I can achieve in Matlab of 40 - 50 fps (using the following code):

vid = videoinput('winvideo', 1, 'MJPG_640x480');
triggerconfig(vid, 'manual');    
start(vid);

tic;

for k = 1:100;
    clc;
    disp(k);
    I = peekdata(vid, 1);
    imshow(I);
    drawnow;
end

toc;

close();
stop(vid);
delete(vid);

I've looked at using mex files to speed up my C++ project and also enabling GPU / CUDA support but I've hit some hardware problems so I was seeing if there is a simpler approach or something I'm missing in my current code.

Thanks in advance!

EDIT I just ran a performance analysis on the code and there's a few sticky points namely:

VideoCapture cap(0);            10.5%
cap >> frame;                   36.8%
imshow("video", frame);         31.6%
like image 495
MSTTm Avatar asked Oct 18 '22 20:10

MSTTm


1 Answers

waitkey(1) is slowing you down. You can try to do it on each, for example, tenth iteration. See http://answers.opencv.org/question/52774/waitkey1-timing-issues-causing-frame-rate-slow-down-fix/

Your way of calculating FPS is rather bad. Try
double time_elapsed = (finish - start) / CLOCKS_PER_SEC; It is not guaranteed that CLOCKS_PER_SEC == 1000

like image 174
V. Kravchenko Avatar answered Oct 21 '22 16:10

V. Kravchenko