Save each image loaded by a loop

1 次查看(过去 30 天)
MF
MF 2016-5-11
编辑: Adam 2016-5-11
I have 4 bmp images named Target1.bmp ..Target4.bmp and I want to load them using a loop. I am using the following code but it is giving me only the data of the last image. How can i adjust it to load all the 4 images and each one store it as separately. Thx
for k = 1:4
filename = strcat('Target', num2str(k),'.bmp');
TargetImg = imread(filename);
end

回答(1 个)

Adam
Adam 2016-5-11
编辑:Adam 2016-5-11
TargetImgs{k} = imread(filename);
will put them in a cell array. If all your images are the same size and you intend to do further analysis across all images it would be much better to store them in a 3d numeric array where the 3d dimension is, in this case, 4, but a cell array is the more generic approach if you can't guarantee the images are all the same size.
You can presize with e.g.
TargetImgs = cell.empty( 0, 4 );
before the loop if you wish, but I'm not sure it makes any real difference with a cell array of just 4 values.

标签

Community Treasure Hunt

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

Start Hunting!

Translated by