OpenCV Image Arithmetic Operations

In image processing, arithmetic operations and bitwise operations are very basic and important operations.

This article will detail how to use OpenCV for image addition, subtraction, multiplication, division, bitwise operations, and image blending.

1. Basic Operations

Image Addition

Image addition is the process of adding the corresponding pixel values of two images to generate a new image.

In OpenCV, you can use thecv2.add()function to implement image addition.

Example

import cv2
import numpy as np

# Read two images
img1 = cv2.imread('image1.jpg')
img2 = cv2.imread('image2.jpg')

# Image addition
result = cv2.add(img1, img2)

# Display the result
cv2.imshow('Result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

Note: If the pixel value exceeds 255 after addition, OpenCV will automatically truncate it to 255.

Image Subtraction

Image subtraction is the process of subtracting the corresponding pixel values of two images to generate a new image.

In OpenCV, you can use thecv2.subtract()function to implement image subtraction.

Example

# Image subtraction
result = cv2.subtract(img1, img2)

# Display the result
cv2.imshow('Result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

Note: If the pixel value is less than 0 after subtraction, OpenCV will automatically truncate it to 0.

Image Multiplication

Image multiplication is the process of multiplying the corresponding pixel values of two images to generate a new image.

In OpenCV, you can use thecv2.multiply()function to implement image multiplication.

Example

# Image multiplication
result = cv2.multiply(img1, img2)

# Display the result
cv2.imshow('Result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

Note: If the pixel value exceeds 255 after multiplication, OpenCV will automatically truncate it to 255.

Image Division

Image division is the process of dividing the corresponding pixel values of two images to generate a new image. In OpenCV, you can use thecv2.divide()function to implement image division.

Example

# Image division
result = cv2.divide(img1, img2)

# Display the result
cv2.imshow('Result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

Note: If the divisor is 0, OpenCV will automatically set the result to 0.

2. Image Bitwise Operations (AND, OR, NOT, XOR)

Bitwise operations perform binary bit operations on each pixel of the image.

OpenCV providescv2.bitwise_and()、cv2.bitwise_or()、cv2.bitwise_not()andcv2.bitwise_xor()functions to implement bitwise operations on images.

FunctionFunctionalityApplication scenarios
cv2.bitwise_and()Bitwise AND operationMasking, image segmentation
cv2.bitwise_or()Bitwise OR operationImage overlay
cv2.bitwise_not()Bitwise NOT operationImage inversion
cv2.bitwise_xor()Bitwise XOR operationImage difference detection

Bitwise AND Operation (AND)

The bitwise AND operation performs a bitwise AND on each pixel of two images.

Example

# Bitwise AND operation
result = cv2.bitwise_and(img1, img2)

# Display the result
cv2.imshow('Result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

Bitwise OR Operation (OR)

The bitwise OR operation performs a bitwise OR on each pixel of two images.

Example

# Bitwise OR operation
result = cv2.bitwise_or(img1, img2)

# Display the result
cv2.imshow('Result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

Bitwise NOT Operation (NOT)

The bitwise NOT operation performs a bitwise inversion on each pixel of an image.

Example

# Bitwise NOT operation
result = cv2.bitwise_not(img1)

# Display the result
cv2.imshow('Result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

Bitwise XOR Operation (XOR)

The bitwise XOR operation performs a bitwise XOR on each pixel of two images.

Example

# Bitwise XOR operation
result = cv2.bitwise_xor(img1, img2)

# Display the result
cv2.imshow('Result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

3. Image Blending (cv2.addWeighted())

Image blending is the process of linearly combining two images according to certain weights to generate a new image.

In OpenCV, you can use thecv2.addWeighted()function to implement image blending.

Example

# Image blending
alpha = 0.7  # Weight of the first image
beta = 0.3   # Weight of the second image
gamma = 0    # Optional scalar value

result = cv2.addWeighted(img1, alpha, img2, beta, gamma)

# Display the result
cv2.imshow('Result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

Parameter description:

  • alpha: Weight of the first image.
  • beta: Weight of the second image.
  • gamma: Optional scalar value, usually set to 0.

Formula:

result = img1 * alpha + img2 * beta + gamma

Summary

This article detailed the image arithmetic operations, bitwise operations, and image blending in OpenCV. These operations are the foundation of image processing, and mastering them is crucial for subsequent more complex image processing tasks. By practicing these operations, you can better understand the basic principles of image processing and lay a solid foundation for future computer vision tasks.

Other extensions