Measuring distance between centroid and perimeter points

2 次查看(过去 30 天)
My code currently allows me to draw three polygons on an image and then plot the centroid. How do I measure the distance between the centroid and each perimeter point?
Here is my current code:
close all, imshow(I),
for k = 1:3
title(['Polygon # ' int2str(k)]);
h = drawpolygon;
wait(h);
centroid=mean(h.Position);
drawpoint('Position',centroid,'Label','Centroid');
c{k}(1:2) = round([cx cy],0); % unit: pixels
c{k}(3) = round(area(polyin{k}),0)*p2cm; % unit: cm^2
disp(['Centroid of polygon: [' int2str([cx cy]) ']'])
disp(['Area of polygon = ' int2str(c{k}(3)) 'cm^2'])
end
Thanks!

回答(1 个)

KSSV
KSSV 2022-3-8
If C is your centroid coordinates and P is your m*2 array of points. Where m is the number of points. Then you can get the distance using:
d = sqrt((C(1)-P(:,1)).^2+(C(2)-P(:,2)).^2) ;
Alo have a look on pdist2.

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by