[Learning OpenCV4] Summary of OpenCV edge detection algorithm

The content shared in this article comes from the book "Learning OpenCV 4: Python-based Algorithm Combat". The content of the book is as follows:

1章 OpenCV快速入门;
第2章 图像读写模块imgcodecs;
第3章 核心库模块core;
第4章 图像处理模块imgproc(一);
第5章 图像处理模块imgproc(二);
第6章 可视化模块highgui;
第7章 视频处理模块videoio;
第8章 视频分析模块video;
第9章 照片处理模块photo;
第102D特征模块features2d;
第11章 相机标定与三维重建模块calib3d;
第12章 传统目标检测模块objdetect;
第13章 机器学习模块ml;
第14章 深度神经网络模块dnn

Welcome to the books "Deep Learning Computer Vision Combat" and "Learning OpenCV4: Python-based Algorithm Combat".

insert image description here

Edges are very important features in images and play an important role in image recognition, object detection and other fields. OpenCV encapsulates commonly used edge detection algorithms, such as Sobel, Scharr, Laplacian and Canny edge detection algorithms, this section introduces the use of these algorithms.

4.7.1 Case 44: Sobel Edge Detection

The Sobel edge detection function Sobel is provided in OpenCV. The function definition is as follows:

dst = Sobel(src, ddepth, dx, dy, dst=None, ksize=None, scale=None, delta=None, borderType=None)

The parameters are explained as follows:
src, the input image;
ddepth, the depth of the output image, if set to -1, the depth is the same as the depth of the input image;
dx, calculate the derivative in the x direction;
dy, calculate the derivative in the y direction;
dst , the output image (return value);
ksize, the size of the kernel;
scale, the magnification ratio of the gradient calculation result;
delta, the pixel value can be increased by the delta value before the image is stored, the default is 0;
borderType, the border mode, set by BorderTypes are defined (see Section 3.4.5).
In this case, the Sobel function is called for edge detection, which is divided into three steps. The first step is to calculate the edge in the x direction, the second step is to calculate the edge in the y direction, and the third step is to superimpose the edges in the x and y directions to form the edge of the image.
The input image used in this case is Figure 3.10, and the case code is as follows:

import cv2

src = cv2.imread("src.jpg")
#高斯滤波
src = cv2.GaussianBlur(src, (3, 3), 0)
#转为灰度图
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
#求x方向边缘
sobel_gradx = cv2.Sobel(gray, -1, 1, 0)
#求y方向边缘
sobel_grady = cv2.Sobel(gray, -1, 0, 1)
#边缘合并
sobel_grad = cv2.addWeighted(sobel_gradx, 0.5, sobel_grady, 0.5, 0)

#图像显示
cv2.imshow("sobel_gradx", sobel_gradx)
cv2.imshow("sobel_grady", sobel_grady)
cv2.imshow("sobel_grad", sobel_grad)
cv2.waitKey(0)
cv2.destroyAllWindows()

The result of calculating the edge in the x direction is shown in Figure 4.23.
insert image description here

Figure 4.23
The result of calculating the edge in the y direction is shown in Figure 4.24.
insert image description here

Figure 4.24
The merged edge result is shown in Figure 4.25.
insert image description here

Figure 4.25
Sobel edge detection can also calculate the edges in the x and y directions at the same time, that is, set dx and dy to 1 at the same time, the case code is as follows:

import cv2

src = cv2.imread("src.jpg")
#高斯滤波
src = cv2.GaussianBlur(src, (3, 3), 0)
#转为灰度图
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
#Sobel边缘检测,同时计算x和y方向的边缘
sobel_grad = cv2.Sobel(gray, -1, 1, 1)
cv2.imshow("sobel_grad", sobel_grad)
cv2.waitKey(0)
cv2.destroyAllWindows()

The calculation results are shown in Figure 4.26.
insert image description here

Figure 4.26

4.7.2 Case 45: Scharr Edge Detection

The edge detection of the Sobel algorithm is not very accurate. Another algorithm improved on the Sobel algorithm is the Scharr algorithm, which is more accurate than the Sobel algorithm and has the same detection speed.
The Scharr edge detection function Scharr is provided in OpenCV. The function definition is as follows:

dst = Scharr(src, ddepth, dx, dy, dst=None, scale=None, delta=None, borderType=None)

