How to Get Started with Image Processing: A Tour of the OpenCV Library

Author: Zen and the Art of Computer Programming

1 Introduction

In the field of image processing, OpenCV (Open Source Computer Vision Library) is an open source cross-platform computer vision library. It is mainly used in image processing, machine vision and other applications. Due to its powerful functions and wide range of applications, it is widely used in many fields such as machine learning, data analysis, autonomous driving, image retrieval, motion tracking, and video analysis. Therefore, mastering OpenCV is a must-have skill. This article will introduce in a short form how to get started with image processing and use OpenCV for image processing.

2. Experimental environment preparation

1) Hardware: Laptop (Windows/Mac OS); 2) Software: a) IDE: Visual Studio Code (vscode)/PyCharm Professional Edition (paid); b) Python: Anaconda 3+ (including NumPy and Matplotlib packages) /Python 3.x (no additional libraries required).

3.Course Outline

1) Introduction to OpenCV, download and installation; 2) Loading images and displaying them; 3) Basic image operations; 4) Color space conversion and channel separation; 5) Image filtering and edge detection; 6) Morphological operations and contour extraction; 7) Template matching; 8) Introduction to deep learning and target detection algorithms; 9) Summary and suggestions.

  1. Introduction to OpenCV, download and installation OpenCV (Open Source Computer Vision Library) is an open source cross-platform computer vision library. It is mainly used for image processing, machine vision and other applications. For primary image processing tasks, you can use various function interfaces provided by OpenCV. Next, we briefly introduce OpenCV, and download and install it from the official website.

OpenCV official website: https://opencv.org/

OpenCV download address: https://github.com/opencv/opencv/releases

Installation tutorial: https://docs.opencv.org/master/d7/d9f/tutorial_linux_install.html

  1. Loading images and displaying them First, import the cv2 library and create a window. Then read the image and load the image through the imread function. The parameter specifies the image path or file name, and the second parameter specifies the reading mode. The third parameter indicates whether the image is transparent. Finally, call the imshow function to display the image in the window.
import cv2 

cv2.namedWindow('image') # 创建一个窗口

cv2.imshow("image", img) # 在窗口显示图片

cv2.waitKey(0) # 等待按键
cv2.destroyAllWindows() # 销毁所有窗口
  1. Basic image operations In OpenCV, images can be understood as multi-dimensional arrays, and the value of each pixel in the image is the element value in the matrix. You can do some basic operations on matrices, such as inversion, scaling, rotation, flipping, cropping, etc. Here, we introduce several commonly used basic image operations.

The resize() method can resize the image:

img = cv2.resize(img,(int(w/2), int(h/2))) # 调整图片尺寸为原始大小的一半

The rotate() method can rotate the image:

rows, cols, chnals = img.shape 
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), 90, 1)
dst = cv2.warpAffine(img, M, (cols, rows))

The cvtColor() method converts the color space of an image:

gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # BGR 转灰度图

The blur() method can blur the image:

kernel = np.ones((5,5),np.float32)/25
blurred_img = cv2.filter2D(img,-1,kernel) # 对原图像做模糊化
  1. Color space conversion and channel separation The basic operations of images were introduced earlier, including resizing, rotation, color space conversion, blurring, etc. There are many image processing operations in OpenCV, which are implemented by modifying the elements of the matrix. However, in some cases, we will encounter situations where the matrix cannot be directly manipulated. For example, if we want to draw a circle on the picture, we can use the cv2.circle() function. However, this function needs to know the center coordinates and radius, and this information is not reflected in the matrix. Therefore, in order to process images more intuitively, we need to understand the concepts of color space and channels.

Color space: Different color systems or devices represent pixels in different ways, resulting in color images unable to intuitively present their physical meaning. The RGB space commonly used in computers cannot fully describe color differences, so a new color space needs to be defined. , such as YUV, HSV, etc.

