Skip to content

Beginners Opencv · Tutorials

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

Access community, courses and source codes
Logo

AI Vision Academy

Access the code of this tutorial, computer vision courses and an exclusive community on AI Vision Academy

  • Access to over 50+ source codes from Pysource.com/blog
  • Dedicated video courses about computer vision
  • Access to an exclusive community of professionals
  • Real-World AI Projects – Get hands-on experience building practical AI Computer Vision solutions with a structured path.
  • Monthly Coaching Calls – get support and any of your questions answered

Subscribe to our newsletter to learn more

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()

Join the discussion