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.
-
4FYI, this is the answer 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." – Alexey Dec 30 '11 at 13:51
-
1Frame rate is an issue with IMREAD function in MATLAB - it only grabs single images, but not a stream. I am going the route of compiling my OpenCV C++ code to Matlab mex function. 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. – Alexey Jan 04 '12 at 18:30
-
1THANKs for the mexopencv link. I am using the videoio library since 2 years now and was quite pleased with it. It's great but compiling was a hassle. The mexopencv installation was straightforward. The examples are great und the mex library is done in an awesome way. I think I'll switch to mexopencv and opencv right now. – georg Dec 03 '12 at 21:42
-
I recommend you to post the solution you created as an answer, otherwise this question will remain open. – Dennis Jaheruddin Dec 18 '12 at 12:39
2 Answers
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:
- 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
. 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); }
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.
-
1Here I am exactly two years later trying to do the same thing. Were you ever able to accomplish this? – Kyle Wright Dec 18 '14 at 22:44
-
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.

- 1,456
- 9
- 20