Find the average frame from avi file
조회 수: 4 (최근 30일)
이전 댓글 표시
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)
댓글 수: 0
채택된 답변
Image Analyst
2012년 5월 29일
"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.
댓글 수: 3
Ramya Sampath
2012년 10월 14일
편집: Image Analyst
2012년 10월 14일
Hi i tried like that too.. But i didnt get the accumulated image output Lets send the correct code.. Or correct my code it follows.
if true
% code
clc
clear
obj=mmreader('D:\ME PROJECT\Swarna proj\Project Code\Dataset\Videos1\daria bend.avi');
info=get(obj);
nframes=obj.NumberOfFrames;
frame=read(obj,1);
rgbSum=double(frame);
% gray_add=zeros([size(frame,1) size(frame,2) 3], class(frame));
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
figure(1),imshow(rgbSum);
% Get the mean by dividing by the number of frames.
rgbMean = rgbSum / nframes;
figure(2), imshow(uint8(rgbMean));
end
Pls say me the mistakes i did in advance...!
Image Analyst
2012년 10월 14일
편집: Image Analyst
2012년 10월 14일
It looks right to me except for the imshow(rgbsum). Why do you say there is a mistake? You'll notice that I did not have that imshow(rgbSum) line in my code because it won't work unless you pass rgbSum into im2double() first. But there is absolutely no point in that because you'd see the same thing in the next line once you take the average and cast to uint8. Is that why you say there is a mistake, because the first imshow line was all white?
추가 답변 (1개)
Walter Roberson
2012년 5월 29일
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.
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!