Record multiple points

You can use this function to record multiple points and you can easily modify it to display dots rather than lines between the points!
Example:  » pts = readPoints(image, 45)

flower_points

function pts = readPoints(image, n)
%   readPoints  Read manually-defined points from image
%   points = readPoints(image, n) reads up to n points.
%   points = readPoints(image ) reads as many points as you want!
if nargin < 2
    n = Inf;
    pts = zeros(2, 0);
else
    pts = zeros(2, n);
end
imshow(image);
xold = 0;
yold = 0;
k = 0;
hold on;           % and keep it hold for out points
while 1
    [xi, yi, but] = ginput(1);      % get a point
    if ~isequal(but, 1)             % stop if not button 1
        break
    end
    k = k + 1;
    pts(1,k) = xi;
    pts(2,k) = yi;
      if xold
          plot([xold xi], [yold yi], 'ro-');  % draw line as we go
      else
          plot(xi, yi, 'ro');         % first point on its own
      end
      if isequal(k, n)
          break
      end
      xold = xi;
      yold = yi;
  end
hold off;
if k < size(pts,2)
    pts = pts(:, 1:k);
end
end

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.