【问题标题】:Finding the distance between two plot curves with unknown equations in MATLAB在 MATLAB 中查找具有未知方程的两条绘图曲线之间的距离
【发布时间】:2014-04-14 18:32:40
【问题描述】:

我目前正在处理一个MATLAB 项目,我需要找到膝盖两块骨头之间的距离。

在将chenvese 程序用于活动轮廓并设法绘制骨骼曲线后,我需要找到它们之间的空间。

我使用interp1 函数尝试了各种程序,但我的曲线没有相同的大小/长度。所以我试图用findobj 提取曲线方程,但它没有让我到任何地方。

%*********************** 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 是一个计算两条曲线之间距离的程序,但它不起作用并告诉我我的曲线大小不一样...

【问题讨论】:

  • 我认为interp1 是正确的 - 但使用单独的尺寸。
  • 如果你有统计工具箱,或许可以使用pdist2

标签: matlab line distance curve


【解决方案1】:

这里有一些示例代码如何使用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])

【讨论】:

  • 我正在尝试适应我的问题,但我不知道如何告诉 matlab 使用我的蓝色/红色曲线。我使用了findobj 的结果,但它给了我一个空白的结果。
  • 改用findall
【解决方案2】:

在这种情况下,您可能正在寻找线上任意两点之间的最小距离:

%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);

使用pdist2 的效果相同,但需要统计工具箱。如果可用,请使用它。它可能更快。

【讨论】:

  • 实际上,我正在寻找更多相同的蓝色/绿色曲线和每个 x 值之间的距离。我有 pdist2,我正在尝试。
猜你喜欢
  • 2021-05-16
  • 2023-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-30
  • 1970-01-01
  • 1970-01-01
  • 2014-06-23
相关资源
最近更新 更多