Difference Image

20,777 bytes10,816 bytes

The above images differ by the quality of the JPEG compression.

» im1 = imread('fashion.jpg');
» im2 = imread('fashion2.jpg');
» im1 = im2double(im1);
» im2 = im2double(im2);
» im1g = rgb2gray(im1);
» im2g = rgb2gray(im2);
» imwrite(im1g,'im1g.jpg');
» imwrite(im2g,'im2g.jpg');

» diff = im1g - im2g;
» std2(diff)

ans =

    0.0256

» mean2(diff.*diff)

ans =

  6.5492e-004

» sqrt(ans)

ans =

    0.0256

» mean2(diff)

ans =

  2.1021e-004

» diff1 = 0.5*diff+0.5;
» imshow(diff1);
» imwrite(diff1,'diff1.jpg');

» imhist(diff1);

» cmax = max(max(diff))

cmax =

    0.2135

» cmin = min(min(diff))

cmin =

   -0.1777

» diff2 = 0.5*diff/max(cmax,-cmin)+0.5;
» imwrite(diff2,'diff2.jpg');

» imhist(diff2);

» diff3 = (diff-cmin)/(cmax-cmin);
» imwrite(diff3,'diff3.jpg');

» imhist(diff3)

Root-Mean-Square (RMS) Difference

 
» mse = mean2(diff.*diff)

mse =

  6.5492e-004

» rmse = sqrt(mse)

rmse =

    0.0256

Image statistics

» imstats(diff)
Image has 51 of 256 possible levels

    zmax	    zmin	   range	  contrast
  0.2135	 -0.1777	  0.3911	     1.832

    mean	     std	  skewness	  kurtosis
0.0002102	 0.02559	   0.05473	       5.1

The skewness and kurtosis are not affected by a change in scale.

» imstats(diff2)
Image has 203 of 256 possible levels

    zmax	    zmin	   range	  contrast
       1	 0.08383	  0.9162	    0.9162

    mean	     std	  skewness	  kurtosis
  0.5005	 0.05994	   0.05473	       5.1

function imstats(im)
%
%  Calculate image statistics
%

[counts, x] = imhist(im);
N = sum(counts);

f = find(counts);
n = length(f);
nlevels = length(x);
if (n <nlevels)
   disp(sprintf('Image has %d of %d possible levels\n',n, nlevels));
end

z = im2double(im);
% contrast
zmax = max(max(z));
zmin = min(min(z));
range = zmax-zmin;
modulation = range/max(abs([zmin zmax]));
disp(sprintf('    zmax\t    zmin\t   range\t  contrast'));
disp(sprintf('%8.4g\t%8.4g\t%8.4g\t%10.4g\n',zmax,zmin,range,modulation));
   

% moments
avg = mean2(z);
z = z - avg;
sig = sqrt(mean2(z.*z));
z = z/sig;
skewness = mean2(z.^3);
kurtosis = mean2(z.^4)-3;

disp(sprintf('    mean\t     std\t  skewness\t  kurtosis'));
disp(sprintf('%8.4g\t%8.4g\t%10.4g\t%10.4g',avg,sig,skewness,kurtosis));


Maintained by John Loomis, last updated 15 Jan 2001