How to get and plot boundary/vertex of drawn polygon

8 ビュー (過去 30 日間)
Michael Boyle
Michael Boyle 2022 年 4 月 7 日
回答済み: Amish 2023 年 9 月 15 日
I am trying to use drawpolygon to trace an imported image and then plot the boundary in a normal figure elsewhere. Matlab seems to take the position in terms of pixels from the top left corner of the image, rather than x,y coordinates. Is there a way to convert the pixel coordinates to x,y or a way to get the x,y coordinates of the drawn boundary?
h = drawpolygon('FaceAlpha',0);
h.Position

回答 (1 件)

Amish
Amish 2023 年 9 月 15 日
Hi Michael,
I see that you are having issue with the Coordinates while using the ‘drawpolygon’ method where the coordinates are used considering the distances from the top-left corner while you desire it to be the XY coordinates.
The actual problem in this case is that it is essential for you to understand the coordinate system used. In MATLAB, images are typically represented using pixel coordinates, with the origin (0,0) located at the top-left corner of the image. If you want to convert pixel coordinates to the (x, y) coordinate system, you'll need to consider the scaling factor and the image size.
Here is a generic code that might help you to convert the pixel coordinates to (x, y) coordinates:
% Get the Image Size
[imageHeight, imageWidth, ~] = size(yourImage); % Replace 'yourImage' with your image variable
% Convert Pixel Coordinates to (x,y) Coordinates
x = (pixelX / imageWidth) * imageXRange;
y = (pixelY / imageHeight) * imageYRange;
% Now proceed with your workflow
h = drawpolygon('FaceAlpha',0);
h.Position
You can also refer to the official documentation and example here: https://www.mathworks.com/help/images/ref/drawpolygon.html
Hope this helps.
Regards
Amish

カテゴリ

Help Center および File ExchangeScatter Plots についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by