我使用的是Windows下的OpenCV 1.1pre1。 我有一个网络摄像头,我需要从OpenCV的抢帧。该摄像机可以在流RTSP通过HTTP标准的MPEG4码流或MJPEG。 我见过很多线程谈论使用与ffmpeg的OpenCV的,但我不能让它工作。

如何抓住从IP相机帧与OpenCV的?

由于

安德烈

有帮助吗?

解决方案

RTSP协议并没有为我工作。 MJPEG工作的第一次尝试。我假设它被内置到我的相机(Dlink的DCS 900)。

语法发现这里: http://answers.opencv.org/问题/ 133 /如何-DO-存取一个-IP-相机/

我并不需要编译的OpenCV与ffmpg支持。

其他提示

我括C ++代码用于抓取的帧。它需要的OpenCV 2.0或更高版本。该代码使用这是优选的,以旧的IplImage结构CV ::垫结构。

#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;
    }   
}

<强>更新您可以抓住帧从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: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支持进行编译。从 ./配置--help

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

可以然后使用 cvCreateFileCapture_FFMPEG 以创建具有例如CvCapture相机的MJPG流的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

相机需要允许未经认证访问过,通常是通过其网络接口进行设置。 MJPEG格式通过RTSP工作,但MPEG4没有。

HTH

的Si

使用ffmpeglib连接到流中。

这些功能可能是有用的。但采取在docs看看

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

您将需要一点点算法中得到一个完整的框架, 这是可在这里

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

一旦获得一帧(如果需要为每个平面)你可以复制的视频数据转换成一 的IplImage其是OpenCV的图像对象。

您可以使用的东西创造的IplImage像...

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

一旦你有一个IplImage结构,你可以执行OpenCV的lib中可用的各种图像操作

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top