How to convert current figure to matrix 1:1?

5 次查看(过去 30 天)
The following code firstly saves the figure to an image, then converts the same figure into a matrix which however has not the same resolution (in pixels) of the image, that is the matrix has a different number of rows and columns, why does this happen?
figure('visible','off')
fplot(@(x) sin(x),[0 2*pi])
saveas(gcf, 'test_saveas.png') % 656x875
im = frame2im(getframe(gcf));
imwrite(im, 'test_frame2in.png') % 420x560
[size(im) ; size(imread('test_saveas.png'))]
ans =
420 560 3
656 875 3
Is it possibile to convert the figure into a matrix preserving the resolution?

回答(1 个)

Walter Roberson
Walter Roberson 2020-8-28
saveas() uses print()
print() uses the figure PaperSize and PaperPosition, and a default resolution that appears to be 100 pixels per inch. It does not just capture the screen: it internally redraws the figure at the resolution implied by PaperSize, and saves that.
getframe() on the other hand, uses whatever the screen resolution and figure size is.
"Is it possibile to convert the figure into a matrix preserving the resolution?"
Preserving which resolution? The figure with the toolbar cropped out? The axes OuterPosition ?
If you are using a high resolution screen and MATLAB has silently rescaled for you, which resolution do you want to use?
  1 个评论
giannit
giannit 2020-8-28
编辑:giannit 2020-8-28
Thank you Walter for support! I'm using a HD ready monitor (1366x720), I would like to have a matrix whose size is the same as the image saved using the saveas command, that is a matrix with 656 rows and 875 columns.
The images generated with saveas and with frame2im+imwrite are identical except for the pixels resolution https://imgur.com/a/3Q8nf7Y
The problem has not to do with axes I think, since the same happens if we just plot annotations or even nothing, for example
figure('visible','off')
saveas(gcf, 'test_saveas.png') % 656x875
im = frame2im(getframe(gcf));
imwrite(im, 'test_frame2in.png') % 420x560

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Printing and Saving 的更多信息

标签

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by