I am developing a real-time image analysis project using C ++ and OpenCV, which require frames to be retrieved from a webcam. I'm having problems trying to extract these frames at any speed - currently I can only manage 18 frames per second. Here is a simple code that I use to extract frames from a 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 tried other codes, but no one allows me to retrieve frames faster than 18 frames per second. I hope to achieve a speed similar to what I can achieve in Matlab at a speed of 40-50 frames per second (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 looked at using mex files to speed up my C ++ project, as well as GPU / CUDA support, but I ran into some hardware problems, so I saw if there was a simpler approach or something that I was missing in my current code.
Thanks in advance!
EDIT I just checked the code performance analysis and there are a few sticky points, namely:
VideoCapture cap(0); 10.5% cap >> frame; 36.8% imshow("video", frame); 31.6%
source share