How to convert current figure to matrix 1:1?

조회 수: 2 (최근 30일)
giannit
giannit 2020년 8월 28일
편집: giannit 2020년 8월 28일
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