Opencv Image Operator
This article will provide a detailed introduction to four basic image operations: accessing and modifying pixel values, image ROI (Region of Interest) operations, image channel splitting and merging, and image scaling, rotation, translation, and flipping.
Common Methods:
| Operation | Function/Method | Description |
| --- | --- | --- |
| Access Pixel Value | `image[y, x]` | Get or modify pixel value. |
| Image ROI | `image[y1:y2, x1:x2]` | Get or modify a rectangular region in the image. |
| Channel Splitting and Merging | `cv2.split()` / `cv2.merge()` | Split or merge image channels. |
| Image Scaling | `cv2.resize()` | Adjust image size. |
| Image Rotation | `cv2.getRotationMatrix2D()` | Rotate image. |
| Image Translation | `cv2.warpAffine()` | Translate image. |
| Image Flipping | `cv2.flip()` | Flip image. |
| Image Addition | `cv2.add()` | Perform addition operation on two images. |
| Image Subtraction | `cv2.subtract()` | Perform subtraction operation on two images. |
| Image Blending | `cv2.addWeighted()` | Perform weighted blending on two images. |
| Threshold Processing | `cv2.threshold()` | Perform threshold processing on image. |
| Smoothing Processing | `cv2.blur()` / `cv2.GaussianBlur()` | Perform smoothing processing on image. |
## 1. Accessing and Modifying Pixel Values
An image is a matrix composed of pixels, where each pixel has one or more values representing color or grayscale. In grayscale images, each pixel has only one value representing the grayscale intensity; in color images, each pixel typically has three values representing the intensity of the red, green, and blue (RGB) channels respectively.
### Accessing Pixel Values
In Python, you can use the OpenCV library to access pixel values of an image. Assuming we have a grayscale image `img`, you can access the pixel value at position `(x, y)` via `img[y, x]`. For color images, you can access the pixel value of a specific channel `c` via `img[y, x, c]`, where `c` is 0 (blue), 1 (green), or 2 (red).
## Example
import cv2
# Read image
img = cv2.imread('image.jpg')
# Access pixel value
pixel_value = img[100,150]# Access pixel value at position (150, 100)
print(pixel_value)
### Modifying Pixel Values
Modifying pixel values is equally simple - just assign a new value to the corresponding pixel position.
## Example
# Modify pixel value
img[100,150]=[255,255,255]# Set pixel value at position (150, 100) to white
## 2. Image ROI (Region of Interest) Operations
ROI refers to the region of interest in an image. By extracting the ROI, we can process only specific parts of the image, thereby improving processing efficiency.
### Extracting ROI
In OpenCV, you can extract ROI through slice operations. Assuming we want to extract a 100x100 pixel region from the top-left corner of the image:
## Example
# Extract ROI
roi = img[0:100,0:100]
### Modifying ROI
After extracting the ROI, you can modify it and then place the modified ROI back into the original image.
## Example
# Modify ROI
roi[:, :]=[0,255,0]# Set ROI region to green
# Place modified ROI back into original image
img[0:100,0:100]= roi
## 3. Image Channel Splitting and Merging
Color images are typically composed of multiple channels, such as the three channels of RGB images. Sometimes we need to split these channels for separate processing and then merge them back into the original image.
### Channel Splitting
In OpenCV, you can use the `cv2.split()` function to split the channels of an image.
## Example
# Channel splitting
b, g, r = cv2.split(img)
### Channel Merging
The split channels can be merged back into the original image using the `cv2.merge()` function.
## Example
# Channel merging
merged_img = cv2.merge([b, g, r])
## 4. Image Scaling, Rotation, Translation, and Flipping
Geometric transformations of images are common operations in image processing, including scaling, rotation, translation, and flipping.
### Image Scaling
Image scaling can be achieved through the `cv2.resize()` function. You can specify the target image size or scaling ratio.
## Example
# Image scaling
resized_img = cv2.resize(img,(200,200))# Scale image to 200x200 pixels
### Image Rotation
Image rotation can be achieved through the `cv2.getRotationMatrix2D()` and `cv2.warpAffine()` functions. You need to specify the rotation center and rotation angle.
## Example
# Image rotation
(h, w)= img.shape[:2]
center =(w // 2, h // 2)
M = cv2.getRotationMatrix2D(center,45,1.0)# Rotate 45 degrees
rotated_img = cv2.warpAffine(img, M,(w, h))
### Image Translation
Image translation can be achieved through the `cv2.warpAffine()` function. You need to specify the translation matrix.
## Example
# Image translation
M = np.float32([[1,0,100],[0,1,50]])# Translate 100 pixels to the right, 50 pixels down
translated_img = cv2.warpAffine(img, M,(w, h))
### Image Flipping
Image flipping can be achieved through the `cv2.flip()` function. You can specify the flip direction (horizontal, vertical, or both).
## Example
# Image flipping
flipped_img = cv2.flip(img,1)# Horizontal flip
YouTip