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

2 ビュー (過去 30 日間)
Rachel Dawn
Rachel Dawn 2020 年 11 月 6 日
編集済み: Subhadeep Koley 2020 年 11 月 8 日
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 日
@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