How to get and plot boundary/vertex of drawn polygon
10 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
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
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!