OpenCV Image Arithmetic Operations
In image processing, arithmetic and bitwise operations are very basic and important operations.
This article will introduce in detail how to use OpenCV to perform image addition, subtraction, multiplication, division, bitwise operations, and image blending.
1. Basic Operations
Image Addition
Image addition adds the corresponding pixel values of two images to produce a new image.
In OpenCV, you can use the cv2.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 Results
cv2.
imshow
(
'Result'
,
result
)
cv2.
waitKey
(
0
)
cv2.
destroyAllWindows
(
)
Note: If the sum of pixel values exceeds 255, OpenCV automatically truncates it to 255.
Image Subtraction
Image subtraction subtracts the corresponding pixel values of two images to produce a new image.
In OpenCV, you can use the cv2.subtract() function to implement image subtraction.
Example
Image Subtraction
result
=
cv2.
subtract
(
img1
,
img2
)
Display Results
cv2.
imshow
(
'Result'
,
result
)
cv2.
waitKey
(
0
)
cv2.
destroyAllWindows
(
)
Note: If the difference of pixel values is less than 0, OpenCV automatically truncates it to 0.
Image Multiplication
Image multiplication multiplies the corresponding pixel values of two images to produce a new image.
In OpenCV, you can use the cv2.multiply() function to implement image multiplication.
Example
Image Multiplication
result
=
cv2.
multiply
(
img1
,
img2
)
Display Results
cv2.
imshow
(
'Result'
,
result
)
cv2.
waitKey
(
0
)
cv2.
destroyAllWindows
(
)
Note: If the product of pixel values exceeds 255, OpenCV automatically truncates it to 255.
Image Division
Image division divides the corresponding pixel values of two images to produce a new image. In OpenCV, you can use the cv2.divide() function to implement image division.
Example
Image Division
result
=
cv2.
divide
(
img1
,
img2
)
Display Results
cv2.
imshow
(
'Result'
,
result
)
cv2.
waitKey
(
0
)
cv2.
destroyAllWindows
(
)
Note: If the divisor is 0, OpenCV automatically sets the result to 0.
2. Image Bitwise Operations (AND, OR, NOT, XOR)
Bitwise operations perform binary bit operations on each pixel of an image.
OpenCV provides the cv2.bitwise_and()、cv2.bitwise_or()、cv2.bitwise_not() and cv2.bitwise_xor() function to implement image bitwise operations.
| function | Function | Application scenarios |
|---|---|---|
cv2.bitwise_and() |
Bitwise AND operation | Mask operations, image segmentation |
cv2.bitwise_or() |
Bitwise OR operation | Image overlay |
cv2.bitwise_not() |
Bitwise NOT operation | Image inversion |
cv2.bitwise_xor() |
Bitwise XOR operation | Image 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 Results
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 Results
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 Results
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 Results
cv2.
imshow
(
'Result'
,
result
)
cv2.
waitKey
(
0
)
cv2.
destroyAllWindows
(
)
3. Image Blending (cv2.addWeighted())
Image blending linearly combines two images according to certain weights to produce a new image.
In OpenCV, you can use the cv2.addWeighted() function to implement image blending.
Example
Image Blending
alpha
=
0.7
The weight of the first image
beta
=
0.3
The weight of the second image
gamma
=
0
An optional scalar value
result
=
cv2.
addWeighted
(
img1
,
alpha
,
img2
,
beta
,
gamma
)
Display Results
cv2.
imshow
(
'Result'
,
result
)
cv2.
waitKey
(
0
)
cv2.
destroyAllWindows
(
)
Parameter Description:
alpha: The weight of the first image.beta: The weight of the second image.gamma: An optional scalar value, usually set to 0.
Formula:
result = img1 * alpha + img2 * beta + gamma
Summary
This article introduced in detail image arithmetic operations, bitwise operations, and image blending in OpenCV. These operations are the foundation of image processing, and mastering them is essential for more complex image processing tasks later. By practicing these operations, you can better understand the basic principles of image processing and lay a solid foundation for subsequent computer vision tasks.