c++ - 使用 OpenCV 访问网络摄像机

标签 c++ opencv video-streaming ip-camera

下面给出的代码用于使用 OpenCV 访问 Axis IP 摄像机。在运行程序时,它首先显示“打开 cap_ffmpeg_impl 时出错...”,然后显示找不到相机。

#include <opencv\cv.h>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

int main()
{
    Mat frame;
    namedWindow("video", 1);
    VideoCapture cap("http://IPADDRESS/video.mjpg");
    if(!cap.isOpened())
    {
        cout<<"Camera not found"<<endl;
        getchar();
        return -1;
    }
    while ( cap.isOpened() )
    {
        cap >> frame;
        if(frame.empty()) break;

        imshow("video", frame);
        if(waitKey(30) >= 0) break;
    }   
    return 0;
}

我哪里错了?

最佳答案

我在尝试使用公共(public)网络摄像机显示网络摄像机时遇到了类似的问题。 Opencv 需要一些典型的 URL 来打开相机。试试下面代码中的 URL。 这是对我有用的代码。

int main(int, char**) {
    cv::VideoCapture vcap;
    cv::Mat image;

    // This works on a D-Link CDS-932L
    const std::string videoStreamAddress = "http://ID:PASSWORD@IPADDRESS:PORTNO/mjpeg.cgi?user=ID&password=ID:PASSWORD&channel=0&.mjpg";
       //open the video stream and make sure it's opened
    if(!vcap.open(videoStreamAddress)) {
        std::cout << "Error opening video stream or file" << std::endl;
        return -1;
    }

    for(;;) {
        if(!vcap.read(image)) {
            std::cout << "No frame" << std::endl;
            cv::waitKey();
        }
        cv::imshow("Output Window", image);

        if(cv::waitKey(1) >= 0) break;
    }   

}

按原样复制此代码并尝试。

#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <iostream>
int main(int, char**) {
    cv::VideoCapture vcap;
    cv::Mat image;

    // This works on a D-Link CDS-932L

    const std::string videoStreamAddress = "http://USER:PWD@IPADDRESS:8088/mjpeg.cgi?user=USERNAME&password=PWD&channel=0&.mjpg";
       //open the video stream and make sure it's opened
    if(!vcap.open(videoStreamAddress)) {
        std::cout << "Error opening video stream or file" << std::endl;
        return -1;
    }

    for(;;) {
        if(!vcap.read(image)) {
            std::cout << "No frame" << std::endl;
            cv::waitKey();
        }
        cv::imshow("Output Window", image);

        if(cv::waitKey(1) >= 0) break;
    }   

}

关于c++ - 使用 OpenCV 访问网络摄像机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21324785/

相关文章:

c++ - opencv以Point为键创建 map

Python OpenCV 在网络摄像头捕获时出现黑屏

c++ - 如何使用 FFMPEG 从存储为视频 AVPackets 的 NAL 单元播放 H.264 流

c++ - C++中的指针赋值。 (指向指针的指针位于 LHS 上)

c++ - C++ 中的#import 和#include 有什么区别?

java - 使用openCV从图像中查找圆形区域

http - Nginx 不接受字节范围

video-streaming - Dash 流媒体服务器原型(prototype)

c++ - 从 Python 调用 C/C++?

c++ - 使用 C++ TransmitFile 和重叠 I/O 的 TCP winsock 文件上传