Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

OpenCV Image Edge Detection

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:

  1. Noise reduction: Use a Gaussian filter to smooth the image to reduce the effect of noise.
  2. Compute gradients: Use the Sobel operator to compute the gradient magnitude and direction of the image.
  3. Non-maximum suppression: Along the gradient direction, keep the pixels with the local maximum gradient and suppress the others.
  4. 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.
  5. 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)

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 vertical convolution kernel:
[-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)

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

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)

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.