% NONMAXSUPPTS - non maximal suppression for corners
%
% Non maxima suppression and thresholding for points generated by a corner
% detector.
%
% Usage:   [r,c] = nonmaxsuppts(cim, radius, thresh, im)
%                                                    /
%                                                  optional
% Arguments:
%            cim    - corner strength image.
%            radius - radius of region considered in non-maximal
%                     suppression. Typical values to use might
%                     be 1-3 pixels.
%            thresh - threshold.
%            im     - optional image data.  If this is supplied the
%                     thresholded corners are overlayed on this
%                     image. This can be useful for parameter tuning.
% Returns:
%            r      - row coordinates of corner points.
%            c      - column coordinates of corner points.

% Peter Kovesi
% School of Computer Science & Software Engineering
% The University of Western Australia
% pk @ csse uwa edu au
% http://www.csse.uwa.edu.au/~pk
%
% September 2003

function [r,c] = nonmaxsuppts(cim, radius, thresh, im)
    
% Extract local maxima by performing a grey scale morphological
% dilation and then finding points in the corner strength image that
% match the dilated image and are also greater than the threshold.

    sze = 2*radius+1;                   % Size of mask.
    mx = ordfilt2(cim,sze^2,ones(sze)); % Grey-scale dilate.

    cim = (cim==mx)&(cim>thresh);       % Find maxima and threshold
    
    [r,c] = find(cim);                  % Find row,col coords.
    
    if nargin==4          % overlay corners on supplied image
	show(im,1), hold on
	plot(c,r,'r+'), title('corners detected');
    end

