OpenCV note finishing [Canny edge detection]

Canny edge detection is a method of detecting edges using a [multi-stage] edge detection algorithm.

It is mainly divided into the following steps:

1. Denoising:

Filter noise, use filtering to smooth some non-edge areas with weaker textures.

2. Calculate the gradient:

Calculate the magnitude and angle of the gradient (PS: 2↑ means the magnitude is 2 and the angle is 90°). It is worth noting that the edges and gradients are always vertical.
insert image description here

3. Non-maximum suppression:

Traverse the pixels in turn to determine whether the current pixel is the maximum value in the surrounding gradient direction. If it is the maximum value, it is kept, otherwise it is suppressed (zeroed).
insert image description here

4. Identify edges using double thresholding:

Eliminate virtual edges and determine real edges.
insert image description here

After the previous steps, we have obtained some edges, but in addition to the real edges, there are some noise edges among these edges, so we need to further remove them. There are mainly 3 situations:

Strong edges greater than or equal to maxVal: retain
virtual edges greater than minVal and less than maxVal: keep if connected to strong edges, otherwise suppress.
Edges smaller than minVal: Suppress

Above code:

Canny(img,minVal,maxVal)

import cv2

o=cv2.imread("lena.bmp",cv2.IMREAD_GRAYSCALE)
r1=cv2.Canny(o,128,200)
r2=cv2.Canny(o,32,128)
cv2.imshow("original",o)
cv2.imshow("result1",r1)
cv2.imshow("result2",r2)

cv2.waitKey()
cv2.destroyAllWindows()

Result:
insert image description here
Bye bye. . . .

Guess you like

Origin blog.csdn.net/qq_34699535/article/details/120456934