The parameters are explained as follows:
src, the input image;
ddepth, the depth of the output image, if set to -1, the depth is the same as the depth of the input image;
dx, calculate the derivative in the x direction;
dy, calculate the derivative in the y direction;
dst , the output image (return value);
scale, the magnification ratio of the gradient calculation result;
delta, the pixel value can be increased by the delta value before the image is stored, the default is 0;
borderType, the border mode, defined by BorderTypes (see 3.4.5 Festival).
The kernel size of the Scharr algorithm is 3, so compared with the Sobel function, there is no need to set ksize, and other parameters are the same. In this case, calling the Scharr function for edge detection is also divided into three steps. The first step is to calculate the edge in the x direction, the second step is to calculate the edge in the y direction, and the third step is to superimpose the edges in the x and y directions to form the edge of the image.
The case code is as follows:

import cv2

src = cv2.imread("src.jpg")
#高斯滤波
src = cv2.GaussianBlur(src, (3, 3), 0)
#转为灰度图
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)

#求x方向边缘
scharr_gradx = cv2.Scharr(gray, -1, 1, 0)
#求y方向边缘
scharr_grady = cv2.Scharr(gray, -1, 0, 1)
#边缘合并
scharr_grad = cv2.addWeighted(scharr_gradx, 0.5, scharr_grady, 0.5, 0)

#图像显示
cv2.imshow("scharr_gradx", scharr_gradx)
cv2.imshow("scharr_grady", scharr_grady)
cv2.imshow("scharr_grad", scharr_grad)
cv2.waitKey(0)
cv2.destroyAllWindows()

The result of calculating the x-direction edge is shown in Figure 4.27.
insert image description here

Figure 4.27
The result of calculating the y-direction edge is shown in Figure 4.28.
insert image description here

Figure 4.28
The result of edge merging is shown in Figure 4.29.
insert image description here

Figure 4.29

4.7.3 Case 46: Laplacian Edge Detection

Sobel edge detection calculates the first-order gradient, while Laplacian edge detection calculates the second-order gradient. OpenCV provides the function Laplacian for Laplacian edge detection. The function is defined as follows:

dst = Laplacian(src, ddepth, dst=None, ksize=None, scale=None, delta=None, borderType=None)

The parameters are explained as follows:
src, the input image;
ddepth, the depth of the output image, if set to -1, the depth is the same as the input image depth;
dst, the output image (return value);
ksize, used to calculate the second order The kernel size of the derivative filter;
scale, the magnification ratio of the gradient calculation result;
delta, the pixel value can be increased by the delta value before the image is stored, the default is 0;
borderType, the border mode, defined by BorderTypes (see Section 3.4.5 ).
The case code for Laplacian edge detection is as follows:

import cv2

src = cv2.imread("src.jpg")
#高斯滤波
src = cv2.GaussianBlur(src, (3, 3), 0)
#转为灰度图
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
#Laplacian边缘检测
laplacian_grad = cv2.Laplacian(gray, -1)
#图像显示
cv2.imshow("laplacian_grad", laplacian_grad)
cv2.waitKey(0)
cv2.destroyAllWindows()

The edge detection result is shown in Figure 4.30.
insert image description here

Figure 4.30

4.7.4 Case 47: Canny Edge Detection

The Canny edge detection algorithm is a better algorithm in the traditional image processing edge detection. The algorithm first calculates the first-order derivatives of the x and y directions, and then combines them into four directional derivatives, and the directional derivatives are (the local maximum). point) candidates to make up the edge. Two thresholds are used in the Canny algorithm. If the gradient of the pixel is greater than the larger threshold, it is accepted as a contour, and if it is smaller than the smaller value, it is discarded. If the gradient between the thresholds is connected to a pixel higher than the threshold, it is accepted, otherwise it is accepted. Discarded, the algorithm suggests a threshold between 2:1 and 3:1. The Canny edge detection function Canny is provided in OpenCV. The function is defined as follows:
edges = Canny(image, threshold1, threshold2, edges=None, apertureSize=None, L2gradient=None) The
parameters are described as follows:
image, input image;
threshold1, Threshold 1;
threshold2, threshold 2;
edges, the output edge image (return value);
apertureSize, Sobel kernel size;
L2gradient, whether to use L2 gradient.

The case code for Canny edge detection is as follows:
import cv2

src = cv2.imread("src.jpg") #Gaussian
filter
src = cv2.GaussianBlur(src, (3, 3), 0) #Convert
to grayscale
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
# Canny edge detection
canny_grad = cv2.Canny(gray, 70, 160) #The
image shows
cv2.imshow("canny_grad", canny_grad)
cv2.waitKey(0)
cv2.destroyAllWindows()
The result of edge detection is shown in Figure 4.31.
insert image description here

Figure 4.31

Guess you like

Origin blog.csdn.net/lxiao428/article/details/123023619