Search code examples
matlabsavemontage

Save montage as image matlab


I have 225 image with put together with the montage function in matlab. And I can show them with montage. But i cannot save the montage as a complete image, please help me.

path = uigetdir;

D=dir(fullfile(path,'*.tif'));

imcell = cell(1,numel(D));
imcropped = cell(1,numel(D));

figure(1);
title('Drag square to crop picture, end with a double click',...
  'FontSize', 15 , 'HandleVisibility' , 'off' ) ;
axis equal
set( gca , 'NextPlot' , 'replacechildren') ;
imcell1 = imread(D(50).name);
[~, rect] = imcrop(imcell1);
close(figure(1));
% 
for i = 1:numel(D)
  imcell{i} = imread(D(i).name);
  imcropped{i} = imcrop(imcell{i}, rect);
end

h=montage(cat(4,imcropped{:}),'Size', [15 15] );

The output on montage "h" is just a number.


Solution

  • You're almost there! The value 'h' is actually the handles to the image object created by the montage you made in the figure. What you can do is use getframe to capture the content of the figure (graphics object) and save it as an image. Here is a very simple example, with the code going directly after yours

    h=montage(cat(4,imcropped{:}),'Size', [15 15] );
    
    MyMontage = getframe(gca) %// Get content of current axes. I did it with sample images.
    

    The output is the following:

    MyMontage = 
    
           cdata: [384x1024x3 uint8] % Yours will be different
        colormap: []
    

    Hence you can save the actual data, stored in cdata, in a new file and you're good to go!

    imwrite(MyMontage.cdata,'FancyName.tif','tif');