Clear Filters
Clear Filters

Save figure at specific/original size

19 views (last 30 days)
Veilchen1900
Veilchen1900 on 29 Sep 2016
Answered: Anusha Sridharan on 5 Oct 2016
Hi, I have an overlayed figure which I would like to save with the same size (401x521) as the two original images (401x521). Now I get an image with the size 419x560. Thanks for the help!
This is my code:
%%Image Overlay (Image 1: not transparent, Image 2: transparent)
% Convert grayscale image to RGB image
Color = repmat(Image1,[1 1 3]);
% Display RGB image (da 3x der gl. Inhalt, bleibt es ein Graubild)
figure;
imagesc(Color);
% Creation of a matrix with 0.3 values
alpha_matrix = 0.3*ones(size(Image2,1),size(Image2,2));
hold on
% Display Image2 as RGB image
h = imagesc(Image2);
colormap (cool)% Choice of color for Image2
set(h,'AlphaData',alpha_matrix); % Über den Set-Befehl wird die alpha_matrix mit dem Bild2 verbunden
% Remove axis,frame
set(findobj(gcf, 'type','axes'), 'Visible','off')
set(gca,'position',[0 0 1 1],'units','normalized')
% Save figure without frame
AxesH = gca; % Not the GCF (gca=get current axis)
F = getframe(AxesH);
imwrite(F.cdata,'ImageOverlay');
  1 Comment
Adam
Adam on 29 Sep 2016
Why are you using getframe to save a static image? Can't you just save the data directly? Is it the data mapped via the colourmap that you are trying to save?

Sign in to comment.

Answers (1)

Anusha Sridharan
Anusha Sridharan on 5 Oct 2016
Every time MATLAB creates a figure, it is created with a default size, this is where the 419 and 560 dimensions come from which are the default height and width of the figure.
The reason you get an image with size 419*560 is because you are setting the axes to fit the figure in this line
set(gca,'position',[0 0 1 1],'units','normalized')
You could fix this issue by changing the line below
set(gca,'position',[0 0 1 1],'units','normalized') to
set(gca,'Units','pixels'); %changes the Units property of axes to pixels
set(gca,'Position',[1 1 521 401] %The first two elements correspond to the left and bottom define the distance from the lower-left corner of the container to the lower-left corner of the axes, the next two elements are the width and the height which can be updated to reflect the Image dimensions.
For more information, please the following documentation link for Axes properties:
Alternatively, depending on your work-flow you could also change the figure size to reflect the size of the axes containing the image such that the axes is always fit to the figure.
Change this line
figure; to figure('Position', [680 678 521 401]);
% Here the four-element vector corresponds to [left bottom width height]. 680 and 678 for the left and bottom elements correspond to the default values used by MATLAB while creating a figure and width, height can be updated to reflect the image size.
For more information, please the following documentation link for Figure properties:

Community Treasure Hunt

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

Start Hunting!