Addition and Subtraction of Images using OpenCV
Last Updated :
11 Aug, 2025
Arithmetic operations like addition and subtraction can be performed on images because digital images are stored as numerical arrays. For RGB images, these are 3-dimensional arrays (height, width, color channels), while grayscale images are represented as 2-dimensional arrays.
Step-by-Step Implementation
Let's see the step by step implementation of Addition and Subtraction Arithmetic Operations on images.
Step 1: Import Libraries
Let's import the required libraries,
- cv2: Used for image processing tasks like reading, resizing and performing arithmetic.
- numpy: Used for numerical operations and handling the underlying array structure of images.
- matplotlib.pyplot: Used for plotting and displaying the images in the output cells.
Step 2: Upload the Images
Let's upload the images on which we want to perform the tasks.
- uploaded = files.upload(): This command opens the file upload dialog. It returns a dictionary where the keys are the filenames of the uploaded files.
- first_image_path = list(uploaded.keys()): This line takes the dictionary of uploaded files, gets a list of its keys (the filenames) and selects the first one.
Python
print("Upload the first image (background):")
uploaded = files.upload()
first_image_path = list(uploaded.keys())[0]
print("\nUpload the second image (overlay):")
uploaded = files.upload()
second_image_path = list(uploaded.keys())[0]
Step 3: Prepare the Images for Operations
Now we prepare our both the images for the arithmetic operations,
Python
first_img = cv2.imread(first_image_path)
second_img = cv2.imread(second_image_path)
height = first_img.shape[0]
aspect_ratio = second_img.shape[1] / second_img.shape[0]
new_width = int(height * aspect_ratio)
resized_second_img = cv2.resize(second_img, (new_width, height))
separator = np.zeros((height, 5, 3), dtype=np.uint8)
combined_inputs = np.hstack([first_img, separator, resized_second_img])
combined_inputs_rgb = cv2.cvtColor(combined_inputs, cv2.COLOR_BGR2RGB)
print("\n--- Combined Input Images ---")
plt.figure(figsize=(12, 6))
plt.imshow(combined_inputs_rgb)
plt.title('Input Images')
plt.axis('off')
plt.show()
Output:
Input ImagesStep 4: Perform the Arithmetic Operations
4.1 Image Addition: We blends the two images together using weighted addition.
Python
dim = (first_img.shape[1], first_img.shape[0])
resized_second_img_for_blending = cv2.resize(
second_img, dim, interpolation=cv2.INTER_AREA)
blended_img = cv2.addWeighted(
first_img, 0.7, resized_second_img_for_blending, 0.3, 0)
blended_img_rgb = cv2.cvtColor(blended_img, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(8, 8))
plt.imshow(blended_img_rgb)
plt.title('Blended Image Result')
plt.axis('off')
plt.show()
Output:
After Addition4.2 Image Subtraction: We subtract the pixel values of the second image from the first.
Python
dim = (first_img.shape[1], first_img.shape[0])
resized_second_img_for_subtraction = cv2.resize(
second_img, dim, interpolation=cv2.INTER_AREA)
subtracted_img = cv2.subtract(first_img, resized_second_img_for_subtraction)
subtracted_img_rgb = cv2.cvtColor(subtracted_img, cv2.COLOR_BGR2RGB)
print("\n--- Final Subtracted Image ---")
plt.figure(figsize=(8, 8))
plt.imshow(subtracted_img_rgb)
plt.title('Subtracted Image Result')
plt.axis('off')
plt.show()
Output:
After Subtraction
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice