MATLAB: Write loop for averaging every 20 images out of 5000

doit4meimage averageimage loopno attempt

Hello and thanks in advance for any help!
I'm trying to come up with a loop to average every 20 images out of over 5000. In essence, I want to average images 1-20, 21-40, 41-60 etc. until the end, and then run code for image processing on those averaged images. The images are .jpg files, all of which are in the same folder. I have no idea on how to setup the loop and haven't had much success researching it online.
Any help is greatly appreciated!!

Best Answer

Code to get you started is in the FAQ. http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F You'll need 2 for loops: one that goes over every image in steps of 20, and then an inner one that does the average of the 20. Be sure you cast to double before summing the images because a uint8 image will clip at 255 if it would exceed that. Post your code (attempt at it) if you cannot figure it out.