How can I process multiple image with for loop?

5 visualizaciones (últimos 30 días)
Yoann
Yoann el 27 de Dic. de 2014
Respondida: David Young el 27 de Dic. de 2014
Hi,
I know that this question was still asked but I didn't find my answer. I want to process multiple images with a for loop when images are open.
What I did:
img1=imread('30.jpg');
img2=imread('25.jpg');
img3=imread('20.jpg');
for i=1:3
imagename=strcat('img',num2str(i))
figure;
imshow(imagename);
end
I check and imagename return img1, img2 and img3. But imshow function can't work with these names and an error occured. Matlab return me:
??? Error using ==> getImageFromFile at 14
Cannot find the specified file: "img1"
Error in ==> imageDisplayParseInputs at 74
[common_args.CData,common_args.Map] = ...
Error in ==> imshow at 199
[common_args,specific_args] = ...
Error in ==> test at 13
imshow(imagename);
I don't understand why have I an error because that they can't find "img1".
If somebody can help me, it will be very nice.
Thank you,
Yoann

Respuestas (1)

David Young
David Young el 27 de Dic. de 2014
You are passing a string to imshow, which it interprets as a filename. However, there isn't a file called "img1". If you wish to read all the images into main memory (MATLAB workspace) and then process them, it's best to use a cell array, like this:
img{1}=imread('30.jpg');
img{2}=imread('25.jpg');
img{3}=imread('20.jpg');
for i=1:3
figure;
imshow(img{i});
end
Alternatively you can store the file names as a cell array of strings and read them in one at a time, just before displaying them:
imfile{1}='30.jpg';
imfile{2}='25.jpg';
imfile{3}='20.jpg';
for i=1:3
img = imread(imfile{i});
% process img here if you wish
figure;
imshow(img);
end
I recommend revising MATLAB strings and array types - the introductory documentation is very helpful.

Categorías

Más información sobre Image Processing Toolbox en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by