Size of saved figure differs from size of frame2im(getframe(gcf))

I need to create big images in which displaying lot of annotations, but my computer has a low screen resolution 1366x768. So I looked on this site for a workaround to get bigger figures, and I found the command set(gcf, 'Position', get(0, 'Screensize')) which sets the figure size to 2134x1095 (I don't why this particular value though... is there a way to create figures with custom size in pixels?).
However, when using the frame2im(getframe(gcf)) command I get a 701x1366x3 matrix instead of a 1095x2134x3 matrix, am I doing something wrong? This is just an example to replicate the problem
clc; clear all; close all
figure('visible','off')
set(gcf, 'Position', get(0, 'Screensize'))
saveas(gcf, 'aaa.png')
im = frame2im(getframe(gcf));
[size(im) ; size(imread('aaa.png'))]

Answers (1)

saveas() is not a good option for exporting high-resolution images. Use exportgraphics() and specify the required resolution. See this example: https://www.mathworks.com/help/releases/R2020a/matlab/ref/exportgraphics.html#mw_b4d91f8c-574f-4574-89af-37d54c5a182b.
exportgraphics() was introduced in R2020a, for older versions, see print(). For example
print('filename.jpg', '-r300'); % for printing at 300dpi
print('filename.jpg', '-r300');

7 Comments

Thank you for the fast answer, exportgraphics is only from R2020a and I'm on R2019b, how do I update to R2020a? Moreover, I don't understand how can I choose a desired resolution in pixels using DPI? I mean, DPI doesn't specify the aspect ratio right?
You can try print() or export_fig() which are available in R2019b. These functions will preserve the aspect ratio, as shown in the figure window. These functions will save the image as it is displayed on the figure window.
Thank you Ameer for support. My script automatically creates many figures (containg only annotations) hence I don't want that a windows is opened each time, that's why I use the figure('visible','off') command.
When using figure('visible','off') a figure object is created, and then with set(gcf, 'Position', get(0, 'Screensize')) the figure is set to 2134x1095, then I can add many annotations on the same figure.
As you suggested I tried both print and export_fig
figure('visible','off')
output = num2str(toeplitz(1:40))
annotation('textbox',[0 1 0 0],'String',output,'FitBoxToText','on','FontSize',14,'FontName','Consolas')
print('aaa.png', '-r300')
but the image generated cannot be opened it says it is not a valid format.
figure('visible','off')
annotation('textbox',[0 1 0 0],'String',output,'FitBoxToText','on','FontSize',14,'FontName','Consolas')
export_fig aaa.png
the image generated is small 560x420 (see below) and contains only a small part of the output variable (which is a 40x40 matrix), is there an option to show all the output variable and customize the pixels resolution? Thank you again
export_fig should be showing capturing the complete figure. I think some part of the annotation is going outside the limits of the figure window. Maybe try to resize the figure before export_fig. Also, try to use the functional syntax.
figure('visible','off')
set(gcf, 'Position', get(0, 'Screensize'))
annotation('textbox',[0 1 0 0],'String',output,'FitBoxToText','on','FontSize',14,'FontName','Consolas')
export_fig(gcf, 'aaa.png', '-r300'); % resolution of 300dpo
Thank you Ameer, however in this way we cannot customize the size of the figure, because the command set(gcf, 'Position', get(0, 'Screensize')) fix the size to a certain value. Using the -r300 option we can increase the definition, but not the size of the figure.
Anyway, the problem for which I wrote this question is another one, that is: why the matrix generated by frame2im(getframe(gcf)) does not have the same size of the figure?
For example, by running
figure('visible','off')
saveas(gcf, 'aaa.png')
im = frame2im(getframe(gcf));
[size(im) ; size(imread('aaa.png'))]
we get
ans =
420 560 3
656 875 3
Ok. If you want to set the width and height of the figure in pixels, then you can specify a four-element vector like this [x y w h]. Where x is the distance (in pixels) from the left edge of screen, and y is the distance from the bottom of the screen. w and h are the width and height of the figure window.
set(gcf, 'Position', [0 0 500 500])
This command will set the figure window to the bottom left corner and set the width and height to 500 each.
Thank you Ameer very kind, I tried with this code, but still the matrix and the image have different sizes, I begin to think that this problem cannot be solved easily
figure('visible','off')
set(gcf, 'Position', [0 0 500 500])
saveas(gcf, 'aaa.png') % 781x781
im = frame2im(getframe(gcf)); % 500x500
[size(im) ; size(imread('aaa.png'))]
ans =
500 500 3
781 781 3

Sign in to comment.

Categories

Find more on Printing and Saving in Help Center and File Exchange

Products

Release

R2019b

Tags

Asked:

on 27 Aug 2020

Commented:

on 28 Aug 2020

Community Treasure Hunt

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

Start Hunting!