Channel: Each pixel in the image is composed of several components, and different channels are used to express different information. For example, the three color components of R, G, and B in the RGB space represent the intensity of the three colors of red, green, and blue, while the three color components of L, U, and V represent the brightness, hue, and saturation. Generally, pixel values ​​in RGB space are continuous, while pixel values ​​in other spaces are discrete.

OpenCV provides the cvtColor() function to convert the color space and channel separation of the image. Next, we use cvtColor() to convert the image from BGR color space to HSV color space and display its V component.

hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # 从 BGR 色彩空间转换为 HSV 色彩空间
v_channel = hsv_img[:, :, 2] # 获取 HSV 空间的 V 分量

cv2.imshow('V channel', v_channel) # 显示 V 分量
cv2.waitKey(0)
cv2.destroyAllWindows()

The second parameter of the cv2.cvtColor() function is used to specify the color space of the source image and the color space of the target image. The following two numbers represent the number of channels of the source image and the number of channels of the target image. If there is only one parameter, it defaults to RGB color space.

Channel separation: There is cvtColor() in OpenCV that can convert an image from one color space to another, but it cannot be directly used for channel separation of an image. In this case, we can only use the reshape() function in the numpy library to complete channel separation. The reshape() function can change the shape of a matrix and convert a multidimensional array into a two-dimensional array.

Below, we separate the V component from the HSV color space and display its original and separated images.

v_original = img[:,:,2].copy() # 获取原图的 V 通道
v_hsv = hsv_img[:,:,2].copy() # 获取 HSV 色彩空间的 V 通道

v_original = np.expand_dims(v_original, axis=-1) # 扩展维度使得通道数为 3
v_hsv = np.expand_dims(v_hsv, axis=-1) # 扩展维度使得通道数为 3

v_decomposed = np.concatenate([v_hsv, v_original], axis=2) # 拼接 HSV 和原图的 V 分量

v_hsv_only = v_decomposed[:,:,:1] # 只保留 HSV 中的 V 分量
v_original_only = v_decomposed[:,:,1:] # 只保留原图中的 V 分量

cv2.imshow('Original image only V', v_original_only) # 显示原图的 V 分量
cv2.imshow('HSV only V', v_hsv_only) # 显示 HSV 的 V 分量
cv2.waitKey(0)
cv2.destroyAllWindows()

Through the above examples, we can see that the color in the image is not only determined by the three channels of R, G, and B, but can also be encoded by the V, H, and S components. At the same time, through channel separation, we can distinguish certain channels in the original image from certain channels in HSV, improving the efficiency of image processing.

  1. Image Filtering and Edge Detection Image filtering is an important operation in image processing. Smoothing and blurring the image through filters can eliminate noise and isolated points, making the image clear and smooth. OpenCV provides us with various filters, allowing us to quickly achieve various effects.

Below, we use the mean filter to blur the original image and display the results.

blur_img = cv2.blur(img,(5,5)) # 用均值滤波器对原图进行模糊化

cv2.imshow('Blur image', blur_img) # 显示模糊化后的图片
cv2.waitKey(0)
cv2.destroyAllWindows()

Edge detection: In images, edges often represent the changing direction or structural information of the image, and are very important objects in image analysis and computer vision. OpenCV provides several edge detection methods, such as Sobel filter, Canny edge detection algorithm, Hough gradient method, etc.

Below, we use the Canny edge detection method to perform edge detection and display the results.

edges = cv2.Canny(img,100,200) # 使用 Canny 边缘检测方法

cv2.imshow('Edges detected', edges) # 显示检测出的边缘
cv2.waitKey(0)
cv2.destroyAllWindows()

In addition to image filtering and edge detection, OpenCV also has algorithms such as image morphology operations, template matching, deep learning, and target detection. These algorithms have different functions, but they can all help us solve different image processing problems.

Guess you like

Origin blog.csdn.net/universsky2015/article/details/133504707