How can I process multiple image with for loop?
5 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
David Young
2014-12-27
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!