asynchronous video reader

Read video files or camera streams parallel to your main matlab program for real time performance.
329 Downloads
Updated 24 Apr 2016

View License

Reading videos using the inbuilt videoReader from matlab can be quite slow. This mex function enables you to read frames from video files or your webcam/cameras parallel to your main matlab program to get real time performance. When reading multiple videos simultaneously it can also make use of multiple cores on your machine to speed up the reading.
NOTE: this mex file makes use of mexopencv which can be installed from https://kyamagu.github.io/mexopencv
NOTE: If some files of mexopencv don't compile, you can just delete the corresponding .cpp files from pathToYourMexOpenCVinstall/mexopencv/src/+cv
NOTE: this mex file is tested under windows 10, and will not work under LINUX based systems due to the use of windows parallelization functions (including Apple OS X).

How to install
---------------
Install mexopencv according to: https://kyamagu.github.io/mexopencv/

Place asyncVideoReader.cpp in pathToYourMexOpenCVinstall/mexopencv/src/+cv
Place asyncVideoReader.m in pathToYourMexOpenCVinstall/mexopencv/+cv

cd to your mexopencv path e.g: cd('C:/path/to/mexopencv')
type: mexopencv.make in your command window

How to use
----------
Initialize a video:
vidObj = cv.asyncVideoReader('init video','C:/path/to/your/video.mp4');

Initialize a camera/webcam:
vidObj = cv.asyncVideoReader('init camera',cameraID);
Where the cameraID = 0,1,2,...
Your webcam is likely at cameraID = 0

Read from the video or camera:
frame = cv.asyncVideoReader('read frame',vidObj);
If the returned frame is empty you reached the last frame of the
video

Release a video or camera:
cv.asyncVideoReader('release',vidObj);
You should always release a video after you're done with it to
free memory.

Or release all videos simultaneously:
cv.asyncVideoReader('clear all');

Examples
--------
Initialize a video:
vid2read = [matlabroot '\mcr\toolbox\matlab\audiovideo\xylophone.mp4'];
vid0 = cv.asyncVideoReader('init video',vid2read);

Initialize a camera/webcam:
vid1 = cv.asyncVideoReader('init camera',0);

Read from the video or camera:
frameVideo = cv.asyncVideoReader('read frame',vid0);
frameCamera = cv.asyncVideoReader('read frame',vid1);

Release a video or camera:
cv.asyncVideoReader('release',vid0);
cv.asyncVideoReader('release',vid1);

Or release all videos simoultaneously
cv.asyncVideoReader('clear all');

Example 1, read a video with a while loop:
vid2read = [matlabroot '\mcr\toolbox\matlab\audiovideo\xylophone.mp4'];
vid0 = cv.asyncVideoReader('init video',vid2read);
frameRGB = 0;

while ~isempty(frameRGB)
frameRGB = cv.asyncVideoReader('read frame',vid0);
% do something with the frame
end

cv.asyncVideoReader('release',vid0);
cv.asyncVideoReader('clear all'); % Or just clear all

Example 2, read from your webcam:
vid0 = cv.asyncVideoReader('init camera',0);

for i = 1:300 % read up to 300 frames
frameRGB = cv.asyncVideoReader('read frame',vid0);
% do something with the frame
end

cv.asyncVideoReader('release',vid0);
cv.asyncVideoReader('clear all'); % Or just clear all

Example 3, read a video with a for loop:
vid2read = [matlabroot '\mcr\toolbox\matlab\audiovideo\xylophone.mp4'];
vid0 = cv.asyncVideoReader('init video',vid2read);

for frame = 1:vid0.frames
frameRGB = cv.asyncVideoReader('read frame',vid0);
% do something with the frame
end

cv.asyncVideoReader('release',vid0);
cv.asyncVideoReader('clear all'); % Or just clear all

Cite As

Wout Oude Elferink (2024). asynchronous video reader (https://www.mathworks.com/matlabcentral/fileexchange/56686-asynchronous-video-reader), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2015b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.1.0.0

Improved performance and bug fixes.

1.0.0.0

Updated the description.