IP摄像头使用OpenCV访问

IP Camera access using OpenCV

本文关键字:OpenCV 访问 摄像头 IP      更新时间:2023-10-16

下面给出的代码用于使用OPENCV访问轴IP摄像机。运行程序时,它首先显示"打开CAP_FFMPEG_IMPL中的错误",然后显示找不到相机。

#include <opencvcv.h>
#include <opencv2highguihighgui.hpp>
#include <opencv2imgprocimgproc.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;
}

我要去哪里?

我在尝试使用公共IP摄像机显示IP摄像机时遇到了类似的问题。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;
    }   
}

通过以太网电缆连接到我的计算机的轴M1004-WENK:

  1. 在您选择的浏览器(我正在使用Chrome)中,导航到相机的IP地址。提供必要的凭据。
  2. 您应该看相机的直播流。右键单击视频流,然后选择"检查元素"(或在非铬浏览器中等效的元素)。
  3. 您应该看到一个称为SRC的变量 - 这是您可以在OpenCV中使用的变量直接访问相机。我的是 /mjpg/video.mjpg,我敢打赌你会很相似。

您给OpenCV的地址应该看起来像这样:

http://<USERNAME>:<PASSWORD>@<IP_ADDRESS>/<the value of src>

这就是我的样子:

http://uname:login@192.168.0.0/mjpg/video.mjpg

我将地址输入您的代码,并可以从OpenCV窗口中查看视频流。

我在iPhone上安装了"迷你网络摄像头"应用程序,并将其用作IP摄像机,并将其用作" http://192.168.1.103"。此外,我使用了此代码:

VideoCapture capture;
Mat image;
if (!capture.open("http://192.168.1.103/video.cgi?.mjpg")) {
    cout << "Error opening video stream or file" << endl;
    return -1;
}
....

它有效。(http://192.168.1.103/video.cgi?.mjpg)