Nav LOGO digital image – Edge detection principle – Sobel, Laplace, Canny operator 96 ck2016 2017.02.14 19:14* On the right is the image after edge detection, which is to detect the edge information on the image. On the right, the degree of white indicates the depth of the edge.
The sobel.png edge is simply a collection of points on an image that change gray quickly. How do you figure out these points that are changing rapidly?
1. Derivative, the slope of a point on a continuous function. The larger the derivative is, the greater the rate of change is, the more “edge” the place with the higher rate of change is.
2. Differential: x changes dx on the continuous function, resulting in y changes dy. The larger the dy value is, the greater the change is. Differential and derivative relations: dy = f ‘(x) dx
Here’s an example:
In a continuous function, it’s called a differential, because the picture is discrete, it’s called a difference, which is the same thing as a differential, which is the rate of change. The definition of difference, f prime of x, is equal to f of x plus 1 minus f of x, minus the first term. Arrange in sequence -f(x) + f(x + 1) Put forward the coefficient [-1, 1] as the filtering template, and perform convolution operation with the original picture F (x) to detect the edge
Why do templates have odd numbers? Because if the template is even, the result of the convolution should be in the middle, not easy to represent. Such as: The value of the image [10, 20, 30] convolved with [-1, 1] should be placed in the middle of the image 10 and 20, that is, in the middle of the position 0 and 1, that is, position 0.5. However, the image is discrete, there is no place in the middle, so it can only be placed at position 0, that is, position 10. In order to facilitate processing, the filter template is generally an odd number, 3, 5 and 7.
Sobel edge detection operator so use F ‘(x) = F (x + 1) -f (x-1) approximate calculation of the first order difference.
Sorted: [f (x – 1), 1 * * 0 f (x), 1 * f (x + 1)] coefficient is put forward: [- 1, 0, 1] so template [1, 1] was converted into [- 1, 0, 1] is a two-dimensional case
-1, 0, 1-1, 0, 1-1, 0, 1 This is the Prewitt edge detection operator.
1 f (x, y – 1), f (x, y), f (x + 1, 1) y f (1 x, y), f (x, y), f (x + 1, y) f (x – 1, y + 1), f (x, y + 1), f (x + 1, y + 1) center point (x, f Y) is the key consideration, it should be more weight, so improve the following
-1, 0, 1-2, 0, 2-1, 0, 1 this is the Sobel edge detection operator, off-x. (Similar to the partial derivative of a function of two variables, partial x, partial y)
Minus 1, minus 2, minus 1 0, 0, 1, 2, 1 is the Sobel operator in the y-direction.
Laplace edge detection operator is used to calculate the edge by second order difference. In the case of continuous function, the maximum or minimum value in the first order differential graph is considered as the edge. The point crossing zero between a maximum and a minimum in a second-order differential graph is considered an edge.
Laplacian derived: first order difference: ‘f (x) = f (x) – f (x – 1) second order difference:’ f (x) = (f (x + 1) – f (x)) – (f (x) – f (x – 1)) after the reduction: ‘f (x) = f (x – 1) – 2 f (x)) + f (x + 1) extraction factor: in front of [1, 2, 1]
In two-dimensional case, f ‘(x, y) = -4 f(x, y) + f(x+1, y) + f(x, y-1) + f(x, y-1) + f(x, y+1) extract each coefficient and write it in template form
0, 1, 0 1, minus 4, 1 0, 1, 0 let’s take two diagonal cases
1, 1, 1, 1, minus 8, 1, 1, 1, 1 that’s the Laplace operator, convolved with the original graph to find the edge.
Canny edge detection operator Canny calculation process 1. Gaussian filter smoothing image. 2. Calculate the gradient and direction of the first-order difference partial derivative. 3. Suppress where the gradient value is not the maximum value. 4. Connect the connection points on the graph using double thresholds.
Generally speaking, 1. Gaussian filtering is mainly used to remove the noise on the image. 2. Calculate the first difference, OpenCV source code is also using sobel operator to calculate. 3. In the calculated gradient value, all the points that are not extreme values are set to 0, and most weak edges are removed. So the edges will get thinner. The double thresholds t1 and T2 are like this. The point where T1 <= T2 is greater than T2 must be the point whose edge is smaller than T1, and it must not be the point whose edge is between T1 and T2. Through the identified edge points, a search in the direction of 8 domains is initiated (broad search). Finally, the Canny edge diagram is obtained.
The three operators distinguish between strong and weak edges generated by Sobel and have good anti-noise performance Laplace is sensitive to edges. Some of them may be noisy edges, which are also included in the calculation. The edges generated by Canny are very thin, perhaps as thin as a pixel, without strong or weak.
The following three graphs are the results of Sobel, Canny and Laplace respectively.
Sobel operator, Canny operator, Laplace operator
Function syntax introduction:
The function prototype of Laplace operator:
dst = cv2.Laplacian(src, ddepth[, dst[, ksize[, scale[, delta[, borderType]]]]])
SRC — original image ddepth — the depth of the image. -1 means that the image is at the same depth as the original image. The depth of the target image must be greater than or equal to the depth of the original image
[Optional parameters]
DST — Target image
Ksize — The size of the operator, which must be 1, 3, 5, or 7. The default is 1
Scale — is the constant scaling of the scaling derivative, with no scaling factor by default
Delta – is an optional increment that will be added to the final DST. Again, no additional value is added to DST by default
BorderType — is the mode that determines the boundaries of the image. This parameter defaults to cv2.border_default.
Example:
import cv2
import numpy as np
img = cv2.imread(“3.jpg”, 0)
gray_lap = cv2.Laplacian(img,cv2.CV_16S,ksize = 3)
dst = cv2.convertScaleAbs(gray_lap)
cv2.imshow(‘laplacian’,dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
The original link https://www.jianshu.com/p/2334bee37de5