Image Differences - Linear Regression

>> im1 = imread('lytle03.jpg');
>> im2 = imread('lytle05.jpg');
>> g1 = rgb2gray(im1);
>> g2 = rgb2gray(im2);
>> gray1 = double(g1)/255;
>> gray2 = double(g2)/255;
>> gray1 = gray1(1:480,1:640);
>> gray2 = gray2(1:480,1:640);

Difference

Find the difference between the two grayscale images, and calculate the standard deviation of the difference.

>> diff = gray2 - gray1;
>> cendiff = 0.5*(diff+1);
>> std2(diff)

ans = 0.0439

Difference histogram

>> [counts,x] = imhist(cendiff);
>> stem(x(100:160),counts(100:160),'.-')
>> xlabel('pixel');
>> ylabel('counts');
>> [f,map] =capture;
>> imwrite(f,map,'hist1.bmp');

Linear Regression

Assume that image Y is point-by-point linearly related to image X

First we define some notation for the mean of an image:

We can use the mean to eliminate k0

The method of least-squares is used to find k1. The total least-squared deviation is:

which can be solved for k1 using:

The solution is given by

or as follows, with a change in notation:

Now we can find k0 from

Matlab code for linear regression difference

>> gm1 = mean2(gray1)

gm1 = 0.5563

>> gm2 = mean2(gray2)

gm2 = 0.5579

>> gc1 = gray1 - gm1;
>> gc2 = gray2 - gm2;
>> sxy = mean2(gc1.*gc2);
>> sxx = mean2(gc1.*gc1);
>> k1 = sxy/sxx

k1 = 0.9694

>> k0 = gm2 - k1*gm1

k0 = 0.0186

>> diff2 = gray2 - k0 - k1*gray1;
>> cd = 0.5*(diff2+1);
>> imwrite(cd,'cd.jpg');
>> std2(diff2)

ans = 0.0433

Final difference image

Final difference histogram


Maintained by John Loomis, last updated 21 May 1998