三种边缘检测算子

3种边缘检测算子

灰度或结构等信息的突变位置是图像的边缘,图像的边缘有幅度和方向属性,沿边缘方向像素变化缓慢,垂直边缘方向像素变化剧烈。因此,边缘上的变化能通过梯度计算出来。

一阶导数的梯度算子

对于二维的图像,梯度定义为一个向量,

Gx对于x方向的梯度,Gy对应y方向的梯度,向量的幅值本来是 mag(f) = (Gx2 + Gy2)1/2,为简化计算,一般用mag(f)=|Gx|+|Gy|近似,幅值同时包含了x而后y方向的梯度信息。梯度的方向为 α = arctan(Gx/Gy) 。

由于图像的数字离散特性,所以梯度微分运算用差分代替,并且用小的空域模板和图像进行卷积近似计算梯度,由于模板的不同,因此衍生处多种梯度算子:Roberts算子、Sobel算子和Prewitt算子。

Sobel与Prewitt算子模板

Sobel与Prewitt算子模板

平滑模板都有一个特点,即模板内所有平滑值的和为0,因此梯度计算的步骤是:

  1. 计算Gx与Gy
  2. 用mag(f)=|Gx|+|Gy|近似近似计算(x,y)点处的梯度值G(x,y)
  3. 模板中心移动一个像素点,计算下一像素点的梯度值(这一平移求和的过程实质就是卷积运算)
  4. 计算完所有的像素点处的梯度值后,选择一个阈值T,如果(x,y)处的G(x,y)>T,则认为该点是边缘点

高斯拉普拉斯算子

上面的一阶导数算子,是各向异性的,因此分x方向和y方向的梯度值,而高斯拉普拉斯算子是对图像求二阶导数,边缘对应二阶导数的过零点。

由上式可知,xy进行互换的结果是一样的,所以拉普拉斯算子没有x方向和y方向的区分,拉普拉斯算子对应图像中的差分运算是:

也可以通过卷积模板实现,

LOG算子

LOG算子

相对于一阶导数,高斯拉普拉斯算子(Laplacian of Gaussian, LOG算子)由于求二阶导数,很容易将点噪声判别为边界,因此常在使用LOG算子前先用高斯平滑滤波器去除正态分布的噪声,二维高斯分布为:

其中 σ 为高斯分布标准差,决定高斯滤波器的宽度,用该函数对图像平滑滤波,可以减少椒盐噪声对LOG算子的影响。

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用来在这条线段两端延伸寻找边缘的断裂处,并连接这些边缘。

OpenCV中相关源码

Sobel算子及LOG算子的源码在/modules/imgproc/src/deriv.cpp中,Canny算子实现在/modules/imgproc/src/canny.cpp中。

经过之前的基础准备,感觉只要知道什么时候该用什么OpenCV函数,其它的一切都变得简单起来了。于是感觉学着去探索OpenCV的源码对自己的受益会更大,就从这里开始吧。

deriv.cpp中有Sobel算子的源码:

<code class="sourceCode c" style="margin: 0px; padding: 0.5em; line-height: normal; font-family: Monaco, 'Courier New', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace; color: rgb(51, 51, 51); border: none; display: block; background: rgb(248, 248, 255);"><span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">void</span> cv::Sobel( InputArray _src, OutputArray _dst, <span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">int</span> ddepth, <span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">int</span> dx, <span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">int</span> dy,
                <span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">int</span> ksize, <span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">double</span> scale, <span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">double</span> delta, <span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">int</span> borderType )
{
    Mat src = _src.getMat();   <span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">// 从InputArray中提取Mat数据结构,InputArray只能作为形参的类型,但可以传入Mat类型实参</span>
    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span> (ddepth < <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>)
        ddepth = src.depth();  <span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">// 像素深度(即像素位数),有CV_8U=0, CV_8S=1, CV_16U=2, CV_16S=3, CV_32S=4, CV_32F=5, CV_64F=6</span>
    _dst.create( src.size(), CV_MAKETYPE(ddepth, src.channels()) );
    Mat dst = _dst.getMat();
    
<span class="ot" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32);">#if defined (HAVE_IPP) && (IPP_VERSION_MAJOR >= 7)</span>
    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span>(dx < <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">3</span> && dy < <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">3</span> && src.channels() == <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span> && borderType == <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>)
    {
        <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span>(IPPDeriv(src, dst, ddepth, dx, dy, ksize,scale))
            <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">return</span>;
    }
