前言:
目标:
- 理解Canny边缘检测
- OpenCV函数的学习cv2.Canny()
Canny算法原理
Canny是一个非常受欢迎的边缘检测算法,主要分为四步的过程:
- 去噪
边缘对噪声敏感,第一步先用高斯滤波器来滤波。 - 寻找图像的密度梯度
对平滑后的图像进行滤波用sobel滤波器来求出x,y方向的导数。
梯度方向总是垂直于边缘 - 非极大值抑制
在得到梯度的值和方向后,对图像进行全面扫描,取出不需要的像素,这些像素可能不构成边缘。对于这一点如果像素是其梯度方向上邻域的最大值,如果是,则考虑下一个,如果不是,则将值抑制,设置为0。 - 滞后阈值法
- 这个阶段决定哪些是边哪些不是边。因此我们需要两个阈值minval和maxval。大于maxval的一定是边,小于minval的一定不是边,居于两者中间的则进一步考察其连通性来确定是否为边缘。
如图所示:
opencv中的用法
Finds edges in an image,寻找一个图像中的边缘。
cv2.Canny(image, threshold1, threshold2[, edges[, apertureSize[, L2gradient]]]) → \rightarrow →edges
- image – 8-bit input image.
- edges – output edge map; single channels 8-bit image, which has the same size as image .
- threshold1 – first threshold for the hysteresis procedure.
- threshold2 – second threshold for the hysteresis procedure.
- apertureSize – aperture size for the Sobel() operator.
- L2gradient – a flag, indicating whether a more accurate L2 norm =p(dI=dx)2 + (dI=dy)2 should be used to calculate the image gradient magnitude (L2gradient=true ), or whether the default L1 norm = jdI=dxj + jdI=dyj is enough (L2gradient=false ).