function [d, scl] = imdiff(a,b)
% IMDIFF
%      imdiff(a,b) displays the bipolar difference image and
%         prints the maximum difference between the images.
% d = imdiff(a,b) returns the difference image.
%      [d, maxdif] = imdiff(a,b) returns both the difference image and the
%         maximum absolute difference (scalar)
%      d = imdiff(a); returns a scaled version of the original image
%         such that the maximum value is 1 for a unipolar image or the
%         maximum deviation of midgray (0.5) is either +0.5 or -0.5.

% Author: John S. Loomis (17 March 2000)

d = im2double(a);
if nargin > 1
   d = d - im2double(b);
end
mx = max(d(:));
mn = min(d(:));
if mn >= 0 % image is unipolar
   scl = mx;
   if scl > 0 % image is not identical to zero
      d = d/mx;
   end
else
% image is bipolar
   scl = max(abs(mx),abs(mn));
   d = (d/scl+1)/2;
end
if
nargout < 2
   disp(sprintf(
'maximum difference: %g',scl))
   if nargout < 1
     imshow(d);
   
end
end


Maintained by John Loomis, last updated 17 March 2000