Edge detection is to extract and detect the edge of the image. There are several methods as follows:

# Category of edge detection operators

``Edge detection operator: first derivative: Roberts, Sobel, Prewitt second derivative: Laplacian, Log/Marr, (Kirsch, Nevitia) non-differential edge detection operator: CannyCopy the code``

``(Math again, baidu again)Copy the code``

# Canny()

OpenCV- Canny() in Python already encapsulates the API, so it’s easy to call it directly, and it works pretty well.

``Steps: The color image is converted to gray image (read in gray image or single channel image) and the image is blurred by Gaussian (denoising) to calculate the image gradient. """ cv2.Canny(image, # input original image (must be a single channel image) threshold1, threshold2, threshold2, threshold2, threshold1, threshold2, threshold2, threshold1, threshold2, # EDGES [, edges[, apertureSize[, # apertureSize: size of Sobel operator L2gradient]]]) # Edges (Boolean) : true: Use the more precise L2 norm (i.e. the sum of squares of the reciprocal of the two directions), false: use the L1 norm (directly adding the absolute values of the derivatives in both directions). """ "(here the function borrows ideas from the boss)Copy the code``

Function:

``````def edge_demo(image): # Edge detection blurred= cv.GaussianBlur(image,(3.3),0)
gray = cv.cvtColor(blurred,cv.COLOR_BGR2GRAY)
# x
# y
ygray = cv.Sobel(gray,cv.CV_16SC1,0.1)

Copy the code``````

Results:In fact, here canny parameter directly put the original image (no processing) can also, such as:

``````def edge_demo2(image): # Edge detection 2 edge_out= cv.Canny(image,50.150)

Copy the code``````

(Tut-tut the power of visible functions)

# Sobel ()

``Sobel's is the first-order differential operator, that is, the first derivative of the image. According to the definition of the derivative, the more obvious the change is, the larger the gradient is, which naturally corresponds to the edge of the image.Copy the code``
``DST = cv2.sobel (img, ddepth, dx, dy, [ksize]) img is the original gray image; Ddepth is the depth of the image, that is, the number of bits in the image, dx is the x direction; Dy is the y direction; The two are usually used together if (1,0) is the gradient in the x direction, if (0,1) is the gradient in the y direction; Ksize indicates the size of the kernel and is usually the default parameterCopy the code``
``````def Sobel_demo(image): # Edge detection Sobel Sobelx= cv.Sobel(image, cv.CV_64F, 1.0Sobely = cv.sobel (image, cv.cv_64f,0.1Sobely = cv.convertScaleabs (sobely) sobelxy = cv.addweighted (sobelx)0.5, sobely, 0.5.0) #, add the gradient in both directions with weights"sobel",sobelxy)
Copy the code``````

Results:

# Scharr ()

It’s the same as Sobel, except the convolution kernel is different, and the operation is exactly the same.

``````def scharr_demo(image): # Edge detection scharr Scharrx= cv.Scharr(image, cv.CV_64F, 1.0)
scharry = cv.Scharr(image, cv.CV_64F, 0.1)
scharrx = cv.convertScaleAbs(scharrx)
scharry = cv.convertScaleAbs(scharry)
scharrxy = cv.addWeighted(scharrx, 0.5, scharry, 0.5.0)
cv.imshow("scharr", scharrxy)
Copy the code``````

Results: