reading multiple images with two indexes using imread

5 views (last 30 days)
Hi all! I am trying to upload multiple images using imread. All images are stored in the same folder. Each image comes in three different versions, therefore I named the files in the following way: "ani_X_level_Y.png", where X identifies the number of the image and Y identifies the number of version of the same image. I wrote the following script, but it seems like it uploads only the last file in the folder.
startfile=1;
endfile=3;
startlevel=0;
endlevel=2;
%load animate images
for i=startfile:endfile;
for s=startlevel:endlevel;
ani=imread(['ani_',num2str(i),'_level_',num2str(s),'.png']);
end
end
I am sorry for the basic question, but I have just started using matlab and I don't have any programming experience. I would be grateful if you could kindly help me with that.
Chiara :)

Accepted Answer

Geoff Hayes
Geoff Hayes on 2 May 2014
Hi Chiara,
Within your inner for loop, you are doing the following:
ani=imread(['ani_',num2str(i),'_level_',num2str(s),'.png']);
So at the first iteration, (i==1,s==0) the code reads the image and sets it to the ani local variable. But then on the next iteration (i==1,s==1) the code reads the image and re-assigns it to the ani variable again and so overwrites the previous imread. That is why it appears that only the last image is being read - because each subsequent imread overwrites the previous one.
If all images are the same size, then you could create some sort of multidimensional array where the first dimension is the number of images (I think that there are 9) and the subsequent dimensions are the rows (height) and columns (width) and number of colours (3 if RGB) for the image. Then on each iteration, just read in the image and store it in the ith element of this array.
An easier/quicker way (especially if the images are of different sizes) is to just read the images into a cell array:
images = {}; % create an empty cell array
k = 1; % index into cell array for next read image
startfile=1;
endfile=3;
startlevel=0;
endlevel=2;
%load animate images
for i=startfile:endfile;
for s=startlevel:endlevel;
% read the image into the kth position of the cell array
images{k}=imread(['ani_',num2str(i),'_level_',num2str(s),'.png']);
% increment k for the next iteration
k = k + 1;
end
end
And so all images are stored in the cell array (images) and you can do whatever you want with them from there.
Geoff

More Answers (1)

Image Analyst
Image Analyst on 2 May 2014
While your loop executes, you load the image into "ani" and it just gets overwritten each iteration, so when your loop is done, only the last image will be in ani. What you should do depends on what you want to do. What I would do is to put a function call inside your loop to process the image:
allResults = [];
for i=startfile:endfile;
for s=startlevel:endlevel;
filename = sprintf('ani_%d_level_%s.png', i, s)
if exist(filename, 'file')
% File does exist.
ani=imread(filename);
theseResults = AnalyzeSingleImage(ani);
allResults = [allResults; theseResults];
else
% File does not exist.
message = sprintf('Warning: skipping missing file:\n%s', filename);
uiwait(warndlg(message));
end
end
end
Note how I made it more robust by checking for the filename after creating it instead of just blindly assuming it's there and attempting to use it. When you're done, all your results are in the allResults array. If for some reason you want to store all your images, then you can use Geoff's code to save every single image in a cell array but then you'll have to again have another nested for loop where you analyze/process the images that you saved into the cell array.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!