views:

77

answers:

3

Hello,

I'm trying to display a video file at 25fps smoothly without any lag. The code below does this, but only achieves about 10fps, taking about 0.1ms to execute. With cvWaitKey(1) I get around 0.03 to 0.04ms, which would be perfect, but the named window just stays grey and doesn't show the video!

Is this because cvShowImage() is too slow? Is there any other way to speed up the code and output the video smoothly?

See my code below.

Thanks a lot in advance,

Adrian

#include <cv.h>
#include <iostream>
#include <highgui.h>
#include <cxcore.h>
#include <cvaux.h>
#include <sstream>
#include <time.h>
using namespace std;
using namespace cv;

int main(int argc, char** argv)
{
    CvCapture* vid = 0;

    IplImage* input; //Input image

    int fps;
    int i=0;

    clock_t start, end;

    /*Creates the GUI to output the processed images*/
    cvNamedWindow("Video input", 0);

    /*Open input video*/
    if (argc!=2)
    {
 cout << "Please specify an input video." << endl;
 return -1;
    }
    else
    {
 vid=cvCreateFileCapture(argv[1]);
 if (!vid)
 {
     cout << "Could not extract frame." << endl;
     return -1;
 }
    }

    input = cvQueryFrame(vid);

    fps = (int)cvGetCaptureProperty(vid, CV_CAP_PROP_FPS);
    cout << fps << endl;

    cout << "Video found." << endl;
    /*Extraction loop */
    while (input)
    {
 start = clock();

 cout << flush;
 cout << i << "\r";

 i++;

 /*Show image*/
 cvShowImage("Video input", input);

 cvWaitKey(2); //Wait is needed or else we see a grey box

 input = cvQueryFrame(vid); //Read next frame from video
 end = clock();
 cout << (double)(end-start)/CLOCKS_PER_SEC << " s" << endl;
    };

    /*Release the allocated memory for the frames */
    cvReleaseImage(&input);
    cvDestroyWindow("Video input");
    return 1;
}
+1  A: 

Have you tried this without all the cout stuff?

The debug build of the Microsoft STL has cout handling which is almost unbelievably slow.

Will Dean
Yeah, that did speed it up a lot! Btw, I'm not using the Microsoft STL, but g++. Does anyone have any idea why I can't use cvWaitKey(1) ?
Adrian
You can... I just did in a program I was working on.
Utkarsh Sinha
Definitely does not work for me, I just get a gray screen when I use cvWaitKey(1). I guess it must have sth to do with this specific build..
Adrian
A: 

Try calling cvWaitKey with 1000 / fps wanted, in your case :

cvWaitKey(1000/25)

dnul
That would work fine if I wasn't doing any processing. But since I am, this slows it down even more.
Adrian
A: 

You could try something like:

char key = cvWaitKey(10); //waits 10 milliseconds

if (key == 27)            //and if ESC is pressed, get out of the loop
  break;
karlphillip