how to save the specific variable for each image when I have done batch image processing?

Hello everybody
I have done a batch of image processing. I would like to compare all the images based on one or two variables (the results that I am finding in the code) Let's say, I am finding a number of pixel in the mask for each image and I need to see the result of each image. When I process all images, it only shows the last image I processed in the workspace. Can I save the result of each image as a MAT-file?
Thanks

 Accepted Answer

"When I process all images, it only shows the last image I processed in the workspace."
No problem.
"Can I save the result of each image as a MAT-file?"
Yes, very easily in your loop, like this:
for kk = ...
...
val = ... % variable to be saved
vec = ... % variable to be saved
fnm = sprintf('myfile%04d.mat',kk); % filename
save(fnm,'val','vec')
end
See also:

8 Comments

Thanks for that. it worked perfectly. However I was able save as matfile1, matfile2, matfile3.... can I save them all as one data and play with them to plot?
@engineer: sure. Preallocate an array and put the images into it during the loop. Something like this:
N = numel(...) % how many loop iterations.
C = cell(1,N); % preallocate the data cell array.
for kk = 1:N
...
C{kk} = ... % store the image /whatever data.
end
Now when the loop is finished all of your data is in the cell array C.
Engineer, regarding "I save them all as one data", you can save all the values in the loop in an array, then save the array after the loop exits. That way you'll have just one .mat file. See my answer below.
I stored all my data in C as a cell array. I converted the cell array to numeric array by using the cell2mat function. After this step, for example, I selected 2 variables and stored them using 4 different images and I would like to plot them. How can I do that?
If you used cell2mat() afterwards, then there was probably no reason to use a cell array in the first place. What kind of data are you putting into the cells? Why does it require a cell array rather than simple double array(s)?
I don't know what "stored them using 4 different images" means. Are the data 2-D matrices so that they can be represented as an image? And what does it mean to "plot" an image? Do you mean "display" the data as an image?
For example, I have 4 images, when I processed them, I saved 2 variables for each image. Now, based on frame number (x-axis), I wanna see how the variables (y-axis) are changing as a figure.
Like I said, no reason to use a cell array. Let's say your two variables are the mean and standard deviation. You can do
subplot(1, 2, 1);
for k = 1 : numberOfImages
% Code to change image, then
imshow(grayImage);
drawnow;
meanValues(k) = mean2(grayImage);
sdValues(k) = std2(grayImage);
end
% Save all values in a .mat file.
save(matFullFileName, 'meanValues', 'sdValues');
subplot(1, 2, 2);
plot(meanValues, 'b-');
plot(sdValues, 'r-');
legend('mean', 'std dev');
If you want to store the filenames along with the data in a single variable you can use a structure array or table. Avoid a cell array. They're confusing as to when to use braces or parentheses, and they can use a lot more memory than simpler types of variables if they get really large.
s(k).meanValues = mean2(grayImage);
s(k).sdValues = std2(grayImage);
s(k).FileName = imageFileName;
"no reason to use a cell array"
And no reason to avoid them!
Using cell arrays makes it trivial to write code that works efficiently with any size data, including images that have different dimensions, thus avoiding awkward errors (this is something known as robust code). It also makes preallocating the output array much simpler than preallocating numeric array/s of the right size.
"Avoid a cell array. They're confusing as to when to use braces or parentheses"
That is one opinion, which I disagree with, built around a weasel word. In any case, cell array indexing is very simple:
  • {} curly braces to access the cell array contents,
  • () parentheses to access the cells themselves.
"they can use a lot more memory than simpler types of variables if they get really large."
Seriously, you gave that as a reason to avoid cell arrays! That has got to be a joke! Is this likely to be a problem for some hundreds or thousands of images? Does your computer still only have 64 KiBi of memory, wound onto little ferrite toroids?
Rather than hindering working with large data (i.e. lots of image), a cell array does not require contiguous memory for its contents, so creating it is really fast. Allocating one huge, contiguous numeric array is slow and makes processing slow... if it can be achieved at all for the required size.
While Image Analyst may have their own opinion on cell arrays, note that the MATLAB documentation actually uses cell arrays for reading image data into MATLAB:
Feel free to follow the examples of the MATLAB documentation, written by the people who actually wrote MATLAB.

Sign in to comment.

More Answers (1)

In the loop, just save the value into an array, for example, if you're looping over k and computing mean values:
for k = 1 : whatever
% Code to change image, then
meanValues(k) = mean2(grayImage);
end
% Save all values in a .mat file.
save(filename, 'meanValues');

Asked:

on 7 Aug 2018

Edited:

on 8 Aug 2018

Community Treasure Hunt

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

Start Hunting!