MATLAB Answers

re. finding perimeter of a 2D shape

12 views (last 30 days)
Nithin on 14 Jun 2018
Commented: Nithin on 15 Jun 2018
Hi, I have coordinates of a 2D shape (x,y) and I am trying to determine the perimeter of the shape. I had initially tried the below code on circles etc, using the alpha shape function and perimeter with a given alpha radius of 2.5 and it worked fine. But my current shape I cant seem to use the alpha radius, as with alpha radius the perimeter is coming as zero. But without it works but I dont know if the result if correct as while plotting the figure seems a bit incomplete ie, the area within the contour is not completely filled. I am attaching the shape file, Thank you, Nithin
the code:
[Data.num] = xlsread('Profgraph.xls', 'sheet2');
nx50 = Data.num(1:end,1 ); ny50 = Data.num(1:end,2);
shp = alphaShape(nx50,ny50);
totalperim = perimeter(shp)


Sign in to comment.

Accepted Answer

Anton Semechko
Anton Semechko on 14 Jun 2018
% Contour coordinates
% Compute perimeter
D=sqrt(sum(D.^2,2)); % edge lenghts
P=sum(D); % perimeter
Answer comes out to 386.19


Nithin on 14 Jun 2018
Thank you for the alternative solution. Will look into this. :) Nithin There is some indexing error in the second line d = y-x. But hope to fix it.
Sorry, When I run the code with D=A(:,2:end)-A(:,1:(end-1));
I get around 585.71, Dont know what mistake I am doing.
Anton Semechko
Anton Semechko on 14 Jun 2018
My bad:
% Compute perimeter
D=sqrt(sum(D.^2,2)); % edge lenghts
P=sum(D); % perimeter

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 14 Jun 2018
Is an alpha value of 2.5 going to be appropriate for all data sets? Probably not.
You probably want to use the criticalAlpha and/or alphaSpectrum function to find an appropriate alpha value for your data set. Using the example from the alphaSpectrum page:
% Generate sample data and plot it
th = (pi/12:pi/12:2*pi)';
x1 = [reshape(cos(th)*(1:5), numel(cos(th)*(1:5)),1); 0];
y1 = [reshape(sin(th)*(1:5), numel(sin(th)*(1:5)),1); 0];
x = [x1; x1+15;];
y = [y1; y1];
axis equal
hold on
% Build an alphaShape using the default alpha value
shp = alphaShape(x,y);
% Determine alpha values that produce unique shapes
alphaspec = alphaSpectrum(shp);
% Iterate through the alpha values. Show each one in turn
% with a 0.1 second pause between each shape
for k = 1:length(alphaspec)
shp.Alpha = alphaspec(k);
h = plot(shp);
title(sprintf('Alpha value of %.16f', alphaspec(k)));
The alpha values returned by criticalAlpha are in the alphaspec variable.

  1 Comment

Nithin on 15 Jun 2018
Thank you both very much :)

Sign in to comment.