(Computer Vision Course - Notes 1) Image Edge Detection

Image edge detection

1. sobel operator

The Sobel operator is an edge detection algorithm commonly used in image processing and computer vision. It is used to identify edge areas in an image, i.e. places where grayscale values ​​change drastically in the image. The Sobel operator is based on the gray gradient of the image and determines the position and direction of the edge by calculating the difference in gray value of the pixels around each pixel.

The Sobel operator is mainly composed of two 3x3 matrices, which are used to calculate the gradient of the image in the horizontal and vertical directions. These two matrices are usually called Sobel operator templates or convolution kernels. The following are examples of Sobel operator templates in horizontal and vertical directions:

Horizontal Sobel operator template (Gx):

-1  0  1
-2  0  2
-1  0  1

Vertical direction Sobel operator template (Gy):

-1 -2 -1
 0  0  0
 1  2  1

The calculation process of Sobel operator is as follows:

首先,将Sobel算子模板与图像的每个像素进行卷积操作。对于每个像素,将其与周围的8个像素进行乘法运算,然后将乘积相加得到一个结果。

对于水平方向的Sobel算子,将像素与模板进行乘法运算后相加的结果表示图像在水平方向上的梯度。

对于垂直方向的Sobel算子,将像素与模板进行乘法运算后相加的结果表示图像在垂直方向上的梯度。

在水平和垂直方向上的梯度计算完成后,可以通过以下公式来计算图像中每个像素的梯度幅值和梯度方向:

梯度幅值(G):G = sqrt(Gx^2 + Gy^2)
梯度方向(θ):θ = atan2(Gy, Gx)
最后,可以根据梯度幅值进行边缘检测。一般来说,梯度幅值越大的像素点,很可能是图像中的边缘点。

The Sobel operator is a simple and effective edge detection algorithm commonly used in image processing tasks. It can help us find edge information in images and play an important role in tasks such as object detection and image segmentation in the field of computer vision.

1. 2sobel operator (cv2 implementation)

import cv2
import numpy as np

# 读取图像
image = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)

# 计算水平方向和垂直方向上的Sobel梯度
sobel_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3)
sobel_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=3)

# 将梯度值转换为绝对值
sobel_x = np.absolute(sobel_x)
sobel_y = np.absolute(sobel_y)

# 将梯度值缩放到0-255范围内
sobel_x = np.uint8(sobel_x)
sobel_y = np.uint8(sobel_y)

# 将水平和垂直方向上的梯度值合并
sobel_combined = cv2.bitwise_or(sobel_x, sobel_y)

# 显示原始图像和Sobel边缘检测结果
cv2.imshow('Original Image', image)
cv2.imshow('Sobel X', sobel_x)
cv2.imshow('Sobel Y', sobel_y)
cv2.imshow('Sobel Combined', sobel_combined)
cv2.waitKey(0)
cv2.destroyAllWindows()

# 函数cv2.Sobel()用于计算图像的Sobel梯度。下面是该函数的参数及其含义的解释:

# image:输入图像。这应该是一个单通道灰度图像(如使用参数cv2.IMREAD_GRAYSCALE加载的图像),或者可以是多通道图像,其中仅考虑一个通道进行边缘检测。

# cv2.CV_64F:输出图像的数据类型。这里我们使用cv2.CV_64F表示64位浮点型数据,以便在计算梯度时能够保留负数值。

# 1:x方向上的差分阶数。这指定了在x方向上计算梯度时使用的差分阶数,设置为1表示使用一阶导数。

# 0:y方向上的差分阶数。这指定了在y方向上计算梯度时使用的差分阶数,设置为0表示不在y方向上计算梯度。

# ksize=3:Sobel算子的卷积核大小。它定义了在计算梯度时要使用的卷积核的大小。在这种情况下,ksize=3表示使用一个3x3的卷积核。

