Add images and Threshold – OpenCV 3.4 with python 3 Tutorial 5

We’re going to learn in this tutorial how to add two images using Python and Opencv.

First let’s take two images. There is one condition, the images need to have the exact same size.

Image 1
Image 2

Add images

There are two different functions to add the images together.
The function cv2.add (see line below) that adds rispectively the pixels of the first image with the one of the second.

sum = cv2.add(img1, img2)

This method is not the right option when we want to add two images, because it will simply add the values together and when they are 255 or over it simply gets white. See the example below.

Result of applying cv2.add()

Instead this method works fine in other type of operations. And we will see that in the next tutorials.

Add weighted images

The second method instead of adding the pixels right away takes also into account the weight we want to assign to each image.

We can assign the weight from 0 to 1.
For example if we want to highlight the car and show a soft background, we could assign 0.7 to the car and 0.3 to the background.

weighted = cv2.addWeighted(img1, 0.3, img2, 0.7, 0)
Result of cv2.weighted()