Reputation: 215
Given the data of the curve and the size of the image, how to calculate the length of the curve ?
curve = [9.93,4;9.87,4.10;9.80,4.20;9.74,4.30;9.68,4.40;9.63,4.50;9.59,4.60;9.55,4.70;9.53,4.80;9.51,4.90;9.50,5;9.50,5.10;9.51,5.20;9.48,5.30;9.55,5.40;9.45,5.47;9.55,5.52;9.45,5.59;9.55,5.65;9.45,5.72;9.55,5.77;9.45,5.84;9.55,5.90];
backgroud = ones(20,20);
imshow(backgroud)
hold on, plot(curve(2,:),curve(1,:),'r');
Upvotes: 0
Views: 886
Reputation: 22284
To expand on the comment, you can simply sum the lengths of the line segments that make up the curve. This could be accomplished using the following code.
curve = [9.93,4;9.87,4.10;9.80,4.20;9.74,4.30;9.68,4.40;9.63,4.50;9.59,4.60;9.55,4.70;9.53,4.80;9.51,4.90;9.50,5;9.50,5.10;9.51,5.20;9.48,5.30;9.55,5.40;9.45,5.47;9.55,5.52;9.45,5.59;9.55,5.65;9.45,5.72;9.55,5.77;9.45,5.84;9.55,5.90];
len = sum(sqrt(sum(diff(curve).^2,2)))
result
len =
2.4757
Edit: As beaker pointed out, the indices are backwards in your answer. Take a look at the values of curve_x1
, curve_x2
, etc... and you will see that they are just a single value. If you reverse the indices we get the same result. Also, as a sanity check take a look at the plot of the curve, it spans about 2 units in the first dimension and about 0.4 units in the second dimension so a number near 2.5 seems reasonable, a number like around 8 is much too large.
curve_x1 = num2cell(curve(1:end-1,1));
curve_y1 = num2cell(curve(1:end-1,2));
curve_x2 = num2cell(curve(2:end,1));
curve_y2 = num2cell(curve(2:end,2));
instance_length = cellfun(@(x1,y1,x2,y2) sqrt((x2-x1)^2+(y2-y1)^2), curve_x1,curve_y1,curve_x2,curve_y2);
distance = sum(instance_length)
Upvotes: 3