Blogs/Image Subtraction

Image Subtraction

mesakarghm Sep 15 2021 1 min read 202 views
Computer Vision
Image Subtraction.jpeg

Image subtraction is the process where value of pixels of an image is subtracted from another image. This results in an image that generally points out the differences between the two images. 

During Image Subtraction, we can either subtract one image from another or we can also subtract a constant value from one image. Image Subtraction is generally used to 

  • Detecting changes in an image 
  • Levelling uneven sections of an image 

By using image subtraction, we can tell if something in the image has moved. Image Subtraction or Background Subtraction is one of the most widely used technique in traditional object tracking, where we subtract the current frame with moving entities from the original frame to get the blobs for moving objects. These blobs can later be used to track the objects movement using some linear filter such as Kalman Filter. 

As an example, we can subtract this image:

Image subtraction

From this image:

Image subtraction

And the result will be this image:

Image subtraction

Below is some Python Code for implementing Image Subtraction. Here, we subtract an image (numpy array with dimension a*b*c) from another image (numpy array with same dimension). 


Learn and practice this concept here:

import numpy as np
def subtract(mat1,mat2): 
    assert mat1.shape == mat2.shape
    r,g,b = mat1.shape 
    mat_sub = np.zeros((r,g,b))
    for x in range(0,r): 
        for y in range(0,g): 
            for z in range(0,b): 
                mat_sub[x][y][z] = mat1[x][y][z] - mat2[x][y][z]
                if mat_sub[x][y][z] < 0: 
                    mat_sub[x][y][z] = 0 
                elif mat_sub[x][y][z] > 255: 
                    mat_sub[x][y][z] = 255
    return mat_sub