Ask Your Question

Ip / network camera access.

asked 2012-11-01 05:24:00 -0600

this post is marked as community wiki

This post is a wiki. Anyone with karma >50 is welcome to improve it.

How to access any type of IP camera using OpenCv? What is the configuration I have to do to run a camera hosted on network IP. What will be the code for that?

edit retag flag offensive close merge delete

4 answers

Sort by ยป oldest newest most voted

answered 2014-07-06 18:31:00 -0600

pimartos gravatar image


I have one of the "malformed http header" ip cams (vilar IPC-1002), this code (in python) worked at first try, so one can switch it to other languages easily:

The trick is to search for the start and stop of the jpeg image bypassing the http stuff.

Hope this help others. Pedro

edit flag offensive delete link more

answered 2014-02-26 09:04:11 -0600

Ashish Ranjan gravatar image

You can use the following opencv c++ code for windows to get videostream from your IP WEBCAM app on android phone.

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char* argv[])
    VideoCapture cap; //"");
    if (!cap.isOpened())  // if not success, exit program
        cout << "Cannot open the video cam" << endl;
        return -1;

   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 : " << dWidth << " x " << dHeight << endl;

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

    while (1)
        Mat frame;
        Mat contours;

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

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

        flip(frame, frame, 1);
        imshow("MyVideo", frame); //show the frame in "MyVideo" window

        Canny(frame, contours, 500, 1000, 5, true);
        imshow("MyNegativeVideo", contours);

        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;

Here, replace the ip address ( in"");

with your ip address !! But make sure you have, ?x.mjpeg ,in the end (where x can be any string).

If this doesn't work then you should find out what your full ip address is? i.e. find out what is the string after the ip (In my case it was "/video" as shown in above address)

edit flag offensive delete link more


Thanks, this work for me, I'm streaming video using my router an usb-webcam and mpeg-streamer over openwrt :)

felocol gravatar imagefelocol ( 2014-03-31 19:23:21 -0600 )edit

I also want to develop an app which you can get video and audio stream from IP Camera on Android. So can use this piece of code for develop my app ? I mean does it work for it if I use OpenCV lib on Android. I'm newbie on OpenCV. Any idea about how to do it ? Thanks.

simdilikbuolsun gravatar imagesimdilikbuolsun ( 2014-07-17 02:51:01 -0600 )edit

answered 2013-03-07 13:17:06 -0600

Rui Marques gravatar image

updated 2013-04-13 11:25:03 -0600

I have found a work-around which is kind of hacky but it will probably work where other options have failed. It worked for me ;)

If you are able to open the stream with vlc (or other player/library) but fail to do it with opencv you can do this.

1 - Get vlc to open the http/rtsp stream from the ip camera.

2 - Set vlc to stream the source video to a file (Instructions here -

You can probably do this step programmatically with a call to vlc executable with some parameters (Hint: you can run vlc without GUI by running cvlc).

3 - Use your preferred method of opening a file with opencv. I tested it this way (its standard video opening code):

int main(int, char**) {

    cv::VideoCapture vcap;
    cv::Mat image;
    const std::string video = "/path/video";

    if(! {
        std::cout << "Error opening video stream or file" << std::endl;
        return -1;

    for(;;) {
        if(! {
            std::cout << "No frame" << std::endl;
        cv::imshow("Output Window", image);
        if(cv::waitKey(1) >= 0) break;

Plus: this method could be improved if instead of redirecting the stream to a file, you could redirect to a device (/dev/something), and maybe opencv would be able to open it.

You can also read this stackoverflow post for more options:

edit flag offensive delete link more

answered 2012-11-02 17:53:37 -0600

this post is marked as community wiki

This post is a wiki. Anyone with karma >50 is welcome to improve it.

At first you must know the URL for snap (MJPEG) e.g. cvCaptureFromFile(http://ip_addr/snap.jpg); in C. The tail "/snap.jpg" is in Bosch Dinion Ip cameras. In my case it method only works (Linux, ffmpeg + h264 codecs installed), some cameras have problem with encoders and cooperative with installed codecs in system e.g. Bosch support as i saw only windows.

In one of my programs work this function, it's mixed C & C++ operators, in other way it won't work...

Mat GetImage(const char *CameraIP){

IplImage* IplImage;
CvCapture* capture;

capture = cvCaptureFromFile(CameraIP);

IplImage = cvQueryFrame( capture );
Mat OutImage(IplImage);
return( OutImage );


For live stream: 1)C: cvCaptureFromCAM(address); // open stream cvQueryFrame(CvCapture* capture); // get frame

2)C++; // open stream; // get frame

If you work on linux, and have tried a lot in other ways to resolve the problem, feel free to write about it.

I have read a lot of infos that for RTSP streaming from IP cameras are good framework POCO ([]).

Good luck

edit flag offensive delete link more


Awesome tip that one about the "/snap.jpg" tail. I have a Bosch and did not know about that, it will be very usefull. Now to see if the rest works...

Rui Marques gravatar imageRui Marques ( 2013-03-05 12:29:31 -0600 )edit

How would I find out the URL for my camera, it's a Mega-Pixel security camera this model. Onvif protocol. It allows mjpeg and h.264. No idea how to get the url for direct streaming

SergioBasurco gravatar imageSergioBasurco ( 2013-07-11 12:08:54 -0600 )edit

@SergioBasurco I have a Mega-pixel camera too, model name GTN-IPB812VW, the URL is rtsp://IPADDRESS:554/11 (or 12 for the sub stream).

lapis gravatar imagelapis ( 2014-03-26 08:30:17 -0600 )edit

i have the url of streaming camera, but can you write the whole code in example, i'm still ambiguity about your code

dott gravatar imagedott ( 2016-04-16 01:38:02 -0600 )edit

Question Tools



Asked: 2012-11-01 05:24:00 -0600

Seen: 63,882 times

Last updated: Jul 06 '14