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

조회 수: 11 (최근 30일)
S.
S. 2022년 3월 25일
댓글: Simon Chan 2023년 3월 10일
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);

답변 (1개)

Simon Chan
Simon Chan 2022년 3월 25일
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
Eric
Eric 2023년 3월 9일
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."
Simon Chan
Simon Chan 2023년 3월 10일
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);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Animation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by