Image addition in Image Processing using OpenCV

In this article, you’ll see image addition in image processing using OpenCV. I highly recommend you get the “Computer Vision: Models, Learning, and Inference Book” to learn Computer Vision. For image addition in image processing just follow these steps:

Step 1: Install OpenCV

If OpenCV is not installed, then first install it using this code.

pip install opencv-python

Step 2: Import OpenCV

Import the OpenCV library.

import cv2

Step 3: Read the Images

Now read the images from the location.

img1 = cv2.imread("C:\\AiHints\\image1.jpg")
img2 = cv2.imread("C:\\AiHints\\image2.jpg")

Step 4: Image Addition

In this step, add the images using cv2.add() function.

add_img = cv2.add(img1,img2)

Step 5: Display the Output

cv2.imshow("Image 1", img1)
cv2.imshow("Image 2", img2)
cv2.imshow("Image Addition", add_img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

Image 1
Image 2
Image addition
Image Addition

Example: Image Addition ( Add a value in the single channel)

This code will add a value to an image but in the blue channel only.

import cv2

img1 = cv2.imread("C:\\AiHints\\cat.jpg")

add_img = cv2.add(img1,100)

cv2.imshow("Original Image", img1)
cv2.imshow("Image Addition", add_img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

Original Image
Image addition
Image Addition

Example: Add a value to all the channels

import cv2
img1 = cv2.imread("C:\\AiHints\\cat.jpg")

value = np.ones((img1.shape), dtype=np.uint8) * 100
add_img = cv2.add(img1,value)

cv2.imshow("Original Image", img1)
cv2.imshow("Image Addition", add_img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

Original Image
Image addition
Image Addition

Leave a Comment

Your email address will not be published.