MATLAB: Save images and histograms in a sequence.

functionhistogramimageprocessedprocessingsavingsequence

Hi,
I have a function that detects images in a folder and begins to analyse them. At the end of each analysis, I want it to save the processed image, a text file for mean values and the histograms generated from each image in sequence.
E.g.
Image 1 is loaded, processed and 2 histograms generated. I want to save this as Image 1 – processed, text file and the 2 histograms.
Then it does the same for Image 2 and so on.
Is this possible?

Best Answer

The function for creating directories is mkdir. Create the output directory before the processing loop with:
outputdir = fullfile(ImgDirectory, 'Results');
mkdir(outputdir);
To save your file in that directory at the end of the processing loop, you need to prepend the directory name to the file name with fullfile, same as you've done for loading it. sprintf is indeed the function you need to build the file name, read up on the various format fields, %s is what you need to insert a string:
%at the end for K = 1:I2P loop
%assuming that baseFileName does not include extension:
outputfile = sprintf('Process Image - %s.jpg', baseFileName);
%otherwise:
[~, truebasename] = fileparts(baseFileName);
outputfile = sprintf('Process Image - %s.jpg', truebasename);
%save in directory
imwrite(img, fullfile(outputdir, outputfile));
There are various functions for saving text files, csvwrite, dlmwrite, writetable or just do it with low level IO with fopen/fprintf/fclose which allows you to format the file any way you want.
As for saving the histogram figures, saveas or print are the functions to use.