Subtraction of images


Mathematical operations on digital images consist of:
1. Addition
2. Subtraction
3. Multiplication
4. Division

Arithmetic subtraction:
Arithmetic subtraction can be done using the following two ways:
1. Pixel to pixel subtraction of two images
2. Subtraction of constant from an image

Pixel to pixel subtraction of two images
This is the most simple and straightforward operation applied on two input images of identical dimensions that produces an output image of dimensions identical to that of first two, in which a pixel value of the output image is the arithmetic difference of the corresponding pixels from the two input images. Mathematically, the subtraction operation of two images can be written as:
d(x,y)=f(x,y)-g(x,y)
Where
f and g are the two input images of identical dimensions and d is the resulting output image while x and y are the spatial coordinates of the corresponding input and output images.

If the pixel values in the input image are vectors (e.g. for color images) then the individual components of each of the three channels (i.e., red, green and blue) are subtracted separately from the corresponding input images to produce the output value. More often to reduce the computational time and complexity, programmers prefer to use grayscale images.

Apparently, the resulting output shall depend on the image format of the input images used. For example, if the two input grayscale images are of uint8 class, the output grayscale image will have pixel values within the dynamic range [0, 255] i.e., if the pixel subtraction of the corresponding two pixels underflows below the minimum value of the dynamic range of the image the output image will have pixel value equal to the minimum pixel value. For more information on class and their dynamic range click here.

Subtraction of constant from an image
Alternatively, another popular variant in image subtraction allows the subtraction of a constant from every pixel of the input image. Mathematically, the subtraction of a constant from an image pixel can be expressed as:
d(x,y) = f(x,y) - c
Where f is the input image, c is the constant and d is the resulting output image while x and y are the spatial coordinates of the input and output images. Subtraction of a constant from an image is often referred as brightness decrement operation. Subtraction operation is also used to find differences in images and detect motion in images.

Octave code:
1. Subtraction of two images(pixel to pixel subtraction)
clc;
clear all;
close all;
f = imread("ubuntu.jpg");
g = imread("ubuntulogo1.png");
[h1 w1 c1] = size(f);
[h2 w2 c2] = size(g);
if h1==h2 && w1==w2
  if c1 == 3
    f = rgb2gray(f);
  end
  figure, imshow(f); title("Grayscale image 1");
  if c2 == 3
    g = rgb2gray(g);
  end
  figure, imshow(g); title("Grayscale image 2");
  d = imsubtract(f,g,"uint8");
 figure, imshow(d); title("Subtraction of two images using imsubtract command");
  for x = 1 : h1
    for y = 1 : w1
      t(x,y) = f(x,y)-g(x,y);
    end
  end
  figure, imshow(t); title("Subtraction of two images using pixel to pixel subtraction");
else
  msgbox("Choose images of same dimension");
end

Output:



Input grayscale images with identical dimensions


Resulting subtraction of two images
2. Subtraction of a constant from an image
lc;
clear all;
close all;
f = imread("google.jpg");
[h1 w1 c1] = size(f);
c = 25; %% Constant to be subtacted from every pixel of input image
if c1 == 3
  f = rgb2gray(f);
end
d = imsubtract(f,25,"uint8");
for x = 1 : h1
  for y = 1 : w1
    t(x,y) = f(x,y) - 25;
  end
end
figure, imshow(f); title("Grayscale image");
figure, imshow(d); title("Subtraction of constant from an image using imsubtract command");
figure, imshow(t); title("Subtraction of a constant from an image");

Output:

Input grayscale image
Result of subtraction of a constant from an image






Comments

Popular posts from this blog

Washing Machine: An example of mechatronics system

Proximity sensors

Piezoelectric Pressure Sensor