__canny Edge detection algorithm for edge detection in OpenCV

Source: Internet
Author: User

OpenCV provides four different edge detection operators, or High-pass filters: Sobel,scharr and Laplacian operators and canny operators, the specific detection steps are as follows: image filtering : The algorithm of edge detection is based on first and second derivative of image gray value, but the derivative is usually sensitive to noise, so it is necessary to use filter to improve the performance of noise-related edge detector. Commonly used Gaussian smoothing filter. Enhanced Edge : The point of the image Gray point neighborhood value can be significantly changed. When the concrete programming is realized, it can be determined by calculating the gradient amplitude value. Image Detection : For each point in the image gradient, gradient value of the larger place is often edge. First, Sobel, ScHARR and Laplacian operators Sobel operator
Gaussian smoothing and differential operation of the binding body, so it's good anti-noise ability, at any point in the image using this operator, will produce a corresponding gradient vector or its vector. The size of the convolution core (ksize) can be set, if the ksize=−1 ksize=-1, the 3x3 3x3 ScHARR filter (thousand years spare) will be used, and its effect is better than the 3x3 3x3 Sobel filter. To find the gradient of the Sobel operator's horizontal and vertical direction is to use the corresponding convolution kernel and the original image by Pixel convolution. Laplacian operator
OpenCV operators that call the Sobel X and Y directions directly when calculating the Laplace operator ksize=1 ksize = 1 o'clock, the operator uses the default convolution kernel (3∗3 3*3) code and results display

img = cv2.imread (' d:/dave.jpg ')

# parameter 1,0 to seek only the first derivative in the x direction, the maximum can be obtained from the 2-order derivative
sobelx = cv2. Sobel (IMG,-1, 1, 0, ksize=3)
# parameter 0,1 for only the first derivative in the Y direction, the maximum can be obtained from the 2-order derivative
sobely = cv2. Sobel (IMG,-1, 0, 1, ksize=3)

Laplacian = Cv2. Laplacian (IMG,-1, ksize=3)

second, Canny edge detection steps Gaussian smoothing filter: eliminating noise to compute the gradient amplitude and direction of each pixel point in the image
For smoothed images, the gradient amplitude and direction of each pixel are computed by using the Sobel operator to compute the first-order derivative (image gradient Gx g_x and Gy g_y) based on (GX g_x and Gy g_y) in the horizontal and vertical directions:
Edge_gradient (g) =g2x+g2y−−−−−−−√edge\_gradient (g) = \sqrt{g_x^2 + g_y^2}
The direction of the Angle (theta) =tan−1 (GYGX) Angle (\theta) =tan^{-1} (\frac{g_y}{g_x}) gradient is always perpendicular to the boundary. The gradient direction is grouped into four categories: vertical, horizontal, and two diagonal. Non-maximal suppression (Non-maximum suppression)
Check each pixel to see if the gradient of this point is the largest in the point with the same gradient direction around it, and if it is reserved, it is not a maximum to suppress (if set to 0). Because the regions with higher gradient are usually wider, the pixels near the edge can be further excluded by the NMS , and only some thin lines (candidate edges) are retained.
The main purpose of the NMS algorithm in object detection is to eliminate redundant (overlapping) Windows and find the best object detection location.

A lag threshold (hysteresis thresholding) If the gradient amplitude of a pixel (a) exceeds a high threshold (maxval), the pixel is preserved as an edge pixel. If a pixel (D) gradient amplitude is less than the low threshold (minval), the pixel is excluded. If the gradient amplitude of a pixel (C) is between two thresholds, the pixel is considered an edge pixel only when it is connected to a pixel higher than the high threshold, otherwise the pixel (B) is excluded.

Canny Edge Detection in OpenCV:cv2. Canny (IMG, minval, maxval)

Import cv2
import numpy as NP
import matplotlib.pyplot as Plt

img = cv2.imread (' messi5.jpg ')
edges = Cv2. Canny (IMG, minval, maxval)

Plt.subplot (121), Plt.imshow (img, cmap= ' Gray ')
plt.title (' Original Image '), Plt.xticks ([]), Plt.yticks ([])
Plt.subplot (122), plt.imshow (edges, cmap= ' gray ')
plt.title (' Edge Image '), Plt.xticks ([]), Plt.yticks ([])

plt.show ()
Third, reference materials

1, OpenCV edge detection: Canny operator, Sobel operator, Laplace operator and ScHARR filter
2, Opencv-python Tutorials

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.