opencv3.0 函数学习 8——Canny 算子检测轮廓

Canny算子检测轮廓  (有多种轮廓检测算法,可供用户选择)

Canny算子  

1983,MIT,Canny提出的边缘检测三个标准:

  1. 检测标准:不丢失重要的边缘,不应有虚假的边缘;
  2. 定位标准:实际边缘与检测到的边缘位置之间的偏差最小;
  3. 单响应标准:将多个响应降低为单个边缘响应。也就是说,图像中本来只有一个边缘点,可是却检测出多个边缘点,这就对应了多个响应。

Canny算子力图在抗噪声干扰与精度之间寻求最佳方案,Canny算子有相关的复杂理论,其基本的步骤是:

  1. 使用高斯滤波器平滑图像,卷积核尺度通过高斯滤波器的标准差确定
  2. 计算滤波后图像的梯度幅值和方向 可以使用Sobel算子计算Gx与Gy方向的梯度,则梯度幅值和梯度的方向依次为 

  3. 使用非最大化抑制方法确定当前像素点是否比邻域像素点更可能属于边缘的像素,以得到细化的边缘 其实现是:将当前像素位置的梯度值与其梯度方向上相邻的的梯度方向的梯度值进行比较,如果周围存在梯度值大于当前像素的梯度值,则不认为查找到的当前像素点为边缘点。举例来说,Gx方向的3个梯度值依次为[2 4 3],则在Gx梯度方向上4所在像素点就是边缘点,如果把改成[2 4 1]就不是边缘点。如果用全向的梯度方向作为非最大抑制的判断依据,则要求G(x,y)>所有4邻域的或8邻域的梯度值才被认为是边缘点。
  4. 使用双阈值[T1,T2]法检测边缘的起点和终点,这样能形成连接的边缘。T2>T1,T2用来找到没条线段,T1用来在这条线段两端延伸寻找边缘的断裂处,并连接这些边缘。



函数参数

void cv::Canny

(

InputArray  image,
    OutputArray  edges,
    double  threshold1,                 (需调节)
    double  threshold2,                   (需调节)
    int  apertureSize = 3,        (模板大小可调节 3*3  5*5 7*7  默认3*3)
    bool  L2gradient = false   (默认采用false)
 

)

   

Finds edges in an image using the Canny algorithm [25] .

The function finds edges in the input image image and marks them in the output map edges using the Canny algorithm. The smallest value between threshold1 and threshold2 is used for edge linking. The largest value is used to find initial segments of strong edges. See http://en.wikipedia.org/wiki/Canny_edge_detector

Parameters
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 L 2   norm =(dI/dx) 2 +(dI/dy) 2  − − − − − − − − − − − − − − − −     should be used to calculate the image gradient magnitude ( L2gradient=true ), or whether the defaultL 1   norm =|dI/dx|+|dI/dy|  is enough ( L2gradient=false ).


第一个参数:输入:是灰度图,就算是彩色图也会处理成灰度图


第二个参数:输出的图的位置,输出的图式二值图


第三第四个参数:是两个阈值,上限与下限,如果一个像素的梯度大于上限,则被认为是边缘像素,如果低于下限则被抛弃,如果介于两者之间,只有当其与高于上限的阈值的像素连接时才会被接受。


第五个参数:表示模板的大小,如果是3,则表示3*3矩阵的大小



 

你可能感兴趣的:(opencv)