C++ 带有网络摄像机的 OpenCV
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/712998/
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
OpenCV with Network Cameras
提问by Grifo
I'm using openCV 1.1pre1 under Windows. I have a network camera and I need to grab frames from openCV. That camera can stream a standard mpeg4 stream over RTSP or mjpeg over http. I've seen many threads talking about using ffmpeg with openCV but I cannot make it work.
我在 Windows 下使用 openCV 1.1pre1。我有一个网络摄像头,我需要从 openCV 抓取帧。该相机可以通过 RTSP 或通过 http 传输 mjpeg 的标准 mpeg4 流。我已经看到很多线程在谈论将 ffmpeg 与 openCV 一起使用,但我无法使其工作。
How I can grab frames from an IP camera with openCV?
如何使用 openCV 从 IP 摄像机中抓取帧?
Thanks
谢谢
Andrea
安德烈亚
采纳答案by john ktejik
rtsp protocol did not work for me. mjpeg worked first try. I assume it is built into my camera (Dlink DCS 900).
rtsp 协议对我不起作用。mjpeg 工作第一次尝试。我假设它内置于我的相机(Dlink DCS 900)中。
Syntax found here: http://answers.opencv.org/question/133/how-do-i-access-an-ip-camera/
语法在这里找到:http: //answers.opencv.org/question/133/how-do-i-access-an-ip-camera/
I did not need to compile OpenCV with ffmpg support.
我不需要用 ffmpg 支持编译 OpenCV。
回答by Alexey
I enclosed C++ code for grabbing frames. It requires OpenCV version 2.0 or higher. The code uses cv::mat structure which is preferred to old IplImage structure.
我附上了用于抓取帧的 C++ 代码。它需要 OpenCV 2.0 或更高版本。该代码使用 cv::mat 结构,该结构优于旧的 IplImage 结构。
#include "cv.h"
#include "highgui.h"
#include <iostream>
int main(int, char**) {
cv::VideoCapture vcap;
cv::Mat image;
const std::string videoStreamAddress = "rtsp://cam_address:554/live.sdp";
/* it may be an address of an mjpeg stream,
e.g. "http://user:pass@cam_address:8081/cgi/mjpg/mjpg.cgi?.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;
}
//Create output window for displaying frames.
//It's important to create this window outside of the `for` loop
//Otherwise this window will be created automatically each time you call
//`imshow(...)`, which is very inefficient.
cv::namedWindow("Output Window");
for(;;) {
if(!vcap.read(image)) {
std::cout << "No frame" << std::endl;
cv::waitKey();
}
cv::imshow("Output Window", image);
if(cv::waitKey(1) >= 0) break;
}
}
UpdateYou can grab frames from H.264 RTSP streams. Look up your camera API for details to get the URL command. For example, for an Axis network camera the URL address might be:
更新您可以从 H.264 RTSP 流中抓取帧。查找您的相机 API 以获取详细信息以获取 URL 命令。例如,对于 Axis 网络摄像机,URL 地址可能是:
// H.264 stream RTSP address, where 10.10.10.10 is an IP address
// and 554 is the port number
rtsp://10.10.10.10:554/axis-media/media.amp
// if the camera is password protected
rtsp://username:[email protected]:554/axis-media/media.amp
回答by lolong
#include <stdio.h>
#include "opencv.hpp"
int main(){
CvCapture *camera=cvCaptureFromFile("http://username:pass@cam_address/axis-cgi/mjpg/video.cgi?resolution=640x480&req_fps=30&.mjpg");
if (camera==NULL)
printf("camera is null\n");
else
printf("camera is not null");
cvNamedWindow("img");
while (cvWaitKey(10)!=atoi("q")){
double t1=(double)cvGetTickCount();
IplImage *img=cvQueryFrame(camera);
double t2=(double)cvGetTickCount();
printf("time: %gms fps: %.2g\n",(t2-t1)/(cvGetTickFrequency()*1000.), 1000./((t2-t1)/(cvGetTickFrequency()*1000.)));
cvShowImage("img",img);
}
cvReleaseCapture(&camera);
}
回答by f3lix
OpenCV can be compiled with FFMPEG support. From ./configure --help:
OpenCV 可以使用 FFMPEG 支持进行编译。从./configure --help:
--with-ffmpeg use ffmpeg libraries (see LICENSE) [automatic]
You can then use cvCreateFileCapture_FFMPEGto create a CvCapture with e.g. the URL of the camera's MJPG stream.
然后您可以使用cvCreateFileCapture_FFMPEG来创建一个 CvCapture,例如相机的 MJPG 流的 URL。
I use this to grab frames from an AXIS camera:
我用它从 AXIS 相机抓取帧:
CvCapture *capture =
cvCreateFileCapture_FFMPEG("http://axis-cam/mjpg/video.mjpg?resolution=640x480&req_fps=10&.mjpg");
回答by sipi
I just do it like this:
我只是这样做:
CvCapture *capture = cvCreateFileCapture("rtsp://camera-address");
Also make sure this dll is available at runtime else cvCreateFileCapture will return NULL
还要确保此 dll 在运行时可用,否则 cvCreateFileCapture 将返回 NULL
opencv_ffmpeg200d.dll
The camera needs to allow unauthenticated access too, usually set via its web interface. MJPEG format worked via rtsp but MPEG4 didn't.
相机也需要允许未经身份验证的访问,通常通过其 Web 界面进行设置。MJPEG 格式通过 rtsp 工作,但 MPEG4 没有。
hth
第
Si
硅
回答by Indy9000
Use ffmpeglib to connect to the stream.
使用 ffmpeglib 连接到流。
These functions may be useful. But take a look in the docs
这些功能可能很有用。但是看看文档
av_open_input_stream(...);
av_find_stream_info(...);
avcodec_find_decoder(...);
avcodec_open(...);
avcodec_alloc_frame(...);
You would need a little algo to get a complete frame, which is available here
您需要一点算法才能获得完整的框架,可在此处获得
http://www.dranger.com/ffmpeg/tutorial01.html
Once you get a frame you could copy the video data (for each plane if needed) into a IplImage which is an OpenCV image object.
获得一帧后,您可以将视频数据(如果需要,为每个平面)复制到 IplImage 中,该 IplImage 是一个 OpenCV 图像对象。
You can create an IplImage using something like...
您可以使用类似...
IplImage *p_gray_image = cvCreateImage(size, IPL_DEPTH_8U, 1);
Once you have an IplImage, you could perform all sorts of image operations available in the OpenCV lib
一旦你有了一个 IplImage,你就可以执行 OpenCV 库中可用的各种图像操作