How did Canny achieve such efficiency? In brief, it can be summarized as the following four steps:
1. Use 5 * 5 Gaussian filter to eliminate noise
2. Call Sobel to calculate the direction of image gradient (0 °/45 °/90 °/135 °)
3. Take local maximum value in 4 directions
4. Threshold calculation
Canny's api:
Canny(img,minVal,maxVal,...)
Img: specific image
MinVal: What is the minimum threshold value? If it is less than this threshold value, it is not considered as an edge
MaxVal: What is the maximum threshold value? If it is greater than this value, it must be an edge. If it is between the minimum value and the maximum value, it should be calculated
import cv2 import numpy as np # 导入一张图 img=cv2.imread('你的图片') # canny边缘检测 dst=cv2.Canny(img,100,220) # 显示图片 cv2.imshow('img',img) cv2.imshow('dst',dst) cv2.waitKey(0)