Image edge detection using
CV2 provides the function canny to extract the edges of the image.
The idea of the algorithm is as follows:
- 1. Use Gaussian blur to remove noise points ()
- 2. Gray scale conversion ()
- 3. Using the sobel operator, calculate the gradient size and direction of the gradient at each point
- 4. Use non-extremely large value suppression (only the largest retention) to eliminate the spurious effect caused by edge detection
- 5. Apply dual thresholds to identify real and potential edges
- 6. Final edge detection is accomplished by suppressing weak edges
The Canny function is defined as follows:
edge = (image, threshold1, threshold2[, edges[, apertureSize[, L2gradient ]]])
The meaning of the parameters is as follows:
-
image
: The image to be detected -
threshold1
: Threshold 1 (minimum) -
threshold2
: Threshold 2 (maximum value), use this parameter for distinct edge detection -
edges
: Image edge information -
apertureSize
: sobel operator (convolution kernel) size -
L2gradient
: Boolean. -
True
: Calculations using the more precise L2 paradigm (i.e., the sum of the squares of the derivatives in both directions re-squared) -
False
: Use the L1 paradigm (directly add the absolute values of the two directional derivatives)
The larger of the thresholds 2 is used to detect the obvious edges in the image, but generally the detection will not be so perfect and the edges are detected intermittently. So this time the smaller first threshold is used to connect these intermittent edges.
Impact of thresholds on detection results
import cv2 import numpy as np from matplotlib import pyplot as plt img = ('d:\\') edges = (img,100,200,apertureSize=3) edges2 = (img,100,200,apertureSize=5) (131),(img,cmap = 'gray') ('Original Image'), ([]), ([]) (132),(edges,cmap = 'gray') ('Edge Image1'), ([]), ([]) (133),(edges2,cmap = 'gray') ('Edge Image2'), ([]), ([]) ()
It can be seen that after adjusting threshold1, more edges are detected.
Effect of sobel operator on detection results
The default operator size for sobel is 3. Expanding the operator will give you more detail, but it will also extract the image more.
import cv2 import numpy as np from matplotlib import pyplot as plt img = ('d:\\') edges = (img,100,200,apertureSize=3) edges2 = (img,100,200,apertureSize=5) (131),(img,cmap = 'gray') ('Original Image'), ([]), ([]) (132),(edges,cmap = 'gray') ('Edge Image1'), ([]), ([]) (133),(edges2,cmap = 'gray') ('Edge Image2'), ([]), ([]) ()
Effect of Paradigm on Test Results
With L2gradient=True, the detected edges are reduced.
import cv2 import numpy as np from matplotlib import pyplot as plt img = ('d:\\') edges = (img,100,200,L2gradient=False) edges2 = (img,100,200,L2gradient=True) (131),(img,cmap = 'gray') ('Original Image'), ([]), ([]) (132),(edges,cmap = 'gray') ('Edge Image1'), ([]), ([]) (133),(edges2,cmap = 'gray') ('Edge Image2'), ([]), ([]) ()
summarize
The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.