getframe(), rectangle specified in normalized coordinates instead of pixel coordinates

I want to use the getframe() option to make a snapchat of a part of my figure. However, the documentation says that getframe(__, rect), the rectangle coordinates are in pixels. However, to ensure that I will get the same snapshot on another computer, how can I give rect in normalized coordinates?
rect = [65 385 720 255];
f = getframe(h.f, rect); %h.f is the parent container
f = frame2im(f);

Respuestas (1)

Try the following workaround
f = figure;
f.Units = 'normalize';
f.Position = [0.25 0.25 0.5 0.5]; % Location of the figure on the screen
ax = gca;
ax.Units = 'pixels';
plot(1:10,1:10);
ti = ax.TightInset;
pos = ax.Position;
rect = [-ti(1), -ti(2), pos(3)+ti(1)+ti(3), pos(4)+ti(2)+ti(4)];
F = getframe(ax,rect);

4 comentarios

S.
S. el 28 de Mzo. de 2022
Editada: S. el 28 de Mzo. de 2022
I get the error: Rectangle width and height must be at least 1 pixel, as pos is still in normalized coordinates and getframe() expects pixels.
The code should work as follows without any error message.
However, I tested again and found that the TightInset properties is read only and hence the figures saved by the above method may looks a little bit different.
The workaround is to assign a specific axis position for the figure so that the size of the figures saved from any screen resolution should be the same.
f = figure;
f.Units = 'normalize';
f.Position = [0.25 0.25 0.5 0.5]; % Location of the figure on the screen
ax = gca;
ax.Units = 'pixels'; % Set units to pixels
plot(ax,1:10,1:10);
ti = ax.TightInset;
ax.Position = [100 50 500 300]; % This should be specific
pos = ax.Position;
rect = [-ti(1), -ti(2), pos(3)+ti(1)+ti(3), pos(4)+ti(2)+ti(4)];
F = getframe(ax,rect);
ax.Position
ans = 1×4
100 50 500 300
% imwrite(F.cdata,'test1.png') % save the figure
May I ask why -ti(1) and -ti(2)?
Surely that will give negative pixel values that will cause getframe( ) to fall over?
In my example the 4th value (pos(4)+ti(2)+ti(4)) also gives a value that is outside the screen area (greater than its height).
In any case I get the dearly-loved error (with my code, not yours): "The specified rectangle is not fully contained within the figure. MATLAB no longer supports this capability."
Accoring to the Control Axes Layout mentioned here. The 'InnerPosition' is too small where the axes labels and title will not be captured while the 'OuterPosition' is too large which has too much empty space.
Extract the 'TightInset' property and added to the 'InnerPosition' to make the region larger:
pos = ax.Position; % InnerPosition of axis
rect = [-ti(1), -ti(2), pos(3)+ti(1)+ti(3), pos(4)+ti(2)+ti(4)];% Added the margin
Yes, they are negative values for the left and bottom points but they are relative to the axes only and still within the figure window. However, if you manually adjust bottom and left of the 'OuterPosition' property to some negative values, I think the above method is going to be fail.
Try again here and could you share your code.
f = figure;
f.Units = 'normalize';
f.Position = [0.05 0.25 0.4 0.7]; % Location of the figure on the screen
x = rand(1,10);
y = rand(1,10);
z = duration(rand(10,1),randi(60,10,1),randi(60,10,1));
plot3(x,y,z,'o','DurationTickFormat','mm:ss')
xlabel('X')
ylabel('Y')
zlabel('Duration')
grid on
ax = gca;
ax.Units = 'pixels'; % Set units to pixels
ti = ax.TightInset;
pos = ax.Position;
rect = [-ti(1), -ti(2), pos(3)+ti(1)+ti(3), pos(4)+ti(2)+ti(4)];
F = getframe(ax,rect);
f2 = figure;
imshow(F.cdata);

Iniciar sesión para comentar.

Preguntada:

S.
el 25 de Mzo. de 2022

Comentada:

el 10 de Mzo. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by