[OpenCV] Connect to IP Camera by RTSP steaming | 運用RTSP串流連接網絡攝像機(IP Camera)

USB WebCam is supported by openCV library. OpenCV can connected to webcam by call function cvCaptureFromCAM(). Beacuse OpenCV do not have internet steaming function, if we want to make a connection between IP Camera and OpenCV, we needed to do a bit more.

 The IP Camera model that I bought is IPUX 1330. It have video streaming output thought RTSP.


OpenCV可以運用cvCaptureFromCAM()去連接USB網絡攝影機(WebCam)。但OpenCV不支援網絡串流,我們再做多一點以連接網絡攝像機(IP Camera)。

這裡用的網絡攝像機型號是IPUX 1330, 它可以用RTSP串流視訊。

There is a C++ library called The mobile robot programming toolkit which extend the area of openCV to the internet. I will not spend time on how to install this library(It has a detail manual.) and other powerful function in this library.

To connect IP camera by rstp, first we needed to enable RTSP function and find out the rtsp link. The link of my IPUX 1330 is rtsp://192.168.0.30/mpeg4. (I suggested you to fix the IP of the camera). We can proof the connection of rtsp streaming is success by VLC media player.

If the video is ready, we can used MRPT to connect IP Camera and return the image to opencv.


MRPT這C++庫可以將串流視訊連接至OpenCV。詳細的安裝方法在這 ,但沒有中文。

首先啟用並設定網絡攝影機的RTSP功能,然後找出RTSP網址。我的IPUX 1330網址是rtsp://192.168.0.30/mpeg4(最好把IP固定,不然每次運行程式都要再次輸入地址)。如果不確定RTSP設定成功,可以使用VLC 測試。

待以上設定完成,我們可以用MRPT連接IP Camera然後返回IplImage至OpenCV。

char getVideoFromRTSP() {
    mrpt::utils::CImage rtspframe;
    CFFMPEG_InputStream rtspcapture;
    IplImage *frame = 0, *cpimg;

    CTicTac    tictac;
    tictac.Tic();
    unsigned int nFrames = 0;

    if ( !rtspcapture.openURL("rtsp://192.168.0.30/mpeg4",false , true) ) {
        printf("Can't detect the IP Camera\n");
        getchar();
        return '0';
    }

    while ( rtspcapture.retrieveFrame(rtspframe) ) {
        frame = (IplImage*) rtspframe.getAsIplImage();
        if (!frame) break;

        double fps = ++nFrames / tictac.Tac();
        rtspframe.textOut(5,5,format("%.02f fps",fps),0x808080);

        cpimg = cvCreateImage( cvGetSize(frame),
            frame->depth,
            frame->nChannels );
        cvCopy(frame, cpimg, NULL);
        colorImg = cpimg;

        mrpt::system::sleep(1);
        cvWaitKey(1);
        detectFaceFromImage( cha_window,cpimg );
        cvReleaseImage( &cpimg );
    }
}






This is a demo function to make connection and create a IplImage.

We can use class CFFMPEG_InputStream to make connection between RTSP streaming data and IplImage pointer. To do this, first we can call rtspcapture.openURL(). It opens a video file or video stream(rtsp://), then use rtspcapture.retrieveFrame() to get the video frame by frame and save into mrpt::utils::CImage. Class mrpt::utils::CImage have a function to return to image as IplImage by using getAsIplImage()

以上就完成連接RTSP串流至OpenCV IplImage。

首先建立 class CFFMPEG_InputStream 以連接RTSP串流視訊和IplImage指標,然後以rtspcapture.openURL()開啟RSTP串流網址,之後用rtspcapture.retrieveFrame()去取得每幀到mrpt::utils::CImage,最後運用getAsIplImage()連接IplImage.

 //A generic class which process a video file or other kind of input stream (http, rtsp) and allows
//the extraction of images frame by frame. 
CFFMPEG_InputStream rtspcapture;

/** Returns a pointer to an OpenCV's IplImage struct containing the image, which is linked to this class.
  *
  *  NOTE: This method is DEPRECATED, consider using getAs<IplImage>() instead, which will also make code look clearer.
  * Do not manually free the returned pointer, since this object is its owner and will delete it upon destruction.
  *  \sa getAs */
MRPT_DECLARE_DEPRECATED_FUNCTION("Deprecated: Use getAs<> instead",
inline void*  getAsIplImage() const )
{
        makeSureImageIsLoaded();
        return img;
}



留言

此網誌的熱門文章

[OpenCV] RAW rgb data to IplImage | 以rgb原始資料建立IplImage指標

Monitoring System Spec.& IO Mapping

突如奇來的高科技電子零件