How to calculate the average radius of a boundary in Matlab ?

18 views (last 30 days)
How to calculate the average radius of a boundary in Matlab ?
% Example
rng('default')
x = rand(30,1);
y = rand(30,1);
plot(x,y,'.')
xlim([-0.2 1.2])
ylim([-0.2 1.2])
k = boundary(x,y,0.5);
hold on;
plot(x(k),y(k));

Accepted Answer

Image Analyst
Image Analyst on 3 Oct 2022
Try this:
% Example
rng('default')
x = rand(30,1);
y = rand(30,1);
plot(x,y,'r.', 'MarkerSize',30)
grid on;
xlim([-0.2 1.2])
ylim([-0.2 1.2])
k = boundary(x,y,0.5);
hold on;
xb = x(k);
yb = y(k);
plot(xb, yb, 'b.-', 'LineWidth', 2, 'MarkerSize',30);
polyin = polyshape(xb, yb);
[xCtr, yCtr] = centroid(polyin)
plot(xCtr, yCtr, 'g+', 'LineWidth', 2, 'MarkerSize',50)
% Get the area
area = polyarea(xb, yb)
% Get the equivalent circular radius
% area = pi * r^2 so r = sqrt(area/pi)
radius = sqrt(area/pi)
% Show circle on graph.
viscircles([xCtr, yCtr], radius); % Requires Image Processing Toolbox.
axis square % So circle looks circular not elliptical.
xCtr =
0.57896610590936
yCtr =
0.522015154012119
area =
0.579388629608274
radius =
0.429447469135391

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by