The most proper way to save images

13 views (last 30 days)
Hello,
I am reading images and then applying some filters on them. In addition I want to save the output in a different folder. Yet the memory usage increases gradually.
here is the code I utilize:
for jj = 1:3000 % There are 3000 images.
im = imread(fname); % 'fname' is defined for each image in above. it changes and it is preallocated at the beginning.
im = im(20:770,70:820); % Defining a proper frame size
h = fspecial('gaussian',101,15); % Filtering the image
bim = imfilter(im,h,'replicate'); % Filtering the image
bim = imfill(bim,'holes'); % Empty the center of the image
bim = imbinarize(bim); % Turning the images into binary
figure (jj);
set(figure(jj), 'visible', 'off');
imcontour (bim,1,'r'); % Drawing the contour of the image.
axis square;
folder = 'c:\PATH';
ContourFileName = sprintf('Contour_for_image_no_%d',jj);
fullContourName = fullfile(folder, ContourFileName);
set(gcf,'Units','inches');
screenposition = get(gcf,'Position');
set(gcf,'PaperPosition',[0 0 screenposition(3:4)],...
'PaperSize',screenposition(3:4));
saveas(gcf,fullContourName,'tif');
end
I was wondering if there is another way to save the images without using a lot of memory. Because of the amount of images, after the 1000th images. it uses almost 95 percent of both memory RAM and CPU.
Thanks a lot for the help in advance and wish you a happy day filled with joy.
  2 Comments
Walter Roberson
Walter Roberson on 1 May 2019
Do not use a different figure each time. Or delete the figure after you save the image.

Sign in to comment.

Accepted Answer

NIMA FARHANG
NIMA FARHANG on 1 May 2019
Do not use a different figure each time. Or delete the figure after you save the image.
(Credits go to @ Walter Roberson )

More Answers (0)

Categories

Find more on Images in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!