MATLAB: Find the average frame from avi file

average frame from avi file

Hi, I am supposed to find the average frame from a avi file(which has 635 frames). To find the average frame,I am trying to read all the frames and iteratively add each one of them and finally divide it by the total number of frames. Following are my questions: 1. Is it the correct way of finding the average frame from a avi file. 2.Is there any other method of finding the average,since the above method is computationally expensive as it takes a long time. 3.When i divide the final frame with the number of frames then its all the values are 0.So the average frame is dark. 4. Also, I think i should get only 1 frame as an average frame,but after running the code i again find that the number of frame is same as the number of frame in original avi file.
Please help! Here is my code: The details of the video avi file is General Settings: Duration = 177.0880 Name = fruitfly.avi Path = /home/tanmay Tag = Type = mmreader UserData = []
Video Settings:
BitsPerPixel = 24
FrameRate = 25
Height = 238
NumberOfFrames = 635
VideoFormat = RGB24
Width = 502
Here is my code:
clc
clear
obj=mmreader('fruitfly.avi');
info=get(obj);
nframes=obj.NumberOfFrames;
frame=read(obj,1);
gray_add=zeros([size(frame,1) size(frame,2) 3], class(frame));
for i= 1:nframes
singleframe=read(obj,i);
gray_add=gray_add+singleframe;
gray_avg(:,:,:,i)=gray_add;
end
gray_avg=gray_avg/nframes;
framerate=obj.FrameRate
implay(gray_avg,framerate)

Best Answer

"frame" is most likely a uint8 variable, so to avoid clipping, you need to cast your frame to double before summing it. See this (untested) code:
rgbSum = double(frame); % Initialize accumulator for color frames.
for fr = 2 : nframes % Start with frame 2.
% Get one color uint8 frame and cast to double.
thisFrame = double(read(obj, fr));
% Accumulate it.
rgbSum = rgbSum + thisFrame;
end
% Get the mean by dividing by the number of frames.
rgbMean = rgbSum / nframes;
Also note that I'm not using i (the imaginary variable) as the loop index like you did. And I'm not sure why you called things "gray" unless the video really is a 8 bit monochrome video. No need to make gray unless you want to for some reason.
Related Question