OpenCV Image Edge Detection
Image edge detection is a fundamental task in computer vision and image processing. It is used to identify regions in an image where brightness changes significantly, and these regions usually correspond to the boundaries of objects.
are commonly used edge detection functions in OpenCV and their descriptions:
| function | Algorithm | Description | Applicable scenarios |
|---|---|---|---|
cv2.Canny() |
Canny edge detection | A multi-stage algorithm with good detection results and strong noise suppression. | General-purpose edge detection, suitable for most scenarios. |
cv2.Sobel() |
Sobel operator | Edge detection based on first-order derivatives, which can detect horizontal and vertical edges. | Detect horizontal and vertical edges. |
cv2.Scharr() |
Scharr operator | An improved version of the Sobel operator with a stronger response to edges. | Detect subtle edges. |
cv2.Laplacian() |
Laplacian operator | Edge detection based on second-order derivatives, sensitive to noise. | Detect edges and corners. |
1. Canny Edge Detection (cv2.Canny())
Canny edge detection is a multi-stage edge detection algorithm proposed by John F. Canny in 1986.
Canny edge detection is considered the "gold standard" of edge detection because it achieves a good balance between noise suppression and edge localization.
1.1 Steps of Canny Edge Detection
The Canny edge detection algorithm mainly includes the following steps:
- Noise reduction: Use a Gaussian filter to smooth the image to reduce the effect of noise.
- Compute gradients: Use the Sobel operator to compute the gradient magnitude and direction of the image.
- Non-maximum suppression: Along the gradient direction, keep the pixels with the local maximum gradient and suppress the others.
- Double thresholding: Use two thresholds (a low threshold and a high threshold) to determine true edges. Pixels above the high threshold are considered strong edges, pixels below the low threshold are suppressed, and pixels in between are kept if they are connected to strong edges.
- Edge tracking by hysteresis: Through hysteresis thresholding, connect weak edges with strong edges to form complete edges.
1.2 Implementing Canny Edge Detection with OpenCV
In OpenCV, you can use the cv2.Canny() function to implement Canny edge detection.
The prototype of this function is as follows:
edges = cv2.Canny(image, threshold1, threshold2, apertureSize=3, L2gradient=False)
image: The input image, which must be a single-channel grayscale image.threshold1: The low threshold.threshold2: The high threshold.apertureSize: The aperture size of the Sobel operator, defaulting to 3.L2gradient: Whether to use the L2 norm to compute the gradient magnitude, defaulting to False (using the L1 norm).
Example
import
cv2
import
numpy
as
np
Read an image
image
=
cv2.
imread
(
'image.jpg'
,
cv2.
IMREAD_GRAYSCALE
)
Apply Canny edge detection
edges
=
cv2.
Canny
(
image
,
100
,
200
)
Display Results
cv2.
imshow
(
'Canny Edges'
,
edges
)
cv2.
waitKey
(
0
)
cv2.
destroyAllWindows
(
)
2. Sobel Operator (cv2.Sobel())
The Sobel operator is a gradient-based edge detection operator that detects edges by computing the gradient of the image in the horizontal and vertical directions.
The Sobel operator combines Gaussian smoothing and differentiation, so it has a certain suppressive effect on noise.
2.1 Principle of the Sobel Operator
The Sobel operator uses two 3x3 convolution kernels to compute the gradient of the image in the horizontal and vertical directions, respectively:
- The horizontal convolution kernel:
[-1, 0, 1] [-2, 0, 2] [-1, 0, 1]
[-1, -2, -1] [ 0, 0, 0] [ 1, 2, 1]
Using these two convolution kernels, you can obtain the gradient of the image in the horizontal and vertical directions, respectively Gx and Gy. The final gradient magnitude can be computed using the following formula:
G = sqrt(Gx^2 + Gy^2)
2.2 Implementing the Sobel Operator with OpenCV
In OpenCV, you can use the cv2.Sobel() function to compute the gradient of an image. The prototype of this function is as follows:
dst = cv2.Sobel(src, ddepth, dx, dy, ksize=3, scale=1, delta=0, borderType=cv2.BORDER_DEFAULT)
src: The input image.ddepth: The depth of the output image, usually usingcv2.CV_64F。dx: The order of the derivative in the x direction.dy: The order of the derivative in the y direction.ksize: The size of the Sobel kernel, defaulting to 3.scale: A scaling factor, defaulting to 1.delta: An optional delta value, defaulting to 0.borderType: The border filling type, defaulting tocv2.BORDER_DEFAULT。
Example
import
cv2
import
numpy
as
np
Read an image
image
=
cv2.
imread
(
'image.jpg'
,
cv2.
IMREAD_GRAYSCALE
)
Compute the gradient in the x direction
sobel_x
=
cv2.
Sobel
(
image
,
cv2.
CV_64F
,
1
,
0
,
ksize
=
3
)
Compute the gradient in the y direction
sobel_y
=
cv2.
Sobel
(
image
,
cv2.
CV_64F
,
0
,
1
,
ksize
=
3
)
Compute the gradient magnitude
sobel_combined
=
np.
sqrt
(
sobel_x**
2
- sobel_y**
2
)
Display Results
cv2.
imshow
(
'Sobel X'
,
sobel_x
)
cv2.
imshow
(
'Sobel Y'
,
sobel_y
)
cv2.
imshow
(
'Sobel Combined'
,
sobel_combined
)
cv2.
waitKey
(
0
)
cv2.
destroyAllWindows
(
)
3. Laplacian Operator (cv2.Laplacian())
The Laplacian operator is a second-order differential operator that detects edges by computing the second derivative of an image. The Laplacian operator is sensitive to noise, so the image is usually Gaussian-smoothed before use.
3.1 Principle of the Laplacian Operator
The Laplacian operator uses the following convolution kernel to compute the second derivative of an image:
[ 0, 1, 0]
[ 1, -4, 1]
[ 0, 1, 0]
Using this convolution kernel, you can obtain the Laplacian value of the image. Regions with large Laplacian values usually correspond to the edges of the image.
3.2 Implementing the Laplacian Operator with OpenCV
In OpenCV, you can use the cv2.Laplacian() function to compute the Laplacian value of an image.
The prototype of this function is as follows:
dst = cv2.Laplacian(src, ddepth, ksize=1, scale=1, delta=0, borderType=cv2.BORDER_DEFAULT)
src: The input image.ddepth: The depth of the output image, usually usingcv2.CV_64F。ksize: The size of the Laplacian kernel, defaulting to 1.scale: A scaling factor, defaulting to 1.delta: An optional delta value, defaulting to 0.borderType: The border filling type, defaulting tocv2.BORDER_DEFAULT。
Example
import
cv2
import
numpy
as
np
Read an image
image
=
cv2.
imread
(
'image.jpg'
,
cv2.
IMREAD_GRAYSCALE
)
Apply the Laplacian operator
laplacian
=
cv2.
Laplacian
(
image
,
cv2.
CV_64F
)
Display Results
cv2.
imshow
(
'Laplacian'
,
laplacian
)
cv2.
waitKey
(
0
)
cv2.
destroyAllWindows
(
)
Comparison of Common Edge Detection Functions
The following is a comparison of commonly used edge detection functions in OpenCV:
| function | Algorithm | Advantages | Disadvantages | Applicable scenarios |
|---|---|---|---|---|
cv2.Canny() |
Canny edge detection | Strong noise suppression and good edge detection results. | Parameter tuning is relatively complex. | General-purpose edge detection, suitable for most scenarios. |
cv2.Sobel() |
Sobel operator | Simple computation, suitable for detecting horizontal and vertical edges. | Sensitive to noise, with average edge detection results. | Detect horizontal and vertical edges. |
cv2.Scharr() |
Scharr operator | Stronger response to edges, suitable for detecting subtle edges. | Sensitive to noise. | Detect subtle edges. |
cv2.Laplacian() |
Laplacian operator | Can detect edges and corners. | Very sensitive to noise. | Detect edges and corners. |