필터 지우기
필터 지우기

How to save each image generated in a for loop?

조회 수: 28 (최근 30일)
Rachel Dawn
Rachel Dawn 2020년 11월 2일
댓글: Rachel Dawn 2020년 11월 6일
for i=1:20
volume=randi([2 10], 1,1)
location=randi([10 100], 1, 1)
imagegenerator(volume,location)
end
^This is a simplified example of my code.
I generate random variables which are inputted into a function called imagegenerator, which generates an image.
With the for loop, I'm generating several random images.
I want to
1) save each image with a different name each time (i.e "image 1" "image 2" "image 3", etc.)
2) also save the inputs (volume, location) associated with each image (preferably in the same folder/ with the same name "image 1" "image 2" , etc.)
How can I do this? Thanks!
P.S. I want to be able to use all these images/ their inputs later to put into a machine learning algorithm that'll cluster the images based off the pixels or the inputs (not sure yet).

채택된 답변

Abolfazl Chaman Motlagh
Abolfazl Chaman Motlagh 2020년 11월 2일
Hi ,you Can use imwrite for saving images. Also use save for 'volume' and 'location'. (you can make datastore and write a Read_function for datastore to import them sequentially for your ML training).
for datastore see this : datastore
Also for images you can use this : imageDatastore
here's some hint: (consider your imagegenerator has image in output).
for i=1:20
volume=randi([2 10], 1,1)
location=randi([10 100], 1, 1)
Name=['image ' num2str(i)];
save([Name '.mat'],'volume','location');
I=imagegenerator(volume,location);
imwrite(I,[Name '.jpg'])
end
  댓글 수: 7
Abolfazl Chaman Motlagh
Abolfazl Chaman Motlagh 2020년 11월 3일
편집: Abolfazl Chaman Motlagh 2020년 11월 3일
about first question : the image is tiny propobly because the size itself . i don't know this related or not , just guessing. but use size(I) if you don't know the size already.
about inverted image. as you can see argument in imagesc is -x. thats why you see an image but the inverted is saving as image. you can use this transform :
image=-image; % invert the image
% because the values are less than 0 if you save it and read it again you'll see the image corrupted
% so need to sacle it to be between 0 to 1
I=(image-min(image,[],'all'))/(max(image,[],'all')-min(image,[],'all'))
% then save I as an image
this will scale up your image for reading.
Abolfazl Chaman Motlagh
Abolfazl Chaman Motlagh 2020년 11월 3일
imageDatastore is a class for saving image addresses. you should have a cell that every cell include path to an image . then write this :
imds=imageDatastore(paths)
for many of application in matlab imageDatastore is supported as Train (Test or validation) of ML or deep learning training process.
every time you use read on your datastore , an image come out as an output.
also Labels of train dataset can be saved in this object.
see:
it doesnt need read function for this class.
if you use special dataset you can use datastore
datastore (example : use .mat file as a dataset)
but in most cases you should write read_function yourself.
for example this function that train a Network need imagedatastore as first argument.
net = trainNetwork(imds,layers,options)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

S. Walter
S. Walter 2020년 11월 2일
편집: S. Walter 2020년 11월 2일
You can print the image:
print('filename','-dpng','-r300')
where '-dpng' can be set to whatever format you want (check out the help file) and '-r300' is the resolution ('-r0' is screen resolution).
To change the filename to image 1 etc you can add the following to your for loop
f_name = ['image ' num2str(i)];
print(f_name,'-dpng','-r300')
Make sure you close your figures after you're done printing them.
Edit: In terms of saving the data, you can just use
save('filename','volume','location')
That will save it as a Matlab .mat file. If you want to write it to file, you should check out "writematrix" or "fprintf".
  댓글 수: 5
S. Walter
S. Walter 2020년 11월 3일
You can either try
A = gcf
at the end of that snippet of code (gcf = "get current figure"). This stores the figure handle in A.
Or you can do it before:
% PLOT DENSITIES
% Open a new figure window
A = figure;
% Add a colormap
colormap(gray);
% Display the image - store the handle in A
imagesc(-x);
% Make the axis equal and tight
axis equal;
axis tight;
% Turn the axis off
axis off;
% Pause for a moment
pause(1e-6);
Rachel Dawn
Rachel Dawn 2020년 11월 6일
Very helpful. Thank you!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by