function nout = nms(magnitude,sector)
%
% nms: non-maxima suppression (for Canny edge detector)
% On input:
%      magnitude: magnitude image  = sqrt(dx^2 + dy^2)
%      sector: sector image     = sector(orientation)
% On output:
%     nout: local max edge responses
% Call:
%  nout = nms(m,s)
%

[m,n] = size(magnitude);
nout = zeros(m,n);
for ii = 2:m-1
  for jj = 2:n-1
    val = magnitude(ii,jj);
    sec = sector(ii,jj);
    nout(ii,jj) = magnitude(ii,jj);
    if (sec == 0) & ((val<magnitude(ii,jj-1)) | (val<magnitude(ii,jj+1)))
      nout(ii,jj) = 0;
    end
    if (sec == 1) & ((val<magnitude(ii-1,jj+1)) | (val<magnitude(ii+1,jj-1)))
      nout(ii,jj) = 0;
    end
    if (sec == 2) & ((val<magnitude(ii-1,jj)) | (val<magnitude(ii+1,jj)))
      nout(ii,jj) = 0;
    end
    if (sec == 3) & ((val<magnitude(ii-1,jj-1)) | (val<magnitude(ii+1,jj+1)))
      nout(ii,jj) = 0;
    end
  end
end
