MATLAB Answers

How to find the arc length of a trajectory?

Latest activity Edited by Stephan
on 16 Oct 2019
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);
B1 = bwmorph(g,'endpoints');%endpoints
[ye xe] = find(B1);%Position end points

  1 Comment

on 15 Oct 2019
Can't you measure the diameter? Looks like circle

Sign in to comment.

1 Answer

Answer by Stephan
on 15 Oct 2019
Edited by Stephan
on 16 Oct 2019

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);
% Find the center and radius of the circle
[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;
% Tada: The result
arc_lengths = radius*pi*angles./180;
% Plot results
Lines = gobjects(1,numel(xe));
hold on
for k = 1:size(Pe,1)-1
Lines(k) = drawline('Position', [Pe(k,1), Pe(k,2); center],'label',string(k));
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 ;-)


Sign in to comment.