How to find the arc length of a trajectory?

2 次查看(过去 30 天)
Hello , Regards!
I want to find the arc length of every length . I am trying to find the position of every point with bwmorph(img,'endpoints') but Matlab doesn't give me in order the position of every point how I need it. I tried to find the Euclidean distance between points and then the angle between them (to find the arc length L = angle * radius) but the problem is that I can't accommodate the coordinates in the correct order of each start and end of each length stretch of Arc
I have this:
img = imread('img.bmp');
img = rgb2gray(I);
g=bwmorph(img,'thin','inf');%thining
figure,imshow(g)
B1 = bwmorph(g,'endpoints');%endpoints
figure,imshow(B1);
[ye xe] = find(B1);%Position end points

回答(1 个)

Stephan
Stephan 2019-10-15
编辑:Stephan 2019-10-16
Maybe it helps if you know the center coordinates and the radius of your circle -after that you can calculate the angles and use them to calculate the arc-lengths:
% your original code
img = imread('image.bmp');
img = rgb2gray(img);
g=bwmorph(img,'thin','inf');%thining
% Find the center and radius of the circle
g1=imdilate(g,strel('square',20));
g1=bwmorph(g1,'thin','inf');
g1=imfill(g1,round((size(g1)./2)),4);
[center, radius] = imfindcircles(g1,[180 220],'ObjectPolarity','bright','Method','TwoStage','Sensitivity',0.95);
% Proceeding with your code
B1 = bwmorph(g,'endpoints');%endpoints
[ye, xe] = find(B1);%Position end points
% Sort the values to plot the lines later in the right order
Pe = [];
Pe_temp = [xe, ye, xe-center(1), center(2)-ye];
Pe = [Pe; sortrows(Pe_temp(Pe_temp(:,3)>0 & Pe_temp(:,4)>0,:),[4 -3])];
Pe = [Pe; sortrows(Pe_temp(Pe_temp(:,3)<0 & Pe_temp(:,4)>0,:),[-3 -4])];
Pe = [Pe; sortrows(Pe_temp(Pe_temp(:,3)<0 & Pe_temp(:,4)<0,:),[-4 3])];
Pe = [Pe; sortrows(Pe_temp(Pe_temp(:,3)>0 & Pe_temp(:,4)<0,:),[3 -4])];
Pe(2:numel(xe)+1,:) = Pe;
Pe(1,:) = Pe(end,:);
% Calculate Arc-Lengths
vec = [Pe(:,3), Pe(:,4)];
angles = zeros(1,numel(xe)/2);
n = 1;
for k = 1:2:numel(xe)-1
angles(n) = acosd(dot(vec(k,:)./norm(vec(k,:)),vec(k+1,:)./norm(vec(k+1,:))));
n = n + 1;
end
% Tada: The result
arc_lengths = radius*pi*angles./180;
% Plot results
figure
Lines = gobjects(1,numel(xe));
imshow(g)
hold on
for k = 1:size(Pe,1)-1
Lines(k) = drawline('Position', [Pe(k,1), Pe(k,2); center],'label',string(k));
end
scatter(xe,ye,'or')
scatter(center(1),center(2),'xg')
hold off
% Clean up
clearvars -except arc_lengths angles
The results appear to be correct to me - but you should check them by yourself. Dont miss to accept useful answers ;-)

类别

Help CenterFile Exchange 中查找有关 3-D Scene Control 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by