C++ 使用 OpenCV 访问网络摄像机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21324785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IP Camera access using OpenCV
提问by Prakhar Mohan Srivastava
The code given below is for accessing an Axis IP camera using OpenCV. On running the program it first displays "Error in opening cap_ffmpeg_impl..." and then it displays Camera not found.
下面给出的代码用于使用 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;
}
Where am I going wrong?
我哪里错了?
采纳答案by Mayur
I faced similar problem when trying to display IP camera using the public IP camera. Opencv needs some typical kind of URL to open the camera.Try the URL from below code. Heres the code that worked for me.
我在尝试使用公共 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;
}
}
Copy this code as it is and try.
按原样复制此代码并尝试。
#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;
}
}
回答by Connor
The following works for an Axis M1004-W connected to my computer via ethernet cable:
以下适用于通过以太网电缆连接到我的计算机的 Axis M1004-W:
- In the browser of your choice (I'm using Chrome), navigate to the camera's IP address. Provide credentials as necessary.
- You should be looking at a live stream from your camera. Right-click on the video stream and select "Inspect Element" (or its equivalent in non-Chrome browsers).
- You should see a variable called src - this is what you can use within OpenCV to access the camera directly. Mine is
/mjpg/video.mjpg
, and I bet yours will be similar.
- 在您选择的浏览器(我使用的是 Chrome)中,导航到摄像机的 IP 地址。根据需要提供凭据。
- 您应该查看来自相机的实时流。右键单击视频流并选择“检查元素”(或非 Chrome 浏览器中的等效项)。
- 您应该会看到一个名为 src 的变量 - 这是您可以在 OpenCV 中使用以直接访问相机的变量。我的是
/mjpg/video.mjpg
,我敢打赌你的会相似。
The address you give to OpenCV should look like this:
您提供给 OpenCV 的地址应如下所示:
http://<USERNAME>:<PASSWORD>@<IP_ADDRESS>/<the value of src>
This is what mine looks like:
这是我的样子:
http://uname:[email protected]/mjpg/video.mjpg
I entered my address into your code and can see the video stream from an OpenCV window.
我在您的代码中输入了我的地址,并且可以从 OpenCV 窗口看到视频流。
回答by Mohammad Rahimi
I installed "Mini WebCam" app on my iphone and used it as an ip camera with "http://192.168.1.103" as it's address. In addition I used this piece of code:
我在我的 iphone 上安装了“迷你网络摄像头”应用程序,并将其用作网络摄像头,地址为“ 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;
}
....
it works.(http://192.168.1.103/video.cgi?.mjpg)