Write loop for averaging every 20 images out of 5000

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!!

3 Comments

the images are named as such:
prt_028738.jpg up to prt_086831.jpg
That's fine. The FAQ, as mentioned in my answer, will work fine. I recommend using the version that uses the dir() function if your files already exists that way you can be sure of processing all of the files that actually exist and not getting a filename that may not exist.

Sign in to comment.

 Accepted 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.

8 Comments

My attempted code is attached.
It seems to only reed every 20th file and not average them. I also need to be able to save the averaged image in order to incorporate it into the movie.
Rangley, look at this line:
s=sum(double(j));
What are you summing? You are summing the loop index, not the images. Now that I told you, I think you realize that that is incorrect and I think you should know how to fix it to sum images instead.
After looking back on your other comments to other questions...here is what I have been able to get so far. It is still not working properly but am I at least on the right track now? Notice** I'm only trying the first 20 now instead of the entire folder of images.
That's because of this line:
for w = 1:20:20%length(jpegFiles);
The total number of files is commented out. Change it to
for w = 1:20:length(jpegFiles);
Ok. So it is reading the rgbImage (1024x1360x3) but when it sums the images the dimensions of sumImage is changed to (1x1360x3) and the same for the meanImage. So the output image is just a line. Any idea why that might be?
You have to cast it back to uint8 before sending the mean image into imshow().
I did that it but meanImage is still only 1x1360x3 uint8 which only shows the line. Is there something wrong with the way I am summing the images to make it turn out that way?
Rangley, you're not summing the images! You are just assigning the latest image to sumImage:
sumImage = sum(double(rgbImage));
You need to do that only for the first frame, but for subsequent frames, you need to sum it in:
sumImage = sumImage + sum(double(rgbImage));

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!