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

1 次查看(过去 30 天)
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

回答(1 个)

Subhadeep Koley
Subhadeep Koley 2020-11-6
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 个评论
Subhadeep Koley
Subhadeep Koley 2020-11-8
编辑:Subhadeep Koley 2020-11-8
@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

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by