Saving a montage image
28 views (last 30 days)
Show older comments
I created a montage of an RGB and logical image and stored t into a variable 'h'. The content of the variable 'h' is of the type 'image'. I would like to save 'h' into a folder. I have tried to use saveas and imwrite but I am unable to do so. My code and the resulting errors are as follows. How can I save the image in variable 'h'?
ImFileOut = fullfile(ImOutFolder, ImName)
ImCell = {RGBIm, LogicalIm};
h = montage(ImCell); %file type is '1x1 Image'
saveas(h,ImFileOut);
% Error using saveas
% Invalid handle.
imwrite(h,ImFileOut);
%Error using imwrite (line 427)
%Expected DATA to be one of these types:
%numeric, logical
%Instead its type was matlab.graphics.primitive.Image.
0 Comments
Accepted Answer
Rik
on 27 Apr 2019
There is a difference between an image and an image object. The latter is a graphics object that is shown in the axes. It does contain the data required to write the montage to an image. The code below generates some example data and then writes a montage to a file. You can adapt it to suit your specific needs.
%generate required variables
ImOutFolder=pwd;
ImName='example_montage.png';
RGBIm=randi(127,100,100,3,'uint8');
LogicalIm=logical(randi([0 1],100,100));
%compose inputs
ImFileOut = fullfile(ImOutFolder, ImName);
ImCell = {RGBIm, LogicalIm};
%create montage and extract color data from the image object
figure(1),clf(1)%create a clean figure (use clf only in debugging)
h=montage(ImCell);
montage_IM=h.CData;
%write to file
imwrite(montage_IM,ImFileOut);
4 Comments
Rik
on 9 Sep 2020
The CData property is in the doc: on the page with properties of image objects.
If you want to hide things in a loop you have two options: open a figure at the beginning and set Visible to off. Then use explicit handles when calling montage. The other option is to reimplement montage yourself, which is what I did myself.
More Answers (0)
See Also
Categories
Find more on Interactive Control and Callbacks 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!