reading multiple images with two indexes using imread
Show older comments
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
More Answers (1)
Image Analyst
on 2 May 2014
There is a FAQ entry on the topic: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F.
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.
1 Comment
Chiara Avancini
on 7 May 2014
Categories
Find more on Matrix Indexing 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!