re. finding perimeter of a 2D shape
조회 수: 3 (최근 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)
댓글 수: 0
채택된 답변
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
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
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 Center 및 File Exchange에서 Bounding Regions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!