<span class="ot" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32);">#endif</span>
    <span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">int</span> ktype = std::max(CV_32F, std::max(ddepth, src.depth()));

    Mat kx, ky;
    getDerivKernels( kx, ky, dx, dy, ksize, false, ktype );  <span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">// 创建Sobel算子差分用的卷积模板,结果放在kx,ky中</span>
    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span>( scale != <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span> )
    {
        <span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">// usually the smoothing part is the slowest to compute,</span>
        <span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">// so try to scale it instead of the faster differenciating part</span>
        <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span>( dx == <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span> )
            kx *= scale;
        <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">else</span>
            ky *= scale;
    }
    sepFilter2D( src, dst, ddepth, kx, ky, Point(-<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>,-<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>), delta, borderType );  <span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">// 使用卷积核进行平滑操作,前面已经说过,差分转化为卷积操作,而卷积运算就是平滑滤波</span>
}</code>

getSobelKernels是实际创建卷积模板的函数,被上面的getDerivKernels调用,不妨看看OpenCV中Sobel创建的卷积模板是啥样的,下面只是getSobelKernels函数的一部分,

<code class="sourceCode c" style="margin: 0px; padding: 0.5em; line-height: normal; font-family: Monaco, 'Courier New', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace; color: rgb(51, 51, 51); border: none; display: block; background: rgb(248, 248, 255);"><span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">for</span>( <span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">int</span> k = <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>; k < <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">2</span>; k++ )
{
    Mat* kernel = k == <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span> ? &kx : &ky;
    <span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">int</span> order = k == <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span> ? dx : dy;
    <span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">int</span> ksize = k == <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span> ? ksizeX : ksizeY;

    CV_Assert( ksize > order );

    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span>( ksize == <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span> )
        kerI[<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>] = <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>;
    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">else</span> <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span>( ksize == <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">3</span> )
    {
        <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span>( order == <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span> )
            kerI[<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>] = <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>, kerI[<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>] = <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">2</span>, kerI[<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">2</span>] = <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>;   <span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">// 只进行均值平滑,无差分作用</span>
        <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">else</span> <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span>( order == <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span> )
            kerI[<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>] = -<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>, kerI[<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>] = <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>, kerI[<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">2</span>] = <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>;  <span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">// 差分算子</span>
        <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">else</span>
            kerI[<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>] = <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>, kerI[<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>] = -<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">2</span>, kerI[<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">2</span>] = <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>;
    }
    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">else</span>
    {
        ...
    }</code>

ksize表示卷积核的大小,之前理论分析中取的是3x3的模板,对应到if( ksize == 3 )order变量确定对x梯度方向的卷积模板进行赋值还是y梯度方向的卷积进行赋值,因此,当且仅当Sobel函数的输入实参中dx=1时才计算Gx方向的梯度,dy=1时才计算dy方向的梯度。OpenCV没有给出Prewiit算子的源码,但可以自己通过修改替换getDerivKernels函数实现Prewiit的功能。

LOG算子也可以进行相同的分析,这里就不写下来了。再看Canny算子,

<code class="sourceCode c" style="margin: 0px; padding: 0.5em; line-height: normal; font-family: Monaco, 'Courier New', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace; color: rgb(51, 51, 51); border: none; display: block; background: rgb(248, 248, 255);"><span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">void</span> cv::Canny( InputArray image, OutputArray _edges,
                <span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">double</span> threshold1, <span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">double</span> threshold2,
                <span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">int</span> apertureSize, bool L2gradient )
{
    Mat src = image.getMat();
    _edges.create(src.size(), CV_8U);
    CvMat c_src = src, c_dst = _edges.getMat();
    cvCanny( &c_src, &c_dst, threshold1, threshold2,
        apertureSize + (L2gradient ? CV_CANNY_L2_GRADIENT : <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>));
}</code>

