Subtract two images Matlab

The pixel subtraction operator takes two images as input and produces as output a third image whose pixel values are simply those of the first image minus the corresponding pixel values from the second image. It is also often possible to just use a single image as input and subtract a constant value from all the pixels. Some versions of the operator will just output the absolute difference between pixel values, rather than the straightforward signed output.

How It Works :

The subtraction of two images is performed straightforwardly in a single pass. The output pixel values are given by:

Q(i,j) = P1(i,j) – P2(i,j)

Or if the operator computes absolute differences between the two input images then:

Q = |P1(i,j) – P2(i,j)|

Or if it is simply desired to subtract a constant value C from a single image then:

Q = P1(i,j) – C


A = imread('1.jpg');
B = imread('2.jpg');
other1 = imsubtract(B, A); % subtract two images

subplot(1,3,1), imshow(imadd(A,100));
subplot(1,3,2), imshow(imadd(B,100));
subplot(1,3,3), imshow(other1);