Ask Your Question

Revision history [back]

If you only display the frames that contain delay you will get a low frame-rate. Not sure if this is what Valdaine already meant, but based on his solution I came up with a way to remove the delay while keeping the original framerate.

First you have to flush the buffer, and then you start capturing the frames faster than the stream framerate (use cv::waitKey(1) or ideally a loop without any interval).

VideoCapture camera("host-address:port");

Mat frame;

flush(camera);

while (cv::waitKey(1) != 27)
{
    camera >> frame;

    cv::imshow("IP Camera", frame);
}

The first frame read from the buffer has a 2ms delay, so the loop must be escaped only after reading the second frame with delay.

void flush(VideoCapture& camera)
{
    int delay = 0;
    QElapsedTimer timer;

    int framesWithDelayCount = 0;

    while (framesWithDelayCount <= 1)   
    {
        timer.start();

        camera.grab();                      

        delay = timer.elapsed();        

        if(delay > 0)
        {
            framesWithDelayCount++;
        }
    }
}