Identical points in the contour
I have a set of 2D points (unordered) forming a closed loop and I would like to count them down to 14 equally spaced points. This is the outline of the kidney in the image. Any ideas?
One intuitive approach (IMO) is to create an independent variable for x
and y
. Place it at the 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 segments N
:
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 using imfreehand
:
figure, imshow('cameraman.tif'); h = imfreehand(gca); xy = h.getPosition; x = xy(:,1); y = xy(:,2); % run the above solution ...
Let's say that your contour is defined by an independent x vector and a dependent y vector.
You can get your tried and tested vector x with linspace:
new_x = linspace(min(x),max(x),14); %14 to get 14 equally spaced points
Then use interp1 to get the values โโof new_y at each point of new_x:
new_y = interp1(x,y,new_x);
There are several interpolation methods - linear by default. See interp1 help for details.