Equally spaced points in a contour

不想你离开。 提交于 2019-12-23 12:56:13

问题


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.



来源:https://stackoverflow.com/questions/27429784/equally-spaced-points-in-a-contour

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!