简体   繁体   English

通过OpenCV和C ++从IP摄像机流式传输视频

[英]Streaming video from ip camera by opencv and c++

I am trying to access web cam stream from c++ using openCV code but it failed and show error that cannot open stream. 我正在尝试使用OpenCV代码从C ++访问网络摄像头流,但是它失败并显示无法打开流的错误。 The code mentioned below accesses web cam when replace URL with 0. Same camera accessible from VLC and python code. 将URL替换为0时,下面提到的代码将访问网络摄像头。可通过VLC和python代码访问同一摄像机。

#include <stdio.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>

int main(int, char**) {

    VideoCapture cap("rtsp://admin:admin@10.11.18.185:554/cam/realmonitor?channel=1&subtype=0"); // open the video camera using http protocol with the URL specified 
    while (!cap.isOpened())  // if not success, exit program
    {
        cout << "cap not open" << endl;
        continue;
        //return -1;
    }

    Mat frame;
    namedWindow("MyVideo", CV_WINDOW_AUTOSIZE); 
    while (1) {
        cap.read(frame);

        imshow("MyVideo", frame);
        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;
            exit(0);
        }
    }
}

VideoCapture library expects a particular set for formats to receive the video stream in. VideoCapture库要求使用特定的格式集来接收视频流。

When reading from a saved file, we specify the same in the file extension- .mp4 , .avi etc. If this extension is not specified, the VideoCapture will not be able to capture frames. 从保存的文件中读取文件时,我们在文件扩展名.mp4.avi等中指定相同的名称。如果未指定此扩展名,VideoCapture将无法捕获帧。

Try using: 尝试使用:

VideoCapture cap("rtsp://admin:admin@10.11.18.185:554/cam/realmonitor?channel=1&subtype=0/video?x.mjpeg");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM