Search code examples
matlabimage-processinginterpolationcontourpoints

Equally spaced points in a contour


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?


Solution

  • 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 ...