Average all frames of a video in MATLAB

Alec Jacobson

December 16, 2013

weblog/

Here's a snippet to import a video in MATLAB and average all frames.

nf=video.NumberOfFrames;
avg = zeros([video.Height video.Width 3]);
for f = 1:nf
  avg = avg + (1/nf).*im2double(read(video,f));
end;

I get a bit of a boost (200%) by using a parfor parallel for loop:

nf=video.NumberOfFrames;
avg = zeros([video.Height video.Width 3]);
parfor f = 1:nf
  avg = avg + (1/nf).*im2double(read(video,f));
end;