ImagePosition, FigPosition, and AxisPosition perplexity

3 ビュー (過去 30 日間)
francois heslot
francois heslot 2023 年 4 月 4 日
回答済み: Sandeep Mishra 2024 年 12 月 6 日
Hello,
How to get the coordinates of the 4 corners of an image within a figure,
and how it relates to the figure position, the axis position, the xlimits, the ylimits, and possibly the PlotBoxAspectRatio.
also:
imshow('saturn.png');
% then eventually manipulate the figure position, the axis position, the xlim, the ylim.
%
% Why the following code below fails to get the proper image corners (lower left x-y point, width, height),
% in pixel units ?
axlim = get(gca,'Position')
fglim = get(gcf,'Position')
x1 = axlim(1)*fglim(3) + fglim(1);
x2 = (axlim(1)+axlim(3))*fglim(3) + fglim(1);
y1 = axlim(2)*fglim(4) + fglim(2);
y2 = (axlim(2)+axlim(4))*fglim(4) + fglim(2);
Thanks,
  1 件のコメント
Rik
Rik 2023 年 4 月 4 日
I'm not entirely certain the size of the axes object will change if the image requires a smaller window. You might need to get the position of the image object (perhaps by querying the XData and YData properties).

サインインしてコメントする。

回答 (1 件)

Sandeep Mishra
Sandeep Mishra 2024 年 12 月 6 日
Hi francois,
I understand that you want to find the four corners of an image within a figure.
To implement this, you can fetch the X and Y axis limits of the current axis using ‘XLim’ and ‘YLimproperty and based on that you can calculate the width and height of the image.
Refer to the following example code snippet to find the image corners:
% Load and display the image
img = imread('saturn.jpg');
imshow(img);
% Fetch axes limits
x_limits = get(gca, 'XLim');
y_limits = get(gca, 'YLim');
% Finding image staring points
x_start = x_limits(1);
y_start = y_limits(1);
% Calculating width and height of the image
width = x_limits(2) - x_limits(1);
height = y_limits(2) - y_limits(1);
% Image coordinates
image_coordinates = [
x_start, y_start;
x_start + width, y_start;
x_start + width, y_start + height;
x_start, y_start + height
];
For more information, refer to the following MathWorks Documentation:
  1. XLim' property: /https://www.mathworks.com/help/releases/R2024b/matlab/ref/xlim.html
  2. YLim’ property: https://www.mathworks.com/help/releases/R2024b/matlab/ref/ylim.html
I hope this helps!

カテゴリ

Help Center および File ExchangeFeature Detection and Extraction についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by