Ask Your Question

IP Camera Capture delay/lag

asked 2016-09-22 08:48:21 -0600

Leonardo gravatar image

updated 2016-09-28 08:19:55 -0600


I am trying to so some processing on a IP Camera (Onvif) output image, and it works well, but I see a lag between the real world and the video capture in about 3~4 seconds.

I am using the rtsp:// protocol.

This camera have a web interface (IE / ActiveX) that shows the image with very low lag. (about 200~300 ms).

I have no idea why it's so slow on OpenCV. I wold like some tips to make the capture faster.

edit: It May have something to do with VideoCapture buffer. Is there a way to make it aways use the latest image?

edit2: I could get good lag results on VLC, after reducing cache/buffer to 300ms, I believe network and processing are not to blame. Now I got a stronger feeling that cache is to blame

Thank you!

You can see the code I am using bellow, i also included the output:


#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <thread>
#include <unistd.h>

using namespace cv;
using namespace std;

int main(int argc, char *argv[])
    cout << getBuildInformation();
    VideoCapture cap;
    cout << "open stream: " << argv[1] << endl;
    cout << "stream open!\n";

    Mat test;
        cap >> test;

        cout << "Empty!\n";
    } else {
        imshow("imagem", test);
} while(waitKey(1) < 0);



leonardo@LeonardoB:~/Desktop/Ether_Test/build$ make ; ./EtherTest rtsp://admin:[email protected]:554/ucast/11
Scanning dependencies of target EtherTest
[ 50%] Building CXX object CMakeFiles/EtherTest.dir/main.cpp.o
[100%] Linking CXX executable EtherTest
[100%] Built target EtherTest

General configuration for OpenCV 3.1.0-dev =====================================
  Version control:               3.1.0-1362-gcde9d64

  Extra modules:
    Location (extra):            /home/leonardo/OpenCV/OpenCV3/opencv_contrib/modules
    Version control (extra):     3.1.0-297-g246ea8f

    Timestamp:                   2016-09-22T13:20:09Z
    Host:                        Linux 4.7.4-1-ARCH x86_64
    CMake:                       3.6.2
    CMake generator:             Unix Makefiles
    CMake build tool:            /usr/bin/make
    Configuration:               Release

    Built as dynamic libs?:      NO
    C++ Compiler:                /usr/bin/c++  (ver 6.2.1)
    C++ flags (Release):         -fPIC   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -ffunction-sections -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
    C++ flags (Debug):           -fPIC   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -ffunction-sections -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
    C Compiler:                  /usr/bin/cc
    C flags (Release):           -fPIC   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wno-narrowing -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -ffunction-sections -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
    C flags (Debug):             -fPIC   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations ...
edit retag flag offensive close merge delete


It May have something to do with VideoCapture buffer. Is there a way to make it aways use the latest image?

Leonardo gravatar imageLeonardo ( 2016-09-28 13:17:21 -0600 )edit

I could get good lag results on VLC, after reducing cache/buffer to 300ms, I believe network and processing are not to blame. Now I got a stronger feeling that cache is to blame

Leonardo gravatar imageLeonardo ( 2016-09-28 13:17:28 -0600 )edit

I am also like the poster not happy with the delay but in my case it is about 1 second and VLC is about the same. If I set the VLC cache to anything less than 600ms, it drops all frames on the floor as late. There is a way to tell VLC not to drop late frames which may help but I forgot where to find that option.

wreuven gravatar imagewreuven ( 2017-01-19 08:49:00 -0600 )edit

Real answer is:You need to grab as fast as the camera fps, else it goes to a buffer, and it causes lag.

Leonardo gravatar imageLeonardo ( 2017-02-08 13:42:41 -0600 )edit

hello is there any way to have less than a few hundred milliseconds lag on an ip camera? I want to use an infrared camera to focus on an object 1-2 inches away. What camera can have this short focal length ? or do i neeed to use a USB camera ?

sudhir gravatar imagesudhir ( 2017-08-15 04:27:14 -0600 )edit

I have same question。I change IP Camera' fps to 1,and the delay change to 2s.

samtang gravatar imagesamtang ( 2017-08-17 03:45:57 -0600 )edit

1 answer

Sort by » oldest newest most voted

answered 2016-09-27 18:26:51 -0600

Check your encoder settings record tab> file format >configure

This is from the help file:

The Quality setting when set at 50% is designed to produce visually lossless output. You may increase this value to further refine the output or lower it as necessary to reduce hard drive space used. When the encoder is used for streaming, you should enable the Rate control option for Max bitrate. This option will insure that the outbound network bandwidth falls below a specified value in order to provide a smooth streaming experience. You may also select a Maximum keyframe interval, also known as the Group of Pictures (GOP). When streaming, using a higher value is acceptable as it raises the quality/bandwidth equation. When recording, however, this value directly affects file seeking (random access) times and efficiency and should be kept relatively low at the expense of using additional storage resources. B-frames offer further advanced compression technology, at the expense of some output latency (delay). Under Advanced you may select an encoding Preset. The "faster" presets will use less CPU, while the "slower" presets will produce a higher quality output. Use the Zero frame latency option along with 0 b-frames in order to produce encoder output that is closest to real-time, which may be desirable for streaming. However if you can tolerate some latency, leaving this option un-checked will produce significantly higher quality output.

edit flag offensive delete link more


Thanks for your asnwer!

Do you mean directly on Camera? If so, rtsp works 'well' on VLC, after I set network cache/buffer time to 300ms. So I believe there is no limitation on processing and the problem is something on network cache time on OpenCV that I may not be knowing how to set.

Can you help me with that?

Leonardo gravatar imageLeonardo ( 2016-09-28 08:24:01 -0600 )edit

I didn't know you could open a rtsp url directly from opencv?

 open stream: rtsp://admin:[email protected]:554/ucast/11
(ERROR)icvOpenAVI_XINE(): Unable to open source 'rtsp://admin:[email protected]:554/ucast/11'
stream open!

You mention lag but from the output it looks like you don't get it to open at all ? The only way i currently know of to open a ip camera is by using a mjpg url.

atv gravatar imageatv ( 2016-09-29 04:31:57 -0600 )edit

@atv old comment but the answer to your question is that it looks like your opencv was built with cmake option WITH_XINE=ON and WITH_FFMPEG=OFF. For RTSP, you need the WITH_FFMPEG=ON.

wreuven gravatar imagewreuven ( 2017-01-19 08:43:57 -0600 )edit

Question Tools



Asked: 2016-09-22 08:48:21 -0600

Seen: 16,927 times

Last updated: Sep 28 '16