OpenCV Basic Image Operations

This article will detail 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:

OperationFunction/MethodDescription
Access pixel valuesimage[y, x]Get or modify pixel values.
Image ROIimage[y1:y2, x1:x2]Get or modify a rectangular region in an image.
Channel splitting and mergingcv2.split() / cv2.merge()Split or merge image channels.
Image scalingcv2.resize()Resize an image.
Image rotationcv2.getRotationMatrix2D()Rotate an image.
Image translationcv2.warpAffine()Translate an image.
Image flippingcv2.flip()Flip an image.
Image additioncv2.add()Perform addition operation on two images.
Image subtractioncv2.subtract()Perform subtraction operation on two images.
Image blendingcv2.addWeighted()Perform weighted blending on two images.
Thresholdingcv2.threshold()Perform thresholding on an image.
Smoothingcv2.blur() / cv2.GaussianBlur()Smooth an image.

1. Accessing and Modifying Pixel Values

An image is a matrix composed of pixels, and each pixel has one or more values representing color or grayscale. In a grayscale image, each pixel has only one value representing the grayscale intensity; in a color image, each pixel typically has three values representing the intensities of the red, green, and blue (RGB) channels.

Accessing Pixel Values

In Python, you can use the OpenCV library to access image pixel values. Suppose we have a grayscale imageimg, you can useimg[y, x]to access the pixel value at(x, y)position. For color images, you can useimg[y, x, c]to access a specific channelcof the pixel value, wherecis 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 the 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 an ROI, we can process only a specific part of the image, thereby improving processing efficiency.

Extracting ROI

In OpenCV, you can use slicing to extract an ROI. Suppose we want to extract the 100x100 pixel region in the upper-left corner of the image:

Example

# Extract ROI
roi = img[0:100, 0:100]

Modifying ROI

After extracting an ROI, you can modify it and then place the modified ROI back into the original image.

Example

# Modify ROI
roi[:, :] = [0, 255, 0]  # Set the ROI region to green

# Place the modified ROI back into the original image
img[0:100, 0:100] = roi

3. Image Channel Splitting and Merging

Color images typically consist of multiple channels, such as the three channels of an RGB image. 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 thecv2.split()function to split the image channels.

Example

# Channel splitting
b, g, r = cv2.split(img)

Channel Merging

The separated channels can be merged back into the original image using thecv2.merge()function.

Example

# Channel merging
merged_img = cv2.merge([b, g, r])

4. Image Scaling, Rotation, Translation, and Flipping

Geometric transformation of images is a common operation in image processing, including scaling, rotation, translation, and flipping.

Image Scaling

Image scaling can be achieved using thecv2.resize()function. You can specify the target image size or scaling factor.

Example

# Image scaling
resized_img = cv2.resize(img, (200, 200))  # Scale the image to 200x200 pixels

Image Rotation

Image rotation can be achieved using thecv2.getRotationMatrix2D()andcv2.warpAffine()function. 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 using thecv2.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 and 50 pixels down
translated_img = cv2.warpAffine(img, M, (w, h))

Image Flipping

Image flipping can be achieved using thecv2.flip()function. You can specify the flip direction (horizontal, vertical, or both).

Example

# Image flipping
flipped_img = cv2.flip(img, 1)  # Flip horizontally
Other Extensions