How to Subtract Two Images of Different Size in OpenCV Python?

In this post, we learn how to subtract images of different sizes using OpenCV Python. We take two images of different sizes and perform pixel wise subtraction on these images. We use OpenCV Python methods such as cv2.subtract(), etc. to perform the operation.

We will cover the contents listed in the following Table of Content.

Table of Content:


Prerequisites
  • Python 3
  • OpenCV
  • NumPy
  • Matplotlib (Optional)

STEP 1: Import Required Libraries/ Packages

First step is to import the required packages and libraries.
import cv2
import matplotlib.pyplot as plt
Using Matplotlib is optional.We used this to display the images with width and height.

STEP 2: Load the Images

We load the images "Desert.jpg" and "Penguins.jpg". You can download the images and follow the process with the same images.

We will use the following two images to perform our operation.
Desert.jpg
Desert.jpg


Pneguins.jpg
Penguins.jpg

You should change the image path accordingly.
# Load the input images 
img1 = cv2.imread('Desert.jpg')
img2 = cv2.imread('Penguins.jpg')
We have discussed different ways to load image in Python in our previous post. 

STEP 3: Create ROI

The main task is to find the ROI (Region Of Interest). Here our main aim is to find the minimum height and width from both the images. So that we can subtract the common area.  

Get height and width of images

Find the height and width of the images.
# Get height and width of first image
height1, width1 = img1.shape[0],img1.shape[1]
print(height1, width1)
Output
768  1024

# Get height and width of the second image
height2, width2 = img2.shape[0],img2.shape[1]
print(height2, width2)
Output
457  640

You can notice that the two images have different sizes (height and width).

Find minimum height and width of images

Now find the minimum height and width of the images.
# Find the minimum height and width of the two images
min_height = min(height1, height2)
min_width = min(width1, width2)
print(min_height, min_width)
Output

457  640

Crop images with minimum height and width

Crop both images with the above calculated minimum height and width. 
# Crop images with minimum height and width
img11 = img1[0:min_height, 0:min_width]
img22 = img2[0:min_height, 0:min_width]

Now the both images have the same height and width. We could now subtract these images.

Note: You can OpenCV to visualize the output images. We are using the Matplotlib to show the scale for height and width. 

STEP 4: Subtract Images

Now look at how to subtract images of different sizes. You need to follow the above step of creating ROI. Here we will create ROI for subtracting image.

Create subtraction ROI

We have already cropped the images with minimum height and width in step -Crop images with minimum height and width
Now we subtract the cropped image with min height and width.
img_sub = cv2.subtract(img11, img22)
Here we get the subtracted image with min h and w. This our ROI to subtract images with different sizes.

Finally add ROI from original Images

Finally using the indexing and slicing we add the ROI to the original images.
img1[0:min_height,0:min_width] = img_sub
Here we get the subtracted images.

Visualize the subtracted images

plt.imshow(img1)
plt.show()
Subtracted image1 from Image2
Subtracted Image

Complete Program (subtract images of different sizes)

Finally we look at the complete program to subtract the images with different sizes.
# import required libraries
import cv2
import matplotlib.pyplot as plt
# Read two images
img1 = cv2.imread('Desert.jpg')
img2 = cv2.imread('Penguins.jpg')


def subtract_images(img1, img2):
        
    height1, width1 = img1.shape[0],img1.shape[1]
height2, width2 = img2.shape[0],img2.shape[1]

min_height = min(height1, height2)
min_width = min(width1, width2)

img11 = img1[0:min_height, 0:min_width]
img22 = img2[0:min_height, 0:min_width]

img_sub = cv2.subtract(img11, img22)

img1[0:min_height,0:min_width] = img_sub

return img1


img_sub_result = subtract_images(img1, img2)


# cv2.imshow('Subtracted image',img_sub_result)
# cv2.waitKey(0)
# cv2.destroyAllWindows()


# Convert the image from BGR to RGB
img_sub_result = cv2.cvtColor(img_sub_result, cv2.COLOR_BGR2RGB)
plt.imshow(img_sub_result)
plt.show()
We have covered all the content related to subtracting images of different sizes ( different height and width). If you have any doubts or want more related to this topic please comment.

Advertisements



Comments