# 请注意,Sobel算子可以在x方向和y方向上分别计算梯度,通过使用不同的差分阶数来指定。在上述示例中,我们选择在x方向上计算一阶导数(水平方向)
# ,而在y方向上不计算梯度。这是因为Sobel算子通常用于检测图像中的水平边缘。如果您希望检测垂直边缘,可以将x和y方向的差分阶数进行交换。

2. Prewitt operator

The Prewitt operator is an edge detection algorithm commonly used in image processing and computer vision, similar to the Sobel operator. It is also used to detect edge areas in images, i.e. places where gray values ​​change drastically in the image. The Prewitt operator is based on the gray gradient of the image and determines the position and direction of the edge by calculating the difference in gray value of the pixels around each pixel.

The Prewitt operator is similar to the Sobel operator, both using two 3x3 matrices to calculate the gradient of the image. The following are examples of Prewitt operator templates for horizontal and vertical directions:

Horizontal Prewitt operator template (Gx):

-1  0  1
-1  0  1
-1  0  1

Vertical direction Prewitt operator template (Gy):

-1 -1 -1
 0  0  0
 1  1  1

The calculation process of the Prewitt operator is similar to the Sobel operator:

首先,将Prewitt算子模板与图像的每个像素进行卷积操作。对于每个像素,将其与周围的8个像素进行乘法运算,然后将乘积相加得到一个结果。

对于水平方向的Prewitt算子,将像素与模板进行乘法运算后相加的结果表示图像在水平方向上的梯度。

对于垂直方向的Prewitt算子,将像素与模板进行乘法运算后相加的结果表示图像在垂直方向上的梯度。

可以通过以下公式计算图像中每个像素的梯度幅值和梯度方向:

梯度幅值(G):G = sqrt(Gx^2 + Gy^2)
梯度方向(θ):θ = atan2(Gy, Gx)
最后,可以根据梯度幅值进行边缘检测。梯度幅值较大的像素点可能表示图像中的边缘点。

Compared with the Sobel operator, the difference between Prewitt operator and Sobel operator is small. Their templates are slightly different, so in some cases the Prewitt operator may produce slightly different edge detection results. In practical applications, you can choose to use the Sobel operator or the Prewitt operator according to the requirements of the task.

2.2 Prewitt operator (cv2 implementation)

import cv2
import numpy as np

# 读取图像
image = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)

# 构建Prewitt算子的卷积核
kernel_x = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]], dtype=np.float32)
kernel_y = np.array([[-1, -1, -1], [0, 0, 0], [1, 1, 1]], dtype=np.float32)

# 使用filter2D函数进行卷积运算
prewitt_x = cv2.filter2D(image, -1, kernel_x)
prewitt_y = cv2.filter2D(image, -1, kernel_y)

# 将梯度值转换为绝对值
prewitt_x = np.absolute(prewitt_x)
prewitt_y = np.absolute(prewitt_y)

# 将梯度值缩放到0-255范围内
prewitt_x = np.uint8(prewitt_x)
prewitt_y = np.uint8(prewitt_y)

# 将水平和垂直方向上的梯度值合并
prewitt_combined = cv2.bitwise_or(prewitt_x, prewitt_y)

# 显示原始图像和Prewitt边缘检测结果
cv2.imshow('Original Image', image)
cv2.imshow('Prewitt X', prewitt_x)
cv2.imshow('Prewitt Y', prewitt_y)
cv2.imshow('Prewitt Combined', prewitt_combined)
cv2.waitKey(0)
cv2.destroyAllWindows()

2.3 Comparison between Prewitt operator and sobel operator

Sobel operator and Prewitt operator are two commonly used edge detection operators, and they are slightly different in calculating edge gradients. The following are the main differences between the Sobel operator and the Prewitt operator:
1 Template difference:
The horizontal and vertical templates of the Sobel operator are respectively :

Gx = [[-1, 0, 1],
      [-2, 0, 2],
      [-1, 0, 1]]
      
