MATLAB: How to sum up about 3-4 color frames and only show the brightest frame. Matlab

obj = VideoReader('C:\Users\Robotics\Pictures\Saved Pictures\Lightfile\Prelight.MOV');
for img = 1:obj.NumberOfFrames;
filename = strcat('frame',num2str(img),'.jpg');
b = read(obj,img);
imwrite(b,filename);
end
I have this code that reads the file and get all frames, but I need it to sum up about 3-5 frames then take the brightest frame and store it. Can someone help please!!

Best Answer

The below code assumes that the frames are RGB.
The below code does not attempt to detect intervals of flash and determine the best out of the interval: it takes the frames frames_per_group at a time and determines the brightest of that group.
obj = VideoReader('C:\Users\Robotics\Pictures\Saved Pictures\Lightfile\Prelight.MOV');
frameidx = 0;
frames_per_group = 5;
frame_history = [];
frameidx_history = [];
while hasFrame(obj)
frameidx = frameidx + 1;
groupidx = groupidx + 1;
b = readFrame(obj);
frame_history = cat(4, frame_history, b);
frameidx_history(end+1) = frameidx;
if size(frame_history, 4) == frames_per_group
maxbrightness = -inf;
maxbrightnessidx = -inf;
for K = 1 : frame_per_group;
grayframe = rgb2gray(frame_history(:,:,:,K));
thisbrightness = sum(grayframe(:));
if thisbrightness > maxbrightness
maxbrightness = thisbrightness;
maxbrightnessidx = K;
end
end
bestframe = frame_history(:,:,:,maxbrightnessidx);
bestframeidx = frameidx_history(maxbrightnessidx);
filename = sprintf('frame%04d.jpg', bestframeidx);
imwrite(bestframe, filename);
frame_history = [];
frameidx_history = [];
end
end
(Code not tested)
Related Question