## Python + opencv: Canny edge detection

Machine vision 001 2020-11-16 01:29:21
python opencv canny edge detection

## Python+OpenCV：Canny edge detection

### theory

Canny Edge detection is a popular edge detection algorithm , It consists of John F. Canny The invention .

1. This is a multi-stage algorithm .

2. Noise reduction ： Because the edge detection is easy to be affected by the noise in the image , The first step is to use 5x5 Gaussian filter to remove noise in the image .

3.  Find the intensity gradient of the image ： The smoothed image is processed horizontally and vertically Sobel Kernel filtering , Get the horizontal direction (Gx) And the vertical direction (Gy) First derivative of . From these two images , We can find the edge gradient and direction of each pixel as follows ：

The gradient direction is always perpendicular to the edge . It's one of the four corners of a circle , Stand for vertical , Horizontal and two diagonal directions .

4. Non-maximum Inhibition ： The gradient and direction are obtained after , A full scan of the image , To remove any unwanted pixels （ It may not form an edge ）.

So , At each pixel , Check whether there is a local maximum in the neighborhood of the gradient direction . Look at the picture below ：

spot A On the edge ( vertical direction ). The gradient is perpendicular to the edge . spot B Sum point C It's in the gradient direction . therefore , Point to point A Sum point B、C To test , See if it forms a local maximum . If it is , Consider moving on to the next stage , otherwise , It will be suppressed ( Set to zero ).

In short , What you get is a “ Thin edges ” The binary image of .

5.  Lag threshold (Hysteresis Thresholding)： This stage determines which are the real edges , Which are not edges . So , We need two thresholds ,minVal and maxVal.

Any intensity gradient greater than maxVal It must be the edge of , And below minVal It must be a non edge , So it's discarded .

The edges between these two thresholds are classified as edge or non edge according to their connectivity . If they are connected to “ Determine the edge ” Pixels , They are considered part of the edge . otherwise , They will also be discarded . Please look at the chart below. ：

edge A stay maxVal On top of , So it's thought to be “ The definite side ”. Even though C Side by side maxVal below , But it has something to do with A Side to side , So this side is also considered to be an effective side , We got that complete curve . But side B Although in minVal above , And with the edge C In the same area , But it's not connected to anything “sure-edge”, So it's discarded . therefore , We have to choose... Accordingly minVal and maxVal To get the right results , This is very important .

This stage can also assume that the edge is a long line , And remove small pixel noise .

Final , What we get is a strong edge in the image .

### Example

``````####################################################################################################
# Canny edge detection (Canny Edge Detection)
def lmc_cv_canny_edge_detection():
"""
The functionality : Canny edge detection (Canny Edge Detection).
"""
image = lmc_cv.cvtColor(image, lmc_cv.COLOR_BGR2GRAY)
# Canny edge detection (Canny Edge Detection)
edges_image = lmc_cv.Canny(image, 100, 250, L2gradient=False)
# Display images
pyplot.figure('Image Display')
titles = ['Original Image', 'Canny Edge Image']
images = [image, edges_image]
for i in range(2):
pyplot.subplot(1, 2, i + 1)
pyplot.imshow(images[i], 'gray')
pyplot.title(titles[i])
pyplot.xticks([])
pyplot.yticks([])
pyplot.show()
# Save images based on user input
if ord("q") == (lmc_cv.waitKey(0) & 0xFF):
# Destruction of the window
pyplot.close()
return``````