Julie
Julie

Reputation: 21

Finding the distance between two plot curves with unknown equations in MATLAB

I'm currently working on a MATLAB project where I need to find the distance between the two bones of a knee.

After using the chenvese program for active contours and manage to plot the curves of the bones, I need to find the space between them.

Enter image description here

I tried various programs using the interp1 function but my curves don't have the same size/length. So I tried to extract the curve equations with findobj but it didn't get me anywhere.

%*********************** Chen Vese ***********************************
m=zeros(size(IBinaire,1),size(IBinaire,2));
m(300:900,400:1200)=1; %200,1200
seg = chenvese(IBinaire,m,500,0.2,'chan');

%*********************** Courbes de contours *************************
c = contour(seg);
s = getcontourlines(c);
plot(s(1).x,-s(1).y, 'b', s(2).x,-s(2).y,'g', s(3).x,-s(3).y,'r')

h = findobj('type', 'line', 'marker', '-and', 'b', [1 0]);
xx1 = get(h, 'XData')
yy1 = get(h, 'YData')


h = findobj('type', 'line', 'marker', '-and', 'g', [1 0]);
xx2 = get(h, 'XData')
yy2 = get(h, 'YData')

%****************Distance entre lignes ************************
z1=xx1+1i*yy1;
z2=xx2+1i*yy2;
i=interligne(z1,z2); 
plot(i)

interligne is a program to calculate the distance between two curves, but it doesn't work and tell me my curves are not the same size...

Upvotes: 2

Views: 2135

Answers (2)

Daniel
Daniel

Reputation: 36710

In this case, you are probably looking for the minimal distance between any two points on the lines:

%example data line A
A=rand(6,1)+i*rand(6,1);
%example data line B
B=rand(4,1)+i*rand(4,1);
AA=repmat(A,size(B.'));
BB=repmat(B.',size(A));
pwdist=sqrt(real(AA(:)-BB(:)).^2+imag(AA(:)-BB(:)).^2)
[d,f]=min(pwdist);
[a,b]=ind2sub(size(AA),f);

Using pdist2 does the same, but requires the statistics toolbox. If available, use it. It's probably faster.

Upvotes: 0

bdecaf
bdecaf

Reputation: 4732

Here's some sample code how to solve it with interp1:

% just make some test data
s = [
    struct('x',linspace(0,10,20)','y',1+(3-linspace(0,10,20)').^2);
    struct('x',linspace(0,10,30)','y',-(4-linspace(0,10,30)').^2);
    ];

% the logic is here
f = @(i,p) interp1(1:numel(s(i).x), [s(i).x,s(i).y],p);

d = @(X) norm(f(1,X(1))-f(2,X(2)));

X = fminsearch(d,[5,5]) % use a reasonable start point

% just some visualization
res = [f(1,X(1));f(2,X(2))]
clf
line(s(1).x,s(1).y,'Color','b')
line(s(2).x,s(2).y,'Color','g')
line(res(:,1),res(:,2),'Color','r','Marker','x')
axis([0,10,-5,5])

Upvotes: 0

Related Questions