function [xi,yi] = snakeinterp(x,y,dmax,dmin) %插值函数,实现向量差 d(i,i+1)>dmax, then a new point is added between i and i+1
% d(i,i+1)<dmin, then either i or i+1 is removed
%SNAKEINTERP Interpolate the snake adaptively
% [xi,yi] = snakeinterp(x,y,dmax,dmin)
%
% dmax: the maximum distance between two snake points
% dmin: the maximum distance between two snake points
% d(i,i+1)>dmax, then a new point is added between i and i+1
% d(i,i+1)<dmin, then either i or i+1 is removed
%
% NOTE: the spacing of original curve must be close to the
% range defined by dmax and dmin. For arbitrary spacing,
% try snakeinterp1.
%
% See also SNAKEINTERP1
% there is a bug in the program for points removal
% Chenyang Xu and Jerry L. Prince, 4/1/95, 6/17/97
% Copyright (c) 1995-97 by Chenyang Xu and Jerry L. Prince
% Image Analysis and Communications Lab, Johns Hopkins University
% convert to column vector
x = x(:); y = y(:);
N = length(x);
d = abs(x([2:N 1])- x(:)) + abs(y([2:N 1])- y(:)); % x([2:N 1]表示[X的第2到第N个元素,第1个元素]
% remove the points which distance to neighbor points is shorter than dmin
IDX = (d<dmin); % 某一个元素比相邻下一个元素的差大于dmin,则此处为0
idx = find(IDX==0);
x = x(idx); % x,y 剩下那些相邻差大于dmin的元素,那些小于dmin的元素被移除了
y = y(idx);
N = length(x);
d = abs(x([2:N 1])- x(:)) + abs(y([2:N 1])- y(:));
IDX = (d>dmax);
z = snakeindex(IDX);%这个函数是生成一个向量,其大小是2N-M,N为矩阵(向量)X的所有元素个数,M为X中所有元素相邻差小于dmax的元素个
%数,这个向量Z的元素值是1:0.5:N+0.5,其中,在2i位置(i为元素相邻差小于dmax的元素在原向量X中的第i个)
p = 1:N+1;
xi = interp1(p,[x;x(1)],z'); % 一维数据插值 [x;x(1)]其中的分号应该是逗号? Z'应该改成Z ?
yi = interp1(p,[y;y(1)],z');
%得到一个插值了的向量xi和yi
% d(i,i+1)>dmax, then a new point is added between i and i+1
% d(i,i+1)<dmin, then either i or i+1 is removed
N = length(xi);
d = abs(xi([2:N 1])- xi(:)) + abs(yi([2:N 1])- yi(:));
while (max(d)>dmax),%直到生成的向量xi和yi元素之间的间隔d<dmax
IDX = (d>dmax);
z = snakeindex(IDX);
p = 1:N+1;
xi = interp1(p,[xi;xi(1)],z');
yi = interp1(p,[yi;yi(1)],z');
N = length(xi);
d = abs(xi([2:N 1])- xi(:)) + abs(yi([2:N 1])- yi(:));
end
评论0