Gy = [[-1, -2, -1],
      [ 0,  0,  0],
      [ 1,  2,  1]]

The horizontal and vertical templates of the Prewitt operator are:

Gx = [[-1, 0, 1],
      [-1, 0, 1],
      [-1, 0, 1]]
      
Gy = [[-1, -1, -1],
      [ 0,  0,  0],
      [ 1,  1,  1]]

2 Sensitivity difference:
The template of the Sobel operator has a larger weight at the center position, so it is more sensitive to the gradient response of the central pixel.
The template of the Prewitt operator has equal weight in the horizontal and vertical directions, and the gradient response to the central pixel and surrounding pixels is relatively balanced.
3 Gradient estimation:
Since the Sobel operator has a larger template weight, it is more suitable for gradient estimation in images with higher contrast at the edges.
The Prewitt operator can also provide better gradient estimation in images with weak contrast at the edges.
4 Computational efficiency:
Since there are more non-zero weights in the template of the Sobel operator, calculating the gradient of the Sobel operator may be slightly more time-consuming.
There are few non-zero weights in the template of the Prewitt operator, and the gradient of the Prewitt operator is calculated relatively quickly.

3. canny operator

The Canny operator is a commonly used edge detection algorithm proposed by John F. Canny in 1986. It is widely used in the fields of computer vision and image processing to detect edges in images. The Canny operator has high accuracy and good noise suppression capabilities in edge detection.

The edge detection process of the Canny operator includes the following steps:

1: Noise suppression: First, perform Gaussian filtering on the input image to reduce the impact of noise. Gaussian filtering smoothes the image by calculating a weighted average of the pixels in the neighborhood around each pixel.

2: Gradient calculation: Then, use the Sobel operator to calculate the gradient of the image. The Sobel operator performs convolution operations on the image in the horizontal and vertical directions to obtain the gradient intensity and gradient direction of each pixel.

3: Non-maximum suppression: Next, perform non-maximum suppression to refine the edges. For each pixel, according to its gradient direction, check whether it is the maximum value of the local gradient in that direction. If it is not a maximum value, it is suppressed.

4: Dual thresholding: Determines true edges through dual thresholding. Set two thresholds: low threshold and high threshold. According to the gradient strength of the pixel, the pixels are divided into three categories: strong edge, weak edge and non-edge. Only when the gradient strength of a pixel exceeds a high threshold is it considered a strong edge. If the gradient strength of a pixel is below a low threshold, it is considered a non-edge. Pixels located between two thresholds are considered weak edges. You can optionally perform edge joining to connect strong edges with adjacent weak edges.

5: Edge joining: The optional edge joining step can form a complete edge by joining weak edges with adjacent strong edges. This can be achieved by finding strong edges within the neighborhood of weak edges.

The output result of the Canny operator is a binary image, in which edge pixels are marked as white (255) and non-edge pixels are marked as black (0).

3.2 Non-maximum suppression

Non-Maximum Suppression (NMS) is an important step in the Canny edge detection algorithm, which is used to refine edges and retain edge details. This step only retains the maximum value of the local gradient by comparing in the gradient direction, and suppresses the non-maximum value.

The following are the detailed steps for non-maximum suppression:

Gradient calculation: First, during the edge detection process of the Canny operator, the gradient of the image is calculated by applying the Sobel operator. This will get the gradient strength (Gradient Magnitude) and gradient direction (Gradient Direction) of each pixel.

Gradient direction quantization: Quantizes the gradient direction into one of four main directions: 0° (vertical), 45° (diagonal), 90° (horizontal), and 135° (diagonal). This divides the gradient direction into four discrete directions.

Non-maximum suppression: For each pixel, compare its gradient strength in both positive and negative directions along the gradient direction. If the gradient strength of the pixel is greater than the gradient strength of the pixels on both sides (in the gradient direction), the pixel is retained. Otherwise, suppress it as a non-edge point.

