OpenCV Tutorial
Image Processing
Feature Detection and Description
Drawing Functions
Video Processing
Applications and Projects
Arithmetic operations on images using OpenCV are fundamental tasks in computer vision and image processing. These operations can be used for various purposes such as image blending, brightness adjustments, and more.
Here's a rundown of some basic arithmetic operations on images using OpenCV in Python:
Image Addition:
import cv2 import numpy as np img1 = cv2.imread('path_to_image1.jpg') img2 = cv2.imread('path_to_image2.jpg') # Ensure the images are the same size or resize as necessary # img2 = cv2.resize(img2, (img1.shape[1], img1.shape[0])) result = cv2.add(img1, img2)
Image Subtraction:
result = cv2.subtract(img1, img2)
Image Blending (weighted addition):
alpha = 0.7 # weight of the first image (img1) beta = 0.3 # weight of the second image (img2) gamma = 0 # scalar added to each sum blend = cv2.addWeighted(img1, alpha, img2, beta, gamma)
Bitwise Operations (useful for masking and ROI extraction):
result = cv2.bitwise_and(img1, img2, mask=None)
result = cv2.bitwise_or(img1, img2, mask=None)
result = cv2.bitwise_not(img1, mask=None)
result = cv2.bitwise_xor(img1, img2, mask=None)
Brightness Adjustment: Adjusting the brightness of an image can be done by adding or subtracting a scalar value.
M = np.ones(img1.shape, dtype="uint8") * 50 brighter = cv2.add(img1, M) # Increase brightness darker = cv2.subtract(img1, M) # Decrease brightness
When performing arithmetic operations on images, always be aware of the data type and its range. Most images in OpenCV are represented as uint8
with values ranging from 0 to 255. Operations can result in overflow or underflow which might lead to unintended results, so always ensure that the resultant values are clipped within the valid range.
Also, always make sure that images used in arithmetic operations have the same size (height, width) and number of channels. If not, you'll need to resize or crop the images as necessary before performing the operations.
OpenCV allows for arithmetic operations such as addition, subtraction, multiplication, and division on images, providing a flexible way to manipulate pixel values.
import cv2 import numpy as np # Load two images image1 = cv2.imread('image1.jpg') image2 = cv2.imread('image2.jpg') # Add images addition_result = cv2.add(image1, image2) # Subtract images subtraction_result = cv2.subtract(image1, image2) # Display results cv2.imshow('Addition Result', addition_result) cv2.imshow('Subtraction Result', subtraction_result) cv2.waitKey(0) cv2.destroyAllWindows()
# Multiply images multiplication_result = cv2.multiply(image1, image2) # Divide images division_result = cv2.divide(image1, image2) # Display results cv2.imshow('Multiplication Result', multiplication_result) cv2.imshow('Division Result', division_result) cv2.waitKey(0) cv2.destroyAllWindows()
Explore additional image arithmetic operations such as bitwise AND, OR, XOR, and NOT.
# Bitwise AND bitwise_and_result = cv2.bitwise_and(image1, image2) # Bitwise OR bitwise_or_result = cv2.bitwise_or(image1, image2) # Bitwise XOR bitwise_xor_result = cv2.bitwise_xor(image1, image2) # Bitwise NOT bitwise_not_result = cv2.bitwise_not(image1) # Display results cv2.imshow('Bitwise AND Result', bitwise_and_result) cv2.imshow('Bitwise OR Result', bitwise_or_result) cv2.imshow('Bitwise XOR Result', bitwise_xor_result) cv2.imshow('Bitwise NOT Result', bitwise_not_result) cv2.waitKey(0) cv2.destroyAllWindows()
A complete sample code showcasing basic image arithmetic operations.
Use arithmetic operations for image enhancement, such as adjusting brightness and contrast.
# Brightness adjustment brightness_factor = 1.5 brightness_result = cv2.addWeighted(image1, 1, np.zeros(image1.shape, image1.dtype), 0, brightness_factor) # Contrast adjustment contrast_factor = 1.5 contrast_result = cv2.addWeighted(image1, contrast_factor, np.zeros(image1.shape, image1.dtype), 0, 0) # Display results cv2.imshow('Brightness Adjustment', brightness_result) cv2.imshow('Contrast Adjustment', contrast_result) cv2.waitKey(0) cv2.destroyAllWindows()
Combine images through blending and alpha blending techniques.
# Image blending alpha = 0.5 blend_result = cv2.addWeighted(image1, alpha, image2, 1 - alpha, 0) # Display result cv2.imshow('Image Blending Result', blend_result) cv2.waitKey(0) cv2.destroyAllWindows()
Combine various arithmetic operations for advanced image processing tasks.
# Custom arithmetic operation (e.g., image1 - 0.5 * image2) custom_result = cv2.subtract(image1, cv2.multiply(image2, 0.5)) # Display result cv2.imshow('Custom Arithmetic Result', custom_result) cv2.waitKey(0) cv2.destroyAllWindows()