Ask Your Question

Revision history [back]

imshow() "speed upping" cap >> img;

Hello guys, here is the strange stuff i found, calling imshow() and waitKey() functions after a cap >> img; in a loop can speedup the grabbing by halving the time (in my webcam from ~80ms to 40~ms), can someone explain my what's going on? There is a way for having a "fast" grabbing without the imshow()? Here's the code snippet (need C++11 and #include <chrono>):

     VideoCapture capture(0);
 capture.set(CV_CAP_PROP_FRAME_WIDTH, 640);
 capture.set(CV_CAP_PROP_FRAME_HEIGHT, 360);
 capture.set(CV_CAP_PROP_FPS, 30);
 if(!capture.isOpened()){
 cout << "Failed to connect to the camera." << endl;
 }

 Mat frame;
 int i = 60;

 while(1){

 auto t1 = chrono::high_resolution_clock::now();
 capture >> frame;
 auto t2 = chrono::high_resolution_clock::now();

 cout << "grabbing ms: " << chrono::duration_cast<std::chrono::milliseconds>(t2-t1).count() << endl;

 //COMMENT imshow() and see the capture >> frame; taking twice the time
 imshow("Debug",tmpFrame);

 i--; cout << i << endl;
     if(waitKey(32) == 27 || i < 0){
     capture.release();
     return 0;
     }
 }