Specific steps are as follows:

For each pixel point P, find its two adjacent pixel points (in the gradient direction), that is, the positive direction pixel point and the negative direction pixel point.
Check whether the gradient intensity of pixel point P is greater than the gradient intensity of these two adjacent pixels. If so, retain the pixel point P as an edge point. Otherwise, the pixel point P is suppressed as a non-edge point.
This process will refine the edges, retain the maximum value of the local gradient, and remove non-edge pixels that do not meet the maximum value conditions.

The result of non-maximum suppression is a binary image in which only edge pixels are marked white (255) and non-edge pixels are marked black (0).

By applying non-maximum suppression, the Canny operator can refine the edge in the gradient direction, making it thinner and retaining the detail information of the edge. This step is crucial to improve the accuracy of edge detection and preserve important edge features.

3.3canny operator cv code

import cv2

# 读取图像
image = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)

# 高斯滤波
blurred = cv2.GaussianBlur(image, (5, 5), 0)

# 计算梯度
gradient = cv2.Canny(blurred, 50, 150)  # 低阈值和高阈值

# 显示结果
cv2.imshow('Original Image', image)
cv2.imshow('Canny Edge Detection', gradient)
cv2.waitKey(0)
cv2.destroyAllWindows()

使用cv2.imread()函数读取图像,并将其转换为灰度图像(使用cv2.IMREAD_GRAYSCALE参数)。

对图像进行高斯滤波,通过cv2.GaussianBlur()函数实现。这一步骤可以减少噪声对边缘检测的影响。函数的第二个参数是滤波器的大小,这里设置为(5, 5),第三个参数是高斯核的标准差,设置为0表示根据滤波器大小自动计算。

使用cv2.Canny()函数进行Canny边缘检测。函数的第二个和第三个参数是低阈值和高阈值,根据图像的特性可以调整这两个阈值。一般来说,低阈值用于边缘连接,高阈值用于边缘起始。

最后,使用cv2.imshow()函数显示原始图像和Canny边缘检测结果。

注意:在运行代码之前,需要将image.jpg替换为实际的图像文件路径。

4 Roberts operator

Roberts operator is a classic edge detection operator used to detect edges in images. It was proposed by Lawrence Roberts in 1963. The Roberts operator is based on the concept of discrete differential and determines the existence of an edge by calculating the difference between a pixel and its neighbor pixels.

The Roberts operator uses two 2×2 convolution kernels to calculate the horizontal and vertical gradients of the image. The two convolution kernels are as follows:

Horizontal gradient convolution kernel (Gx):

 1  0
 0 -1

Vertical gradient convolution kernel (Gy):

 0  1
-1  0

The edge detection steps of Roberts operator are as follows:

Convert the input image to grayscale if it is not already grayscale.

The grayscale image is convolved using a horizontal gradient convolution kernel (Gx) and a vertical gradient convolution kernel (Gy) to obtain a horizontal gradient image and a vertical gradient image.

Calculate edge intensity of image. The edge intensity of each pixel can be calculated using the following formula:

edge_strength = sqrt(Gx^2 + Gy^2)

Among them, Gx and Gy are the pixel values ​​in the horizontal gradient image and vertical gradient image respectively.

Optional threshold processing: According to the set threshold, the edge intensity image is thresholded, pixels higher than the threshold are marked as edge points, and pixels lower than the threshold are marked as non-edge points.

The output of the Roberts operator is a binary image, in which edge pixels are marked as white (255) and non-edge pixels are marked as black (0).

Although the Roberts operator is a basic operator for edge detection, it is still used in some scenarios due to its simplicity and computational efficiency. However, it is sensitive to noise and may not be as good as other more advanced operators (such as Sobel, Prewitt, Canny, etc.) in terms of edge detection accuracy and continuity.

Guess you like

Origin blog.csdn.net/X131644/article/details/130814877