Sam D.
Sam D.

Reputation: 72

How to analyse video frames in parallel using Matlab?

I'm working on processing large video files frame-by-frame. The processing for each frame is independent of other frames (when decompressed) and is also computationally intensive, so I figured parallel processing would be a great way to speed up my analysis. While I have taught myself the basics of using parallel loops, I'm having problems combining the specifics of parfor with VideoReader objects. In my mind I imagine the code running like this

 video = VideoReader('video.mp4'); 
 parfor ii = 1 : 90000
     frame = read(video, ii);
     ...analysis on frame...
 end

However this warns me against using read() because it will be removed in a future version, so the only alternative I know of is to use frameRead(). However frameRead uses the CurrentTime property of the VideoReader object, which increments itself (according to the fps) each time frameRead is called. This works fine for reading frames in a normal loop, but it makes parfor unhappy because each frame relies on increasing the CurrentTime according to the last. Is there a way to access independent frames in a parallel loop using readFrame or otherwise? I've tried to set the CurrentTime value in each loop by using the loop index and the frame rate like this:

 video = VideoReader('video.mp4');
 fps = video.FrameRate
 results = cell(totalFrames, 1);
 parfor ii = 1 : 900000
     video.CurrentTime = ii/fps;
     frame = readFrame(video);
     results{ii} = customAnalysisFunction(frame)
 end

In this example the parfor is underlined/flagged and the reason is provided in this message:

MATLAB runs loops in parfor functions by dividing the loop iterations into
groups,and then sending them to MATLAB workers where they run in parallel.
For MATLAB to do this in a repeatable, reliable manner, it must be able to
classify all the variables used in the loop. The code uses the indicated
variable in a way that is incompatible with classification

What steps can I take to be able to read video frames in parallel that is compatible?

Should I just use the read function? What are the reasons why I shouldn't? Are there other video tools for Matlab?

One solution that is often suggested to me is, why don't you split up the video into separate clips? I don't want to do this because it is very slow and requires a lot of extra steps and file handling. It find it hard to believe that there isn't a solution to this problem within Matlab, so I'm looking forward to your answers!

Upvotes: 0

Views: 353

Answers (1)

Laleh
Laleh

Reputation: 508

I don't expect that reading frames in parallel would work in MATLAB. The video reader is an object that has an internal state about where it is positioned. You might try to work with a copy of the object. Take a look at this: http://mathworks.com/help/matlab/ref/matlab.mixin.copyable-class.html

Upvotes: 1

Related Questions