How to get and plot boundary/vertex of drawn polygon

조회 수: 9 (최근 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

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by