Mathematical Operations On Images Using OpenCV and NumPy in Python


In this article, you will learn to:

  • How to Add Two Images
  • How to Subtract Two images
  • How to Multiply Two Images

We first load and display two images. For the above operations, the images should be of the same size and type, the second image may be of scalar type.

Install and Import Necessary Libraries:

Install OpenCV using pip command.


pip install opencv-python

First, we import all necessary libraries like cv2, NumPy, and matplotlib, etc.

import cv2
import numpy as np
import matplotlib.pyplot as plt

Read/load and Display Images


import cv2

# load first image
img1 = cv2.imread('lena.png')
img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)

# load second image 
img2 = cv2.imread('OpenCV_logo.png')
img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)

# resize the images
img1 = cv2.resize(img1,(500,500))
img2 = cv2.resize(img2, (500,500))

# display first image
cv2.imshow('image',img1)
cv2.waitKey(0)
cv2.destroyAllWindows()

# display second image
cv2.imshow('image',img2)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

Lena

   opencv logo

You may be interested in the below post to learn different ways to load images.

How to Add Two Images

Using cv2.add() Method

We can use OpenCV function  cv2.add() to add two images, or simply we can use Numpy operation img1 + img2 to add the images. 
cv2.add()

img1 = cv2.imread('lena.png')
img2 = cv2.imread('OpenCV_logo_white.png')

img = cv2.add(img1, img2) # 240+22 = 262 => 255

cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

Add_two_images


Notice that cv2.add() method adds two images of same size, i.e., same height and width. So we have resized the images to make them of same size.


Using numpy operation  img1 + img2 

img1 = cv2.imread('lena.png')
img2 = cv2.imread('OpenCV_logo_white.png')

img = img1 + img2 # 240+22 = 262 % 256 => 6

cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:
numpy add two images

Note the difference between the above two resultant images.

Image Blending

If you want to add two images with different weights, use cv2.addWeighted() function
    img=αimg1+βimg2+γ
Here α and β are weights of first and second images and γ is a scalar quantity.


img1 = cv2.imread('lena.png')
img2 = cv2.imread('OpenCV_logo_white.png')

img = cv2.addWeighted(img1, 0.3, img2, 0.7, 0)

cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:
add weighted


Add Two Images Vertically and Horizontally

We use cv2.vconcat([image1, image2]) and  cv2.hconcat([image1, image2]) to add two images vertically and horizontally respectively. 


import cv2
import matplotlib.pyplot as plt
#add images virtically
img_v = cv2.vconcat([img1,img2])
#add images horizontally
img_h = cv2.hconcat([img1,img2])

fig = plt.figure(figsize=(55))
rows, columns = 12
fig.add_subplot(rows, columns, 1)
# showing vertical image
plt.imshow(img_v)
plt.title("Virtical Add")

fig.add_subplot(rows, columns, 2
# showing horizontal image
plt.imshow(img_h)
plt.title("horizontal Add")


Output:
Image addition using vconcat() and hconcat()
Vertically and Horizontally added image output

Note: Here, to display images we used plt.imshow() to show the pixel values.
We can also use np.vstack(), and np.hstack() to add two images vertically and horizontally.

import numpy as np
vertical = np.vstack((img1, img2))
horizontal = np.hstack((img1, img2))


How to Subtract Two Images



# subtract images
sub_cv = cv2.subtract(img1, img2)
sub_np = np.subtract(img1,img2)
sub_abs = cv2.absdiff(img1, img2)

# define figure to display above three images
fig = plt.figure(figsize=(55))
rows, columns = 22

# add first image to figure
fig.add_subplot(rows, columns, 1)
# showing image
plt.imshow(sub_cv)
plt.axis('off')
plt.title("cv2 image subtraction")

# add second image to figure
fig.add_subplot(rows, columns, 2
# showing image
plt.imshow(sub_np)
plt.axis('off')
plt.title("np image subtraction")

# add third image to figure
fig.add_subplot(rows, columns,   
# showing image
plt.imshow(sub_abs)
plt.axis('off')
plt.title("cv2 abs diff")

Output:
Image subtraction using cv2 and np
Image Subtraction 



How to Multiply Two Images:


#multiply images
mul_cv = cv2.multiply(img1, img2)
mul_np = np.multiply(img1, img2)
# sub_abs_np = np.abs(img1,img2)
fig = plt.figure(figsize=(55))
rows, columns = 12

fig.add_subplot(rows, columns, 1)
  
# showing image
plt.imshow(mul_cv)
plt.axis('off')
plt.title("cv2 image multiplication")

fig.add_subplot(rows, columns, 2)
  
# showing image
plt.imshow(mul_np)
plt.axis('off')
plt.title("np image multiplication")


Output:

Image multiplication using cv2 nad np
Image Multiplication


Comments