Equally spaced points in a contour

2019-07-04 00:47发布

问题:

I have a set of 2D points (not ordered) forming a closed contour, and I would like to resample them to 14 equally spaced points. It is a contour of a kidney on an image. Any ideas?

回答1:

One intuitive approach (IMO) is to create an independent variable for both x and y. Base it on arc length, and interpolate on it.

% close the contour, temporarily
xc = [x(:); x(1)];
yc = [y(:); y(1)];

% current spacing may not be equally spaced
dx = diff(xc);
dy = diff(yc);

% distances between consecutive coordiates
dS = sqrt(dx.^2+dy.^2);
dS = [0; dS];     % including start point

% arc length, going along (around) snake
d = cumsum(dS);  % here is your independent variable
perim = d(end);

Now you have an independent variable and you can interpolate to create N segments:

N = 14;
ds = perim / N;
dSi = ds*(0:N).'; %' your NEW independent variable, equally spaced

dSi(end) = dSi(end)-.005; % appease interp1

xi = interp1(d,xc,dSi);
yi = interp1(d,yc,dSi);

xi(end)=[]; yi(end)=[];

Try it using imfreehand:

figure, imshow('cameraman.tif');
h = imfreehand(gca);
xy = h.getPosition; x = xy(:,1); y = xy(:,2);
% run the above solution ...


回答2:

Say your contour is defined by independent vector x and dependent vector y.

You can get your resampled x vector using linspace:

new_x = linspace(min(x),max(x),14); %14 to get 14 equally spaced points

Then use interp1 to get new_y values at each new_x point:

new_y = interp1(x,y,new_x);

There are a few interpolation methods to choose from - default is linear. See interp1 help for more info.