Save images and histograms in a sequence.

2 views (last 30 days)
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?
  2 Comments
Guillaume
Guillaume on 25 Sep 2014
The straightforward answer to your question is: Yes
I assume it's not all you want to hear, so if you have a problem with one particular aspect ask a more specific question. You seem to have developped the bulk of the function. In comparison what you're asking is fairly simple.
msij
msij on 26 Sep 2014
Fair enough.
Basically, my function reads in a bunch of images like so:
for k = 1:I2P;
baseFileName = TiffFiles(k).name;
fullFileName = fullfile(ImgDirectory, baseFileName);
I = imread(fullFileName);
end
It then goes through a whole set of processing and analysis. Finally it gives me 2 mean values, a final image and a histogram. I have used dlmwrite to save the text file, however when I try put them in a sequence to save according to the original file name it just stops the function. The same happens for imwrite. So my question is, how can I save the final image in the same folder I loaded it from, with the 'Orginalname_Processed' as it's name.
I tried:
for K = 1:I2P;
outputFileName = sprintf('Processed Image - #%d.jpg',K);
imwrite(img, outputFileName );
end
The trouble I'm having is firstly, it is saving to the location of the m-file whereas I want to create a new folder title 'Results' in the folder of the images and secondly, I want the image name to be 'Processed Image - OriginalFileName.JPG'. I'm not too sure how to get these to work but I saw the above code on a different question and thought i'd try it out.

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 26 Sep 2014
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.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!