3

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.

4

2 回答 2

4

I would like to point out a better way to do it. While Benoit_11's way is technically correct, it limits the resolution of the image to the size of your screen. When you use getframe(gca), Matlab is effectively taking a screenshot of the current axes contents, at whatever size your figure window is currently in.

A better way to do this is to use the handle, as it references the actual graphical output of montage() instead of what it displays as. To save an image from the handle, you need to get the cdata from the object it references with get:

h=montage(cat(4,imcropped{:}),'Size', [15 15] );
MyMontage = get(h, 'CData');
imwrite(MyMontage, 'FancyName.tif', 'tif');

This way you get the full resolution of the montage, not just the resolution from displaying it.

For more information on image handles: http://www.mathworks.com/help/matlab/creating_plots/the-image-object-and-its-properties.html

于 2015-09-02T19:50:41.243 回答
1

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');
于 2014-10-10T17:58:25.353 回答