writing a sequence of images inside a for loop using matix

i have sequence of images formed by processing inside a for loop. I need to write them into a new folder using the concept of matrix

 Accepted Answer

Jishnu - since your code produces a new image at each iteration of the for loop, then you could do something like the following
% initialize the folder/path
folderToSaveImg = '/Users/myName/someDirectory/images';
% do your loop
for k=1:100
% do the processing to create the image named img
img = randi(255,100,100,3); % example only
% create the file name
fileName = sprintf('image%d.mat',k);
% join the file name to the folder name
fileDestination = fullfile(folderToSaveImg, fileName);
% save the image as a matrix
save(fileDestination,'img');
end
The above code (example only) will save 100 images from the sequence to individual mat files where the data stored in the file is a three dimensional matrix.

4 Comments

my aim is to store each image using a matrix having the same size of image formed. this code returns a sequence of matrix. But I need output as images itself
Since you want to store the image as an image rather than a matrix, then just choose the format of the image (to save to) and use that instead. Replace much of the above loop with
% create the file name
fileName = sprintf('image%d.bmp',k);
% join the file name to the folder name
fileDestination = fullfile(folderToSaveImg, fileName);
% save the image to file
imwrite(img, fileDestination);
The last line will write the image to file in the bitmap format.
Jishnu, your comment seems to indicate that you think an image and a matrix are different things. Why is that? In MATLAB, an image is a matrix, so you don't need 2 things: an image and some separate matrix. They are one and the same.

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!