MATLAB - 捕获视频流(MJPEG、rtsp、mpeg)

发布于 2024-12-23 17:17:07 字数 439 浏览 2 评论 0原文

有人在 MATLAB 中使用过从 IP 摄像机捕获视频流吗?例如,在 MATLAB 中从 rtsp://10.10.10.10:554/live.sdp(rtsp 流)或 http://xxxx/axis-cgi/mjpg/video.cgi(mjpeg 流)。 MATLAB 的图像采集工具箱当前不支持此功能。我找到了2个选择: 1)使用mmread。但是 64 位 MATLAB 不支持 http 流读取或 2)编写我自己的C++函数来抓取帧(我使用OpenCV库),然后将其编译成MATLAB MEX函数。 任何建议表示赞赏。

Has anyone worked with capturing video streams from IP cameras in MATLAB? For example to grab frames in MATLAB from rtsp://10.10.10.10:554/live.sdp (rtsp stream) or from http://x.x.x.x/axis-cgi/mjpg/video.cgi (mjpeg stream). MATLAB's Image Acquisition Toolbox does not currently support this. I found 2 options:
1) using mmread. However http stream reading is not supported under 64-bit MATLAB or
2) to write my own C++ function that grabs frames (I use OpenCV library) and then compile it into MATLAB MEX function.
Any suggestions are appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无畏 2024-12-30 17:17:07

这是我从 MATLAB 支持处得到的答案:

不幸的是,您的说法是正确的,当前图像采集工具箱不支持 IP 摄像机。关于解决方法: 1. 如果 mmread 适合您,也许您可​​以在 64 位计算机上安装 32 位 MATLAB。 2. 编写自己的 MEX 驱动程序应该是一个可能的选择。 3. IMREAD 能够从IP 摄像机获取帧。可以利用此功能并构建构建视频流的函数。尽管帧速率可能是一个问题。

我建议实现您自己的 Matlab mex 函数来抓取视频帧。以下是执行此操作的一些指示:

  1. OpenCV 库用于从网络摄像机捕获视频流,请参阅使用网络摄像机的 OpenCV。每个IP摄像机可能有不同的API用于访问视频流(即URL地址)。例如,http://10.10.10.10/axis-cgi/mjpg/video.cgi?resolution=800x600&.mjpg
  2. 以下是 OpenCV 库的 matlab mex 函数集合和开发套件的链接(感谢 Kota Yamaguchi): https://github.com/kyamagu/mexopencv。该库可以轻松地在 OpenCV 数据类型和 mxArray 之间进行转换。这是一个例子:

    <前><代码>#include“mexopencv.hpp”
    无效 mexFunction( int nlhs, mxArray *plhs[],
    int nrhs, const mxArray *prhs[] )
    {
    // 检查参数
    if (nlhs!=1 || nrhs!=1)
    mexErrMsgIdAndTxt("myfunc:invalidArgs", "参数数量错误");

    // 将 MxArray 转换为 cv::Mat
    cv::Mat mat = MxArray(prhs[0]).toMat();

    // 做你想做的事

    // 将 cv::Mat 转换回 mxArray*
    plhs[0] = MxArray(mat);
    }

  3. 可以使用线程使应用程序异步,其中生产者线程从相机抓取帧并将其放入循环缓冲区中。另一方面,消费者线程从缓冲区检索帧并将其转换为 mxArray(矩阵)输出。请参阅如何实现 cv::Mat 对象 (OpenCV) 的循环缓冲区?。循环缓冲区需要线程安全,请参阅循环缓冲区的线程安全实现

This is the answer I got from MATLAB support:

Unfortunately, you are correct that currently the Image Acquisition Toolbox does not support IP cameras. Regarding workarounds: 1. If mmread works for you, perhaps it is feasible for you to install a 32-bit MATLAB on your 64-bit machine. 2. Writing your own MEX driver should be a possible option. 3. IMREAD is able to obtain frames from IP cameras. It may be possible to utilize this capability and build a function that constructs the video stream. Although frame rate may be an issue.

I suggest implementing your own Matlab mex function to grab video frames. Here are some pointers to do so:

  1. OpenCV library is used to capture video streams from network cameras, see OpenCV with Network Cameras. Each IP camera may have a different API for accessing video streams (i.e. URL address). For example, http://10.10.10.10/axis-cgi/mjpg/video.cgi?resolution=800x600&.mjpg.
  2. Below is a link to collection and development kit of matlab mex functions for OpenCV library (thanks to Kota Yamaguchi): https://github.com/kyamagu/mexopencv. This library makes it easy to convert between OpenCV data types and mxArray. Here's an example:

    #include "mexopencv.hpp"
    void mexFunction( int nlhs, mxArray *plhs[],
                      int nrhs, const mxArray *prhs[] )
        {
        // Check arguments
        if (nlhs!=1 || nrhs!=1)
            mexErrMsgIdAndTxt("myfunc:invalidArgs", "Wrong number of arguments");
    
        // Convert MxArray to cv::Mat
        cv::Mat mat = MxArray(prhs[0]).toMat();
    
        // Do whatever you want
    
        // Convert cv::Mat back to mxArray*
        plhs[0] = MxArray(mat);
    }
    
  3. The application can be made asynchronous by using threads, where producer thread grabs frames from the camera and puts it into a circular buffer. Consumer thread, on the other hand, retrieves frames from the buffer and converts them into mxArray (matrix) output. See How to implement a circular buffer of cv::Mat objects (OpenCV)?. Circular buffer will need to be made thread safe, see Thread safe implementation of circular buffer.

烙印 2024-12-30 17:17:07

自 MATLAB R2015a 起,使用函数 ipcam 变得非常简单:

cam = ipcam('http://172.28.17.193/video.mjpeg', 'admin', 'password');
% preview the camera
preview(cam);

% close preview
closepreview(cam);

% Or get a snapshop...
img = snapshot(cam);
imshow(img);

% release camera
clear cam;

第一次调用该函数时,MATLAB 可能会提示您下载它。
好消息是,该解决方案甚至不需要相机采集工具箱的许可证。

Since MATLAB R2015a it's become very easy with the function ipcam:

cam = ipcam('http://172.28.17.193/video.mjpeg', 'admin', 'password');
% preview the camera
preview(cam);

% close preview
closepreview(cam);

% Or get a snapshop...
img = snapshot(cam);
imshow(img);

% release camera
clear cam;

The first time you call that function MATLAB may prompt you to download it.
The good news is that solution doesn't even require a license to the camera acquisition toolbox.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文