Hi guys! So i have this question: is there a way to double loop a lot of images?
What i mean is that i know how to loop over a lot of images (of the same type for example .png) lets say i have the next 10 images with names: c1=10.2 , c2=10.2 up to c10=10.2 with the next lines of code i can read them all and do what i want next with them
for n=1:10
images{n} = imread(sprintf('c%d=10.2.png',n));
%code ....
end
but if i lets say want to loop over this for loop and have lets say a code that for the numbers 10 30 50 70 and 100 reads firstly the images with names c1=10.2 up to c10=10.2 and then does the previous for loop and when that for loop is done does the same thing for the c1=30.2 up to c10=30.2 and then for 50 and then 70 and then finally for 100 c1=100.2 up to c10=100.2 .
so it will read
c1=10.2 .... c10=10.2 and then read all the images c1=30.2 up to c10=30.2 and then c1=50.2 up to c10=50.2 and then c1=70.2 ... c10=70.2 and finally c1=100.2 .... c10=100.2
I hope i expained it well !

5 个评论

The sprintf function accepts multiple inputs so you can simply nest the two loops.
thank you for your answer Rik! do you mean like
for m=10:100
for n=1:10
images{n,m} = imread(sprintf('c%d=%d0.2.png',n,m));
%code ....
end
end
im not sure how to write the new sprintf ,if you can show me it would be great
ok so i think this code works
for m=10:20:70
for n=1:10
images{n,m} = imread(sprintf('c%d=%d.2.png',n,m));
%code ....
end
end
but is it reading the images in the corect way ??
c1=10.2 .... c10=10.2 and then read all the images c1=30.2 up to c10=30.2 and then c1=50.2 up to c10=50.2 and then c1=70.2 ... c10=70.2 and finally c1=100.2 .... c10=100.2
Just try it:
for m=10:20:70
for n=1:10
sprintf('c%d=%d.2.png',n,m)
end
end
yeah you are write Jan thank you guys you are awesome!!! So there is no way for it to read the pics up to 100 since they are not +20 from 70 ?? i can only read 10 30 50 and 70 write? not 100 also?

请先登录,再进行评论。

 采纳的回答

Try this:
folder = pwd;
% Now we have a list of all the filenames.
% Now how another loop that reads in one image with names like 10, 30, 50, ...
% and an inner loop that does 1-10.
outLoopValues = [10, 30, 50, 70, 100] % Whatever values you want.
for k = 1 : length(outLoopValues)
index = outLoopValues(k);
for n = 1 : 10
thisBaseFileName = sprintf('c%d=%d.2.png', n, index);
thisFileName = fullfile(folder, thisBaseFileName);
if isfile(thisFileName)
fprintf('Processing "%s"\n', thisFileName);
% Code to call imread and process this image ...
else
% File doesn't exist.
fprintf(' !!!! Skipping non-existent file: "%s"\n', thisFileName);
continue;
end
end
fprintf('\n'); % Print blank line between groups.
end
fprintf('Done running %s.m\n', mfilename);

3 个评论

You may not be able to read in and store all the images in a cell array, so just read it in and don't store it in another array. Just call imread() in the inner loop and process the image right then and there.
Thank you very much Image Analyst!
sorry to bother you again but how can i add the images that i read inside the double loop?
i tried
K = imadd(img_rot{n,index},'uint16');
but it didn't work

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by