OpenCV与网络摄像头

Gri*_*ifo 23 c++ windows networking opencv ffmpeg

我在Windows下使用openCV 1.1pre1.我有一个网络摄像头,我需要从openCV抓取帧.该摄像机可以通过RTSP或mjpeg通过http传输标准mpeg4流.我已经看到很多线程都在谈论将ffmpeg与openCV一起使用,但我无法使其工作.

如何使用openCV从IP摄像头抓取帧?

谢谢

安德里亚

Ale*_*xey 22

我附上了用于抓取帧的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;
    }   
}
Run Code Online (Sandbox Code Playgroud)

更新您可以从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
Run Code Online (Sandbox Code Playgroud)


小智 10

#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);
}
Run Code Online (Sandbox Code Playgroud)


f3l*_*lix 5

OpenCV可以使用FFMPEG支持进行编译.从./configure --help:

--with-ffmpeg     use ffmpeg libraries (see LICENSE) [automatic]
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用cvCreateFileCapture_FFMPEG创建一个CvCapture,例如摄像机的MJPG流的URL.

我用它来从AXIS相机抓取帧:

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

  • 是否可以在Windows下使用ffmpeg支持配置openCV? (3认同)

joh*_*jik 3

rtsp 协议对我不起作用。mjpeg 第一次尝试成功了。我假设它内置于我的相机 (Dlink DCS 900) 中。

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

我不需要编译带有 ffmpg 支持的 OpenCV。