OpenCV与networking摄像机

我在Windows下使用openCV 1.1pre1。 我有一台networking摄像机,我需要从openCV抓取帧。 该摄像机可以通过HTTP在RTSP或mjpeg上传输标准mpeg4stream。 我看过很多关于在openCV中使用ffmpeg的主题,但是我不能使它工作。

我怎样才能从openCV的IP摄像头抓帧?

谢谢

安德里亚

rtsp协议没有为我工作。 mjpeg第一次尝试。 我认为它是内置在我的相机(Dlink DCS 900)。

在这里find的语法: http : //answers.opencv.org/question/133/how-do-i-access-an-ip-camera/

我不需要用ffmpg支持来编译OpenCV。

我附上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, eg "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; } } 

更新您可以从H.264 RTSPstream中获取帧。 查看您的相机API获取详细信息以获取URL命令。 例如,对于Axisnetworking摄像机,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:password@10.10.10.10:554/axis-media/media.amp 
 #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); } 

OpenCV可以用FFMPEG支持编译。 从./configure –help

 --with-ffmpeg use ffmpeg libraries (see LICENSE) [automatic] 

然后,您可以使用cvCreateFileCapture_FFMPEG创build一个CvCapture,例如相机的MJPGstream的URL。

我使用这个来从AXIS相机抓取帧:

 CvCapture *capture = cvCreateFileCapture_FFMPEG("http://axis-cam/mjpg/video.mjpg?resolution=640x480&req_fps=10&.mjpg"); 

我只是这样做:

 CvCapture *capture = cvCreateFileCapture("rtsp://camera-address"); 

还要确保这个DLL在运行时可用,否则cvCreateFileCapture将返回NULL

 opencv_ffmpeg200d.dll 

相机也需要允许未经身份validation的访问,通常通过其Web界面进行设置。 MJPEG格式通过rtsp工作,但MPEG4没有。

心连心

使用ffmpeglib连接到stream。

这些function可能是有用的。 但看看文档

 av_open_input_stream(...); av_find_stream_info(...); avcodec_find_decoder(...); avcodec_open(...); avcodec_alloc_frame(...); 

你需要一个algorithm来获得一个完整的框架,这是可用的

 http://www.dranger.com/ffmpeg/tutorial01.html 

一旦你得到一个框架,你可以复制video数据(如果需要,每个平面)到一个OpenCV图像对象IplImage。

你可以创build一个IplImage使用像…

 IplImage *p_gray_image = cvCreateImage(size, IPL_DEPTH_8U, 1); 

一旦你有一个IplImage,你可以在OpenCV库中执行各种图像操作