Live stream to OpenCV via IP camera?

asked 2015-07-16 03:31:20 -0600

Peter645 gravatar image

Okay so ultimately I am trying to write an accurate multiple person counting program using OpenCV and Visual Studio 2010. I am quite an OpenCV and C++ newbie so please be quite specific in the responses if possible.

I have recently acquired a Dahua IP camera which I have successfully set up, by set up I mean that I can access the live stream over the internet, and can also access it with OpenCV's video capture class, PROVIDED THAT I USE HTTP PROTOCOL with this URL:

"http://admin:[email protected]:80/cgi-bin/snapshot.cgi?chn=0&u=admin&p=admin&.mjpg"

My problem is this: I can access the stream from my Source.cpp. The code is below:

#include <iostream>
#include <opencv\highgui.h>
#include "opencv2/imgproc/imgproc.hpp" 
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
using namespace cv;
using namespace std;

int main(int argc, char* argv[])
    while (1)
    VideoCapture cap("http://admin:[email protected]:80/cgi-bin/snapshot.cgi?chn=0&u=admin&p=admin&.mjpg"); // open the video camera using http protocol with the URL specified 
    // //Just the camera's IP address, I have changed my computers IP to coincide with this.

    if (!cap.isOpened())  // if not success, exit program
        cout << "Cannot open the video cam" << endl;
        return -1;
   cap.set(CV_CAP_PROP_FPS,25); // Feed is already 25fps, I tried setting it to a lower value but nothing      changes.
   double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
   double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video

   cout << "Frame size_1 : " << dWidth << " x " << dHeight << endl; //displays dimensions in console window, displays: "Frame size_1 : 1280 x 720" continuously

   cap.set(CV_CAP_PROP_FRAME_WIDTH, 640); //My attempts to change the frame size, pretty futile
   cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);

   dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); 
   dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); ////get the width of frames of the video, change dwidth and dheight to these dimension, I expected them to be set to 640 and 480 respectively but it doesn't seem that way. 
   cout << "Frame size_2 : " << dWidth << " x " << dHeight << endl; //Still spitting out: "Frame size_2: 1280 x 720" continuously.

    namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"

        Mat frame;

        bool bSuccess =; // read a new frame from video

         if (!bSuccess) //if not success, break loop
             cout << "Cannot read a frame from video stream" << endl;

        imshow("MyVideo", frame); //shows the frame in "MyVideo" window

        if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
            cout << "esc key is pressed by user" << endl;

    return 0;

So, with the above code I am able to open the stream from my IP camera. My first problem is that I cannot manipulate the feed (set the frame dimensions etc).

My BIGGER problem is that I cannot open the stream in my actual counting program in order to do image processing on it ...

edit retag flag offensive close merge delete


no, you can't change the resolution or fps of an ip-stream. but you can just resize the image

berak gravatar imageberak ( 2015-07-16 03:41:25 -0600 )edit

i guess, there's a way to query a different resolution with the url / cgi params. look at your camera's manual again. also please try to find out, if it offers a continuous streaming mode, not a snapshot(single image) one.

then, please do not open a new capture for each image

berak gravatar imageberak ( 2015-07-16 03:43:14 -0600 )edit