How to use Python to perform non-maximum suppression on images
Non-maximum suppression is a commonly used image processing technology in computer vision. Used to extract edges or corners in images. In this article, we will use the Python programming language along with the OpenCV library to implement non-maximal suppression of images.
First, make sure you have installed the Python and OpenCV libraries. You can use pip to install the OpenCV library: pip install opencv-python
.
Then, import the required libraries:
import cv2 import numpy as np
Using OpenCV’s cv2.imread()
The function loads the image and converts the image to grayscale using the grayscale image processing method. Grayscale images contain only one channel and are easier to process. The following code demonstrates how to load and preprocess an image:
# 读取图像 image = cv2.imread('image.jpg') # 转换为灰度图像 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
Non-maximal suppression is based on the image gradient and uses the magnitude and direction of the gradient to determine whether it is a maximum value. We can use the cv2.Sobel()
function to calculate the gradient of the image.
# 计算x和y轴方向的梯度 gradient_x = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3) gradient_y = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3) # 计算梯度的大小和方向 magnitude = np.sqrt(gradient_x ** 2 + gradient_y ** 2) angle = np.arctan2(gradient_y, gradient_x)
Next, we will use the magnitude and direction of the gradient to perform non-maximum suppression. For each pixel, we will check its two adjacent pixels, and if the magnitude of the gradient is larger than the adjacent pixels and is a maximum value in the gradient direction, then retain the pixel as an edge.
# 非极大抑制 suppressed = np.zeros_like(magnitude) for y in range(1, magnitude.shape[0] - 1): for x in range(1, magnitude.shape[1] - 1): current_gradient = magnitude[y, x] current_angle = angle[y, x] if (current_angle >= 0 and current_angle < np.pi / 8) or (current_angle >= 7 * np.pi / 8 and current_angle < np.pi): before_gradient = magnitude[y, x - 1] after_gradient = magnitude[y, x + 1] elif current_angle >= np.pi / 8 and current_angle < 3 * np.pi / 8: before_gradient = magnitude[y - 1, x - 1] after_gradient = magnitude[y + 1, x + 1] elif current_angle >= 3 * np.pi / 8 and current_angle < 5 * np.pi / 8: before_gradient = magnitude[y - 1, x] after_gradient = magnitude[y + 1, x] else: before_gradient = magnitude[y - 1, x + 1] after_gradient = magnitude[y + 1, x - 1] if current_gradient >= before_gradient and current_gradient >= after_gradient: suppressed[y, x] = current_gradient
Finally, we use the cv2.imshow()
function to display the original image and the non-maximum suppression results. The code is as follows:
# 显示结果 cv2.imshow('Original Image', image) cv2.imshow('Non-maximum Suppressed Image', suppressed) cv2.waitKey(0) cv2.destroyAllWindows()
The above is the complete sample code for non-maximum suppression of images using Python. With the above steps, we can easily use Python and OpenCV libraries to implement non-maximum suppression to extract edges or corners in images. Parameters and code logic can be adjusted as needed to achieve better results.
The above is the detailed content of How to perform non-maximal suppression of images using Python. For more information, please follow other related articles on the PHP Chinese website!