C++版本的Canny算子实际就是调用原来C版本中的函数,只是进行了下封装而已,在cvCanny函数中我看到这么几行代码:

<code class="sourceCode c" style="margin: 0px; padding: 0.5em; line-height: normal; font-family: Monaco, 'Courier New', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace; color: rgb(51, 51, 51); border: none; display: block; background: rgb(248, 248, 255);">dx = cvCreateMat( size.height, size.width, CV_16SC1 );
dy = cvCreateMat( size.height, size.width, CV_16SC1 );
cvSobel( src, dx, <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>, <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>, aperture_size );  <span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">// 计算Gx </span>
cvSobel( src, dy, <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>, <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>, aperture_size );  <span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">// 计算Gy</span></code>

Canny就是调用Sobel算子计算x方向的梯度Gx和y方向的梯度Gy。计算梯度角度和非最大化抑制的代码有些长,

<code class="sourceCode c" style="margin: 0px; padding: 0.5em; line-height: normal; font-family: Monaco, 'Courier New', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace; color: rgb(51, 51, 51); border: none; display: block; background: rgb(248, 248, 255);"><span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">// calculate magnitude and angle of gradient, perform non-maxima supression.</span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">// fill the map with one of the following values:</span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">//   0 - the pixel might belong to an edge</span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">//   1 - the pixel can not belong to an edge</span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">//   2 - the pixel does belong to an edge</span>
<span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">for</span>( i = <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>; i <= size.height; i++ )
{
    <span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">int</span>* _mag = mag_buf[(i > <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>) + <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>] + <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>;
    <span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">float</span>* _magf = (<span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">float</span>*)_mag;
    <span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">const</span> <span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">short</span>* _dx = (<span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">short</span>*)(dx->data.ptr + dx->step*i);
    <span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">const</span> <span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">short</span>* _dy = (<span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">short</span>*)(dy->data.ptr + dy->step*i);
    uchar* _map;
    <span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">int</span> x, y;
    ptrdiff_t magstep1, magstep2;
    <span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">int</span> prev_flag = <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>;

    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span>( i < size.height )
    {
        _mag[-<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>] = _mag[size.width] = <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>;

        <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span>( !(flags & CV_CANNY_L2_GRADIENT) )
            <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">for</span>( j = <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>; j < size.width; j++ )
                _mag[j] = abs(_dx[j]) + abs(_dy[j]);
        <span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">/*else if( icvFilterSobelVert_8u16s_C1R_p != 0 ) // check for IPP</span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">        {</span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">            // use vectorized sqrt</span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">            mag_row.data.fl = _magf;</span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">            for( j = 0; j < size.width; j++ )</span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">            {</span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">                x = _dx[j]; y = _dy[j];</span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">                _magf[j] = (float)((double)x*x + (double)y*y);</span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">            }</span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">            cvPow( &mag_row, &mag_row, 0.5 );</span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">        }*/</span>
        <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">else</span>
        {
            <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">for</span>( j = <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>; j < size.width; j++ )
            {
                x = _dx[j]; y = _dy[j];
                _magf[j] = (<span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">float</span>)std::sqrt((<span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">double</span>)x*x + (<span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">double</span>)y*y);
            }
        }
    }
    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">else</span>
        memset( _mag<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">-1</span>, <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>, (size.width + <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">2</span>)*<span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">sizeof</span>(<span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">int</span>) );

    <span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">// at the very beginning we do not have a complete ring</span>
    <span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">// buffer of 3 magnitude rows for non-maxima suppression</span>
    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span>( i == <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span> )
        <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">continue</span>;

    _map = map + mapstep*i + <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>;
    _map[-<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>] = _map[size.width] = <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>;
    
    _mag = mag_buf[<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>] + <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>; <span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">// take the central row</span>
    _dx = (<span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">short</span>*)(dx->data.ptr + dx->step*(i<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">-1</span>));
    _dy = (<span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">short</span>*)(dy->data.ptr + dy->step*(i<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">-1</span>));
    
    magstep1 = mag_buf[<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">2</span>] - mag_buf[<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>];
    magstep2 = mag_buf[<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>] - mag_buf[<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>];

    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span>( (stack_top - stack_bottom) + size.width > maxsize )
    {
        <span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">int</span> sz = (<span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">int</span>)(stack_top - stack_bottom);
        maxsize = MAX( maxsize * <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">3</span>/<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">2</span>, maxsize + <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">8</span> );
        stack.resize(maxsize);
        stack_bottom = &stack[<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>];
        stack_top = stack_bottom + sz;
    }

    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">for</span>( j = <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>; j < size.width; j++ )
    {
        <span class="ot" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32);">#define CANNY_SHIFT 15</span>
        <span class="ot" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32);">#define TG22  (int)(0.4142135623730950488016887242097*(1<<CANNY_SHIFT) + 0.5)</span>

        x = _dx[j];
        y = _dy[j];
        <span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">int</span> s = x ^ y;
        <span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">int</span> m = _mag[j];

        x = abs(x);
        y = abs(y);
        <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span>( m > low )
        {
            <span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">int</span> tg22x = x * TG22;
            <span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">int</span> tg67x = tg22x + ((x + x) << CANNY_SHIFT);

            y <<= CANNY_SHIFT;

            <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span>( y < tg22x )
            {
                <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span>( m > _mag[j<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">-1</span>] && m >= _mag[j<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">+1</span>] )
                {
                    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span>( m > high && !prev_flag && _map[j-mapstep] != <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">2</span> )
                    {
                        CANNY_PUSH( _map + j );
                        prev_flag = <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>;
                    }
                    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">else</span>
                        _map[j] = (uchar)<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>;
                    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">continue</span>;
                }
            }
            <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">else</span> <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span>( y > tg67x )
            {
                <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span>( m > _mag[j+magstep2] && m >= _mag[j+magstep1] )
                {
                    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span>( m > high && !prev_flag && _map[j-mapstep] != <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">2</span> )
                    {
                        CANNY_PUSH( _map + j );
                        prev_flag = <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>;
                    }
                    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">else</span>
                        _map[j] = (uchar)<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>;
                    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">continue</span>;
                }
            }
            <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">else</span>
            {
                s = s < <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span> ? -<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span> : <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>;
                <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span>( m > _mag[j+magstep2-s] && m > _mag[j+magstep1+s] )
                {
                    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span>( m > high && !prev_flag && _map[j-mapstep] != <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">2</span> )
                    {
                        CANNY_PUSH( _map + j );
                        prev_flag = <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>;
                    }
                    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">else</span>
                        _map[j] = (uchar)<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>;
                    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">continue</span>;
                }
            }
        }
        prev_flag = <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>;
        _map[j] = (uchar)<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>;
    }

    <span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">// scroll the ring buffer</span>
    _mag = mag_buf[<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>];
    mag_buf[<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>] = mag_buf[<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>];
    mag_buf[<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>] = mag_buf[<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">2</span>];
    mag_buf[<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">2</span>] = _mag;
}</code>

最后就是使用双阈值跟踪边界,形成连续边缘,

<code class="sourceCode c" style="margin: 0px; padding: 0.5em; line-height: normal; font-family: Monaco, 'Courier New', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace; color: rgb(51, 51, 51); border: none; display: block; background: rgb(248, 248, 255);"><span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">// now track the edges (hysteresis thresholding)</span>
<span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">while</span>( stack_top > stack_bottom )
{
    uchar* m;
    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span>( (stack_top - stack_bottom) + <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">8</span> > maxsize )
    {
        <span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">int</span> sz = (<span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">int</span>)(stack_top - stack_bottom);
        maxsize = MAX( maxsize * <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">3</span>/<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">2</span>, maxsize + <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">8</span> );
        stack.resize(maxsize);
        stack_bottom = &stack[<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>];
        stack_top = stack_bottom + sz;
    }

    CANNY_POP(m);

    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span>( !m[-<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>] )
        CANNY_PUSH( m - <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span> );
    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span>( !m[<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>] )
        CANNY_PUSH( m + <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span> );
    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span>( !m[-mapstep<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">-1</span>] )
        CANNY_PUSH( m - mapstep - <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span> );
    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span>( !m[-mapstep] )
        CANNY_PUSH( m - mapstep );
    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span>( !m[-mapstep<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">+1</span>] )
        CANNY_PUSH( m - mapstep + <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span> );
    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span>( !m[mapstep<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">-1</span>] )
        CANNY_PUSH( m + mapstep - <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span> );
    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span>( !m[mapstep] )
        CANNY_PUSH( m + mapstep );
    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span>( !m[mapstep<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">+1</span>] )
        CANNY_PUSH( m + mapstep + <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span> );
}

<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">// the final pass, form the final image</span>
<span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">for</span>( i = <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>; i < size.height; i++ )
{
    <span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">const</span> uchar* _map = map + mapstep*(i<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">+1</span>) + <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>;
    uchar* _dst = dst->data.ptr + dst->step*i;
    
    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">for</span>( j = <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>; j < size.width; j++ )
        _dst[j] = (uchar)-(_map[j] >> <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>);
}</code>

这其中貌似用到了栈对邻域梯度信息进行保存,以上详细的实现没做太多的分析,但流程就摆在那里了。请注意,OpenCV中的Canny实现包含了Canny算子的3个步骤,唯独没有第一步中的高斯平滑滤波,因此调用前得先使用高斯平滑滤波。

试试身手

Sobel算子的源码:

<code class="sourceCode c" style="margin: 0px; padding: 0.5em; line-height: normal; font-family: Monaco, 'Courier New', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace; color: rgb(51, 51, 51); border: none; display: block; background: rgb(248, 248, 255);"><span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">/*</span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;"> * FileName : sobel.cpp</span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;"> * Author   : xiahouzuoxin @163.com</span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;"> * Version  : v1.0</span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;"> * Date     : Sun 16 Nov 2014 09:53:16 AM CST</span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;"> * Brief    : </span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;"> * </span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;"> * Copyright (C) MICL,USTB</span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;"> */</span>
<span class="ot" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32);">#include <iostream></span>
<span class="ot" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32);">#include "cv.h"</span>
<span class="ot" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32);">#include "highgui.h"</span>
<span class="ot" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32);">#include "opencv2/imgproc/imgproc.hpp"</span>

using namespace std;
using namespace cv;

<span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">int</span> main(<span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">int</span> argc, <span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">char</span> *argv[])
{
    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span> (argc < <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">2</span>) {
        cout << <span class="st" style="margin: 0px; padding: 0px; color: rgb(64, 112, 160);">"Usage: ./sobel [image file]"</span> <<endl;
        <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">return</span> -<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>;
    }
    <span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">// Read image</span>
    Mat src = imread(argv[<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>], CV_LOAD_IMAGE_COLOR);
    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span> (!src.data) {
        cout << <span class="st" style="margin: 0px; padding: 0px; color: rgb(64, 112, 160);">"Error: read image"</span> << endl;
        <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">return</span> -<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>;
    }
    cvtColor(src, src, CV_RGB2GRAY);
    namedWindow(<span class="st" style="margin: 0px; padding: 0px; color: rgb(64, 112, 160);">"Origin"</span>, <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>);
    imshow(<span class="st" style="margin: 0px; padding: 0px; color: rgb(64, 112, 160);">"Origin"</span>,src);

    <span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">// CV_EXPORTS_W void Sobel( InputArray src, OutputArray dst, int ddepth,</span>
    <span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">//                      int dx, int dy, int ksize=3,</span>
    <span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">//                      double scale=1, double delta=0,</span>
    <span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">//                      int borderType=BORDER_DEFAULT );</span>
    Mat dst_x;
    <span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">// Gradient X</span>
    Sobel(src, dst_x, src.depth(), <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>, <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>, <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">3</span>, <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>, <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>, BORDER_DEFAULT);
    namedWindow(<span class="st" style="margin: 0px; padding: 0px; color: rgb(64, 112, 160);">"Sobel image X gradient"</span>, <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>);
    imshow(<span class="st" style="margin: 0px; padding: 0px; color: rgb(64, 112, 160);">"Sobel image X gradient"</span>, dst_x);
    Mat dst_y;
    <span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">// Gradient Y</span>
    Sobel(src, dst_y, src.depth(), <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>, <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>, <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">3</span>, <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>, <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>, BORDER_DEFAULT);
    namedWindow(<span class="st" style="margin: 0px; padding: 0px; color: rgb(64, 112, 160);">"Sobel image Y gradient"</span>, <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>);
    imshow(<span class="st" style="margin: 0px; padding: 0px; color: rgb(64, 112, 160);">"Sobel image Y gradient"</span>, dst_y);

    Mat dst;
    <span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">// Method one: |G|=|Gx|+|Gy|</span>
    convertScaleAbs(dst_x, dst_x);
    convertScaleAbs(dst_y, dst_y);
    addWeighted(dst_x, <span class="fl" style="float: left; margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0.5</span>, dst_y, <span class="fl" style="float: left; margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0.5</span>, <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>, dst);
    namedWindow(<span class="st" style="margin: 0px; padding: 0px; color: rgb(64, 112, 160);">"Sobel image XY"</span>, <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>);
    imshow(<span class="st" style="margin: 0px; padding: 0px; color: rgb(64, 112, 160);">"Sobel image XY"</span>, dst);
    <span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">// Method two:</span>
    Sobel(src, dst, src.depth(), <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>, <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>, <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">3</span>, <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>, <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>, BORDER_DEFAULT);

    waitKey();

    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">return</span> <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>;
}</code>

简单的换一下函数,就是LOG算子的源码:

<code class="sourceCode c" style="margin: 0px; padding: 0.5em; line-height: normal; font-family: Monaco, 'Courier New', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace; color: rgb(51, 51, 51); border: none; display: block; background: rgb(248, 248, 255);"><span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">/*</span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;"> * FileName : Laplace.cpp</span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;"> * Author   : xiahouzuoxin @163.com</span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;"> * Version  : v1.0</span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;"> * Date     : Sun 16 Nov 2014 10:52:09 AM CST</span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;"> * Brief    : </span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;"> * </span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;"> * Copyright (C) MICL,USTB</span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;"> */</span>
<span class="ot" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32);">#include <iostream></span>
<span class="ot" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32);">#include "cv.h" </span>
<span class="ot" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32);">#include "highgui.h"</span>
<span class="ot" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32);">#include "opencv2/imgproc/imgproc.hpp"</span>

using namespace std;
using namespace cv;

<span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">int</span> main(<span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">int</span> argc, <span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">char</span> *argv[])
{    
    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span> (argc < <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">2</span>) {
        cout << <span class="st" style="margin: 0px; padding: 0px; color: rgb(64, 112, 160);">"Usage: ./Laplace [image file]"</span> <<endl;
        <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">return</span> -<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>;
    }
    <span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">// Read image</span>
    Mat src = imread(argv[<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>], CV_LOAD_IMAGE_COLOR);
    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span> (!src.data) {
        cout << <span class="st" style="margin: 0px; padding: 0px; color: rgb(64, 112, 160);">"Error: read image"</span> << endl;
        <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">return</span> -<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>;
    }
    cvtColor(src, src, CV_RGB2GRAY);
    namedWindow(<span class="st" style="margin: 0px; padding: 0px; color: rgb(64, 112, 160);">"Origin"</span>, CV_WINDOW_AUTOSIZE);
    imshow(<span class="st" style="margin: 0px; padding: 0px; color: rgb(64, 112, 160);">"Origin"</span>,src);

    Mat dst;
    <span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">// CV_EXPORTS_W void Laplacian( InputArray src, OutputArray dst, int ddepth,</span>
    <span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">//                          int ksize=1, double scale=1, double delta=0,</span>
    <span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">//                          int borderType=BORDER_DEFAULT );</span>
    Laplacian(src, dst, src.depth(), <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">3</span>, <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>, <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>, BORDER_DEFAULT);
    namedWindow(<span class="st" style="margin: 0px; padding: 0px; color: rgb(64, 112, 160);">"Laplacian"</span>, CV_WINDOW_AUTOSIZE);
    imshow(<span class="st" style="margin: 0px; padding: 0px; color: rgb(64, 112, 160);">"Laplacian"</span>,dst);

    waitKey();

    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">return</span> <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>;
}</code>

Canny算子的源码也很简单,只不过使用了GaussianBlur进行高斯平滑,

<code class="sourceCode c" style="margin: 0px; padding: 0.5em; line-height: normal; font-family: Monaco, 'Courier New', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace; color: rgb(51, 51, 51); border: none; display: block; background: rgb(248, 248, 255);"><span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">/*</span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;"> * FileName : canny.cpp</span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;"> * Author   : xiahouzuoxin @163.com</span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;"> * Version  : v1.0</span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;"> * Date     : Sun 16 Nov 2014 10:59:31 AM CST</span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;"> * Brief    : </span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;"> * </span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;"> * Copyright (C) MICL,USTB</span>
<span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;"> */</span>
 
<span class="ot" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32);">#include <iostream></span>
<span class="ot" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32);">#include "cv.h" </span>
<span class="ot" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32);">#include "highgui.h"</span>
<span class="ot" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32);">#include "opencv2/imgproc/imgproc.hpp"</span>

using namespace std;
using namespace cv;

<span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">int</span> main(<span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">int</span> argc, <span class="dt" style="margin: 0px; padding: 0px; color: rgb(144, 32, 0);">char</span> *argv[])
{
    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span> (argc < <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">2</span>) {
        cout << <span class="st" style="margin: 0px; padding: 0px; color: rgb(64, 112, 160);">"Usage: ./canny [image file]"</span> <<endl;
        <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">return</span> -<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>;
    }
    <span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">// Read image</span>
    Mat src = imread(argv[<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>], CV_LOAD_IMAGE_COLOR);
    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">if</span> (!src.data) {
        cout << <span class="st" style="margin: 0px; padding: 0px; color: rgb(64, 112, 160);">"Error: read image"</span> << endl;
        <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">return</span> -<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">1</span>;
    }
    cvtColor(src, src, CV_RGB2GRAY);
    namedWindow(<span class="st" style="margin: 0px; padding: 0px; color: rgb(64, 112, 160);">"Origin"</span>, CV_WINDOW_AUTOSIZE);
    imshow(<span class="st" style="margin: 0px; padding: 0px; color: rgb(64, 112, 160);">"Origin"</span>,src);

    Mat dst;
    GaussianBlur(src, dst, Size(<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">3</span>,<span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">3</span>), <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>, <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>);  <span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">// 使用Gaussian滤波器进行平滑</span>
    <span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">// CV_EXPORTS_W void Canny( InputArray image, OutputArray edges,</span>
    <span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">//                      double threshold1, double threshold2,</span>
    <span class="co" style="margin: 0px; padding: 0px; color: rgb(96, 160, 176); font-style: italic;">//                      int apertureSize=3, bool L2gradient=false );</span>
    Canny(dst, dst, <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">50</span>, <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">200</span>, <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">3</span>);
    namedWindow(<span class="st" style="margin: 0px; padding: 0px; color: rgb(64, 112, 160);">"Canny"</span>, CV_WINDOW_AUTOSIZE);
    imshow(<span class="st" style="margin: 0px; padding: 0px; color: rgb(64, 112, 160);">"Canny"</span>,dst);

    waitKey();

    <span class="kw" style="margin: 0px; padding: 0px; color: rgb(0, 112, 32); font-weight: bold;">return</span> <span class="dv" style="margin: 0px; padding: 0px; color: rgb(64, 160, 112);">0</span>;
}</code>

请注意,上面的Sobel和LOG算子代码都没有在计算结果后使用阈值判断是否属于边界,而直接显示了边缘信息。

Sobel边缘检测结果:右上为x梯度结果,左下为y梯度结果,右下为G(x,y)梯度结果

Sobel边缘检测结果:右上为x梯度结果,左下为y梯度结果,右下为G(x,y)梯度结果

Laplace边缘检测结果

Laplace边缘检测结果

Canny边缘检测结果
展开阅读全文

没有更多推荐了,返回首页