Find the average frame from avi file
Show older comments
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)
Accepted Answer
More Answers (1)
Walter Roberson
on 29 May 2012
0 votes
You can read in all of the frames, put them in to a single array, and then mean() to get the average.
But in the meantime, remove the "class(frame)" from your zeros() . Summing a large number of uint8() values is almost certain to saturate a uint8 variable, giving you a result of 255 for that location. Divide that by 635 and convert to uint8 and you get 0.
Categories
Find more on Image Arithmetic in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!