OpenCV Image Processing Basics

OpenCV provides rich image processing and computer vision functions, including image reading, display, color space conversion, filtering, edge detection, contour detection, and more.

In this chapter, we will introduce the basic concepts and common functions of OpenCV.

Image Representation and Processing

OpenCV usesNumPy arraysto represent image data. Each image is a multi-dimensional array, where each element corresponds to a pixel in the image. The size and color mode of the image can also be represented by the shape of the array.

Basic properties of an image:

  • Image size (Width, Height): can beimg.shapeobtained.
  • Color channels (Channels): usually RGB (three channels), or grayscale (single channel).
  • Data type (Data type): common ones areuint8(0-255 range), can also befloat32or other.

Reading an image:

import cv2
img = cv2.imread('image.jpg')
  • cv2.imread()Read the image file and return a NumPy array.

  • If the image path is incorrect or the file does not exist, returnsNone。

Displaying an image:

# 显示图像
cv2.imshow("Display Window", image)

# 等待按键输入
cv2.waitKey(0)

# 关闭所有窗口
cv2.destroyAllWindows()

cv2.imshow()Used to display the image,cv2.waitKey(0)wait for a key event,cv2.destroyAllWindows()close all windows.

Saving an image:

# 保存图像
cv2.imwrite("output_image.jpg", image)

cv2.imwrite()Save the image to the specified path.

Basic Image Operations

1. Accessing and modifying pixel values

# 获取像素值 (BGR 格式)
pixel_value = image[100, 100]  # 获取 (100, 100) 处的像素值

# 修改像素值
image[100, 100] = [255, 255, 255]  # 将 (100, 100) 处的像素设置为白色

2. Image ROI (Region of Interest)

# 获取 ROI
roi = image[50:150, 50:150]  # 获取 (50,50) 到 (150,150) 的区域

# 修改 ROI
image[50:150, 50:150] = [0, 255, 0]  # 将 ROI 区域设置为绿色

3. Splitting and merging image channels

# 分离通道
b, g, r = cv2.split(image)

# 合并通道
merged_image = cv2.merge([b, g, r])

4. Image scaling, rotation, translation, and flipping

# 缩放
resized_image = cv2.resize(image, (new_width, new_height))

# 旋转
rotation_matrix = cv2.getRotationMatrix2D((center_x, center_y), angle, scale)
rotated_image = cv2.warpAffine(image, rotation_matrix, (width, height))

# 平移
translation_matrix = np.float32([[1, 0, tx], [0, 1, ty]])  # tx, ty 为平移距离
translated_image = cv2.warpAffine(image, translation_matrix, (width, height))

# 翻转
flipped_image = cv2.flip(image, flip_code)  # flip_code: 0 (垂直翻转), 1 (水平翻转), -1 (双向翻转)

Image Arithmetic Operations

1. Image addition

result = cv2.add(image1, image2)

2. Image subtraction

result = cv2.subtract(image1, image2)

3. Image blending

result = cv2.addWeighted(image1, alpha, image2, beta, gamma)

alphaandbetais the weight,gammais a scalar value.

Image Thresholding

1. Simple thresholding

ret, thresholded_image = cv2.threshold(image, thresh, maxval, cv2.THRESH_BINARY)

threshis the threshold,maxvalis the maximum value.

2. Adaptive thresholding

thresholded_image = cv2.adaptiveThreshold(image, maxval, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, block_size, C)

3. Otsu's binarization

ret, thresholded_image = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

Image Smoothing

1. Mean filtering

blurred_image = cv2.blur(image, (kernel_size, kernel_size))

2. Gaussian filtering

blurred_image = cv2.GaussianBlur(image, (kernel_size, kernel_size), sigmaX)

3. Median filtering

blurred_image = cv2.medianBlur(image, kernel_size)

4. Bilateral filtering

blurred_image = cv2.bilateralFilter(image, d, sigmaColor, sigmaSpace)

Image Color Spaces and Conversion

OpenCV supports conversion between multiple color spaces, such as from RGB to grayscale, HSV, etc.

Color space conversion is very important in many image processing tasks.

Converting from RGB to grayscale:

gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

Converting from BGR to HSV:

hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

Converting from RGB to YUV:

yuv_img = cv2.cvtColor(img, cv2.COLOR_BGR2YUV)

Color space conversion is a basic operation in image processing, used for different algorithms and visual effects.

Image Resizing and Cropping

OpenCV provides multiple ways to resize images and crop image regions.

Resizing an image:

resized_img = cv2.resize(img, (width, height))

Cropping an image:

cropped_img = img[y1:y2, x1:x2]

A specific region of the image can be cropped using slicing operations.

Image Smoothing and Denoising (Blurring)

Image smoothing can reduce noise. Common blurring algorithms include Gaussian blur, mean blur, etc.

Gaussian blur:

blurred_img = cv2.GaussianBlur(img, (5, 5), 0)

Mean blur:

blurred_img = cv2.blur(img, (5, 5))

Median blur:

blurred_img = cv2.medianBlur(img, 5)

Image Edge Detection

Edge detection is a common image processing technique used to detect edges in an image. The most commonly used method is Canny edge detection.

Canny edge detection:

edges = cv2.Canny(img, 100, 200)

The Canny algorithm finds edges by computing gradients on the image and returns a binary image where edges are white and other regions are black.

Sobel operator:

sobel_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=5)
sobel_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=5)

Laplacian operator:

laplacian = cv2.Laplacian(image, cv2.CV_64F)

Morphological Operations

Morphological operations are often used in binary image processing. Common operations include erosion, dilation, opening, closing, etc.

Erosion:Shrinks the white regions in the image.

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
eroded_img = cv2.erode(img, kernel, iterations=1)

Dilation:Expands the white regions in the image.

dilated_img = cv2.dilate(img, kernel, iterations=1)

Opening and closing operations:

  • Opening (erosion followed by dilation): used to remove small objects.
  • Closing (dilation followed by erosion): used to fill small holes in the image.
opening_img = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
closing_img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)

Image Contour Detection

OpenCV provides powerful contour detection capabilities, which can be used in applications such as object recognition and image segmentation.

Detecting contours:

gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, threshold_img = cv2.threshold(gray_img, 127, 255, cv2.THRESH_BINARY)
contours, _ = cv2.findContours(threshold_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

Drawing contours:

cv2.drawContours(img, contours, -1, (0, 255, 0), 3)

Video Processing

OpenCV also supports video processing. It can read video files, capture video streams, and perform real-time processing.

Reading a video:

Examples

cap = cv2.VideoCapture('video.mp4')
while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        break
    # Process each frame
    cv2.imshow('Video', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()
Other Extensions