how to save the specific variable for each image when I have done batch image processing?
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Stephen23
2018-8-7
编辑:Stephen23
2018-8-7
"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 个评论
Image Analyst
2018-8-8
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;
Stephen23
2018-8-8
编辑:Stephen23
2018-8-8
"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.
更多回答(1 个)
Image Analyst
2018-8-7
编辑:Image Analyst
2018-8-7
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');
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!