OpenCV 之 網絡攝像頭


 1  RTSP

  RTSP (Real Time Streaming Protocol),是一種語法和操作類似 HTTP 協議,專門用於音頻和視頻的應用層協議。 和 HTTP 類似,RTSP 也使用 URL 地址。

  海康網絡攝像頭的 RTSP URL 格式如下:

rtsp://[username]:[password]@[ip]:[port]/[codec]/[channel]/[subtype]/av_stream
1) username 用戶名,常用 admin 2) password 密碼,常用 12345 3) ip 攝像頭IP,如 192.0.0.64
4) port 端口號,默認為 554
5) codec 視頻編碼模式,有 h264、MPEG-4、mpeg4 等
6) channel 通道號,起始為1,例如通道1,則為 ch1
7) subtype 碼流類型,主碼流為 main,輔碼流為 sub

  大華網絡攝像頭的 RTSP URL 格式如下:

rtsp://[username]:[password]@[ip]:[port]/cam/realmonitor?[channel=1]&[subtype=1] 
1) username、password、ip、port 同上
2) channel 通道號,起始為1,例如通道2,則為 channel=2 3) subtype 碼流類型,主碼流為0(即 subtype=0),輔碼流為1(即 subtype=1

 

2  VideoCapture 類

  VideoCapture 類是 OpenCV 中用來操作視頻流的類,可以在構造函數中打開視頻,其參數支持以下三種類型:

  1)  name of video file (eg. `video.avi`)

  2)  image sequence (eg. `img_%02d.jpg`, which will read samples like `img_00.jpg, img_01.jpg, img_02.jpg, ...`)

  3)  URL of video stream (eg. `protocol://host:port/script_name?script_params|auth`).

// Open video file or a capturing device or a IP video stream for video capturing
//
VideoCapture 構造函數 CV_WRAP VideoCapture(const String& filename);

  也可以構造后,再使用 open 函數來打開

// 參數同 VideoCapture 的構造函數
CV_WRAP virtual bool open(const String& filename);

 

3  代碼

  下面是以海康威視的某款網絡攝像頭為例,使用 OpenCV 的 VideoCapture 類來顯示實時視頻

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

using namespace cv;

int main(int argc, char** argv)
{
    String rtsp_addr = "rtsp://admin:a1234567@192.168.5.186:554/MPEG-4/ch1/main/av_stream";

    VideoCapture cap(rtsp_addr);
//    cap.open(rtsp_addr);

    Mat frame;

    for(;;) {
        cap >> frame;
        if(frame.empty())
            break;

        imshow("Video Stream", frame);

        if (waitKey(10) == 'q')
            break;
    }
}

  附上一張園區的部分視頻截圖如下:

 

 

參考資料:

  Multimedia Over IP: RSVP, RTP, RTCP, RTSP

  海康、大華IpCamera RTSP地址和格式    xiejiashu

 <Learning OpenCV3>  chapter 8

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM