re. finding perimeter of a 2D shape

2 次查看(过去 30 天)
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);
shp.Alpha=2.5;
plot(shp)
totalperim = perimeter(shp)

采纳的回答

Anton Semechko
Anton Semechko 2018-6-14
% Contour coordinates
A=xlsread('Profgraph.xls','sheet2');
% Compute perimeter
N=size(A,1);
D=A(:,2:N)-A(:,1:(N-1));
D=sqrt(sum(D.^2,2)); % edge lenghts
P=sum(D); % perimeter
disp(P)
Answer comes out to 386.19
  2 个评论
Nithin
Nithin 2018-6-14
编辑:Nithin 2018-6-14
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 2018-6-14
My bad:
% Compute perimeter
N=size(A,1);
D=A(2:N,:)-A(1:(N-1),:);
D=sqrt(sum(D.^2,2)); % edge lenghts
P=sum(D); % perimeter
disp(P)

请先登录,再进行评论。

更多回答(1 个)

Steven Lord
Steven Lord 2018-6-14
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];
plot(x,y,'.')
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)));
pause(0.1)
delete(h)
end
The alpha values returned by criticalAlpha are in the alphaspec variable.

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by