Clear Filters
Clear Filters

Plot polygons as image to process

14 views (last 30 days)
Hi guys,
I have data of a curve which I want plot it as an image. And later use function regionprops to get its orientation and centroid.
Is there any way I can do that, or directly get the orientation of the curve data.
The image is the polygon plot of the curve, while the data has not duplicate point, the function return:
"Warning: Polyshape has duplicate vertices, intersections, or other inconsistencies that may produce inaccurate or
unexpected results. Input data has been modified to create a well-defined polyshape."
And delete half of my data to draw that image, any suggestion I could do to prevent that?

Accepted Answer

Simon Chan
Simon Chan on 23 Feb 2023
You may use function centroid to determine the centroid of a polyshape.
When you convert the polyshape into an image, the result of the centroid would be different due to the resolution of a pixel on an image.
Consider the following example with a very simple polyshape:
pgon = polyshape([20 20 17 15],[7 3 2 9]);
[x,y] = centroid(pgon)
x = 17.7984
y = 5.4419
Now, convert it to an image:
A = zeros(25,25);
for k = 1:length(pgon.Vertices)
J = bwconvhull(A);
ans = struct with fields:
Centroid: [17.6875 5.5000] Orientation: 63.9994
If you want to increase the resolution, for example, 100 times. This would give you a closer value.
MagFactor = 100;
A = zeros(25*MagFactor,25*MagFactor);
for k = 1:length(pgon.Vertices)
J = bwconvhull(A);
stat = regionprops(J,'Centroid','Orientation')
stat = struct with fields:
Centroid: [1.7798e+03 544.2466] Orientation: 65.4331
cent = stat.Centroid/MagFactor
cent = 1×2
17.7977 5.4425
ax1 = subplot(1,2,1);
ax1.XLim = [1 25];
ax1.YLim = [1 25];
ax2 = subplot(1,2,2);

More Answers (1)

Image Analyst
Image Analyst on 23 Feb 2023
If you have the x and y of the outline, you can use poly2mask to turn it into an image and then use regionprops to get its orientation and centroid.
mask = poly2mask(x, y, rows, columns);
props = regionprops(mask, 'Orientation', 'Centroid');


Find more on Elementary Polygons in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!