How to save images from for loop to use for machine learning algorithm?

I have a for loop that generates a different image each time it loops.
I want to save each of these images to input them into a machine learning algorithm (without overriding the images). I know that I would use "imagedatastore" but am confused on how to use it.
Also, once I have my machine learning algorithm, how would I call these stored images into it?
Here's an example of my code:
for i=1:20
volume=randi([2 10], 1,1)
location=randi([10 100], 1, 1)
image=imagegenerator(volume,location)
end

Réponses (1)

imageDatastore can only create an image datastore from the collection of image data specified by their location. However, you're generating images using the custom imagegenerator function. In your case, saving them in a cell array might help. Refer the code below.
% Define empty cell array
imageStack = cell(1, 20);
% Generate images and save them in the cell array
for idx = 1:20
vol = randi([2 10], 1, 1);
loc = randi([10 100], 1, 1);
imageStack{idx} = imagegenerator(vol, loc); % Here each element of the cell array contains one image
end

4 commentaires

Hi! Well, I'd like to save the images in some directory to be able to call them into python & cluster them using k-means. If they're in a cell array in matlab, I can't do this right?
@Rachel Dawn What does the function imagegenerator gives as output? Does it returns a RGB / grayscale image (i.e. a 2D / 3D matrix)?
@Rachel Dawn does the function imagegenerator producing a figure window for each run of the for-loop? If yes then the below code might help
for idx = 1:20
vol = randi([2 10], 1, 1);
loc = randi([10 100], 1, 1);
imagegenerator(vol, loc);
currentFrm = getframe(gcf);
currentImg = frame2im(currentFrm);
imwrite(currentImg, ['img_', num2str(1), '.png'])
end

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by