Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

IP camera video corruption - solutions?

Has anyone been able to get an HD (720p or higher) IP camera working with OpenCV consistently and reliably? If so, did you use the standard VideoCapture interface or something else?

Many OpenCV users such as here and here have reported similar problems with using OpenCV to receive video from network cameras. The typical problem involves getting a bunch of decode errors in the console as well as seeing the image corrupted as follows:

image description

Some have reported that appending '?tcp' to the camera URL fixes the problem at the cost of latency, but this fix works only sometimes.

To characterize the problem, I've run a simple OpenCV video display program under various operating systems and configurations and each of the configurations below gets decoding errors:

  • Windows, OCV 2.4.11, with WIN32 graphics, no TBB
  • Windows, OCV 2.4.11, without WIN32 graphics, with TBB
  • Windows, OCV 2.4.11, with WIN32 graphics, with TBB
  • Windows, OCV 3, with WIN32 graphics
  • Windows, OCV 3, without WIN32 graphics, with TBB
  • Ubuntu, OCV 2.4.11, with ffmpeg, TBB and graphics
  • Ubuntu, OCV 2.4.11, no ffmpeg "Couldn't open the video file."
  • RPi Raspbian, OCV 2.4.11, with TBB and graphics

For testing, I'm using a Hikvision DS-2CD2032-i 1080p color H.264 IP camera. As a control case, I used the ffplay utility of FFMPEG to play the RTSP video stream from this camera for a day and a night with no errors.

I also tested OpenCV 2.4.11 and 3.0.0 beta under Windows 7SP1 x64, and OpenCV 2.4.11 under Ubuntu and Raspbian, with and without TBB.

Every configuration using OpenCV experienced decode errors, though under Linux the errors were less frequent. If I first record the video stream from the camera to a disk file then play the video file from disk, there are no errors.

The errors seem to occur when compressed video is streamed from a camera over the network and played back via OpenCV. Also, the frequency of decode errors seems proportional to the video bit rate. Also, the errors occur even when only 3% of a quad-core 4 GHz CPU is being used for decoding/display, so it doesn't seem to be a CPU bandwidth issue. My guess is that the problem is in the interface between FFMPEG and OpenCV.

Questions: 1. My test code is below - any obvious problems? 2. If you have an HD IP camera working reliably with OpenCV, did you use FFMPEG and the VideoCapture interface or something else?

Thank you.

#include <opencv2/videoio/videoio.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

Mat frame;
string window_name = "Video Test";

int main(int argc, char *argv[])
    int counter = 0;
    VideoCapture capture("rtsp://");
    if (!capture.isOpened())
        cout << "Couldn't open the video file." << endl;
        return 1;
    namedWindow(window_name, CV_WINDOW_KEEPRATIO);
    for (;;)
        capture >> frame;
        if (frame.empty())
//        if ((counter % 20) == 0)
            imshow(window_name, frame);
        if (waitKey(1) == 27)
    return 0;