YouTip LogoYouTip

Opencv Image Arithmetic Operations

In image processing, arithmetic operations and bitwise operations are very basic and important operations. This article will introduce in detail how to use OpenCV for image addition, subtraction, multiplication, division, bitwise operations, and image blending operations. ## 1、Basic Operations ### Image Addition Image addition adds the corresponding pixel values of two images to generate 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 result cv2.imshow('Result', result) cv2.waitKey(0) cv2.destroyAllWindows() **Note**: If the pixel value sum exceeds 255, OpenCV will automatically truncate it to 255. ### Image Subtraction Image subtraction subtracts the corresponding pixel values of two images to generate 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 result cv2.imshow('Result', result) cv2.waitKey(0) cv2.destroyAllWindows() **Note**: If the pixel value difference is less than 0, OpenCV will automatically truncate it to 0. ### Image Multiplication Image multiplication multiplies the corresponding pixel values of two images to generate 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 result cv2.imshow('Result', result) cv2.waitKey(0) cv2.destroyAllWindows() **Note**: If the pixel value product exceeds 255, OpenCV will automatically truncate it to 255. ### Image Division Image division divides the corresponding pixel values of two images to generate 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 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 an image. OpenCV provides `cv2.bitwise_and()`, `cv2.bitwise_or()`, `cv2.bitwise_not()`, and `cv2.bitwise_xor()` functions to implement image bitwise operations. | Function | Function | Application Scenario | | --- | --- | --- | | `cv2.bitwise_and()` | Bitwise AND operation | Mask operation, image segmentation | | `cv2.bitwise_or()` | Bitwise OR operation | Image overlay | | `cv2.bitwise_not()` | Bitwise NOT operation | Image color inversion | | `cv2.bitwise_xor()` | Bitwise XOR operation | Image difference detection | ### Bitwise AND Operation Bitwise AND operation performs bitwise AND operation on each pixel of two images. ## Example # Bitwise AND operation result = cv2.bitwise_and(img1, img2) # Display result cv2.imshow('Result', result) cv2.waitKey(0) cv2.destroyAllWindows() ### Bitwise OR Operation Bitwise OR operation performs bitwise OR operation on each pixel of two images. ## Example # Bitwise OR operation result = cv2.bitwise_or(img1, img2) # Display result cv2.imshow('Result', result) cv2.waitKey(0) cv2.destroyAllWindows() ### Bitwise NOT Operation Bitwise NOT operation performs bitwise NOT operation on each pixel of an image. ## Example # Bitwise NOT operation result = cv2.bitwise_not(img1) # Display result cv2.imshow('Result', result) cv2.waitKey(0) cv2.destroyAllWindows() ### Bitwise XOR Operation Bitwise XOR operation performs bitwise XOR operation on each pixel of two images. ## Example # Bitwise XOR operation result = cv2.bitwise_xor(img1, img2) # Display result 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 generate a new image. In OpenCV, you can use the `cv2.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 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 introduced in detail the image arithmetic operations, bitwise operations, and image blending operations in OpenCV. These operations are the foundation of image processing, and mastering them is essential for more complex image processing tasks in the future. By practicing these operations, you can better understand the basic principles of image processing and lay a solid foundation for subsequent computer vision tasks.
← Opencv Image SmoothingOpencv Image Basic β†’