Plot polygons as image to process
15 次查看(过去 30 天)
显示 更早的评论
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?
Thanks!
0 个评论
采纳的回答
Simon Chan
2023-2-23
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.
Regarding to the warning message, you may refer to this: https://www.mathworks.com/matlabcentral/answers/606836-why-does-the-polyshape-function-deletes-the-vertices-in-the-input-when-there-are-no-duplicated-poi
Consider the following example with a very simple polyshape:
pgon = polyshape([20 20 17 15],[7 3 2 9]);
[x,y] = centroid(pgon)
Now, convert it to an image:
A = zeros(25,25);
for k = 1:length(pgon.Vertices)
A(pgon.Vertices(k,2),pgon.Vertices(k,1))=1;
end
J = bwconvhull(A);
regionprops(J,'Centroid','Orientation')
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)
A(pgon.Vertices(k,2)*MagFactor,pgon.Vertices(k,1)*MagFactor)=1;
end
J = bwconvhull(A);
stat = regionprops(J,'Centroid','Orientation')
cent = stat.Centroid/MagFactor
figure
ax1 = subplot(1,2,1);
plot(pgon);
axis(ax1,'image');
ax1.XLim = [1 25];
ax1.YLim = [1 25];
ax2 = subplot(1,2,2);
imshow(J);
ax2.YDir='normal';
更多回答(1 个)
Image Analyst
2023-2-23
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');
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Elementary Polygons 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!