Post

How to handle Data, and Images(7) Merging Image

How to merge Image using OpenCV

Lesson Notes in .ipynb file

How to handle Data, and Images(7) - Merging Image

Topics

Merging Image

There are two ways to add images

  1. cv2.add(): uses saturation arithmetic, meaning that if addition results below 0, it returns 0. If addition results greater than 255, returns 255
  2. np.add(): uses modulo arithmetic, meaning that if addition results modulo 256 (Example: addition results in 257, then it would return 257 % 256 = 1)

Because of this, when adding images, it’s much better to use cv2.add() in most cases

When using these functions, the two images must have same size

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import cv2
import matplotlib.pyplot as plt

image1 = cv2.imread('chanelsun.jpg')
image2 = cv2.imread('bga.jpg')

# checking size of the image
print(image1.shape)
print(image2.shape)

# since two images have different size, we must resize
height, width = image1.shape[0], image1.shape[1]

image2 = cv2.resize(image2, (width, height))
# after resizing
print(image2.shape)

# using cv2.add() saturation arithmetic
result = cv2.add(image1, image2)
plt.imshow(cv2.cvtColor(result, cv2.COLOR_BGR2RGB))
plt.show()

# using np.add() modulo arithmetic
result = image1 + image2
plt.imshow(cv2.cvtColor(result, cv2.COLOR_BGR2RGB))
plt.show()

Output:

1
2
3
(720, 1280, 3)
(360, 694, 3)
(720, 1280, 3)
Desktop ViewDesktop View

As you can see, cv2.add() shows a better result with sautration arithmetic.

Summary

  • cv2.add(): uses saturation arithmetic, meaning that if addition results below 0, it returns 0. If addition results greater than 255, returns 255
  • np.add(): uses modulo arithmetic, meaning that if addition results modulo 256 (Example: addition results in 257, then it would return 257 % 256 = 1)
  • The two images you’re trying to add must be in same size.
  • Due to how cv2.add() uses saturation arithmetic, it shows much better result when adding image.
This post is licensed under CC BY 4.0 by the author.