如何计算5X5矩阵中sobel边缘检测的dx和dy?

时间:2012-11-06 06:34:04

标签: c# algorithm graphics core-graphics graph-algorithm

我对如何在sobel边缘检测方法中计算dx和dy感到困惑.... 我用Google搜索了一下,然后我发现了.......

  public class SobelEdgeDetector : ImageFilter<SobelEdgeDetectorParms>
{
   private Color grayscale(Color cr)
    {
        return Color.FromArgb(cr.A, (int)(cr.R * .3 + cr.G * .59 + cr.B * 0.11),
            (int)(cr.R * .3 + cr.G * .59 + cr.B * 0.11),
            (int)(cr.R * .3 + cr.G * .59 + cr.B * 0.11));
    }
    public override Bitmap FilterProcessImage(SobelEdgeDetectorParms parms, Bitmap image)
    {
        Bitmap ret = new Bitmap(image.Width, image.Height);
        for (int i = 1; i < image.Width - 1; i++)
        {
            for (int j = 1; j < image.Height - 1; j++)
            {
                Color cr = image.GetPixel(i + 1, j);
                Color cl = image.GetPixel(i - 1, j);
                Color cu = image.GetPixel(i, j - 1);
                Color cd = image.GetPixel(i, j + 1);
                Color cld = image.GetPixel(i - 1, j + 1);
                Color clu = image.GetPixel(i - 1, j - 1);
                Color crd = image.GetPixel(i + 1, j + 1);
                Color cru = image.GetPixel(i + 1, j - 1);
                int dx = 0, dy = 0;
                switch (parms.Channel)
                {
                    case Channels.R:
                        dx = cld.R + 2 * cd.R + crd.R - (clu.R + 2 * cu.R + cru.R);
                        dy = crd.R + 2 * cr.R + cru.R - (cld.R + 2 * cl.R + clu.R);
                        break;
                    case Channels.G:
                        dx = cld.G + 2 * cd.G + crd.G - (clu.G + 2 * cu.G + cru.G);
                        dy = crd.G + 2 * cr.G + cru.G - (cld.G + 2 * cl.G + clu.G);
                        break;
                    case Channels.B:
                        dx = cld.B + 2 * cd.B + crd.B - (clu.B + 2 * cu.B + cru.B);
                        dy = crd.B + 2 * cr.B + cru.B - (cld.B + 2 * cl.B + clu.B);
                        break;
                    case Channels.RGB:
                        dx = grayscale(cld).B + 2 * grayscale(cd).B + grayscale(crd).B - (grayscale(clu).B + 2 * grayscale(cu).B + grayscale(cru).B);
                        dy = grayscale(crd).B + 2 * grayscale(cr).B + grayscale(cru).B - (grayscale(cld).B + 2 * grayscale(cl).B + grayscale(clu).B);
                        break;
                }
                double power = Math.Abs(dx) + Math.Abs(dy);
                if (power > parms.Threshold)
                    ret.SetPixel(i, j, parms.EdgeColor);
                else
                {
                    if (parms.CopyOriginal)
                    {
                        Color c = image.GetPixel(i, j);
                        if (parms.ConvertToGrayscale)
                        {
                            ret.SetPixel(i, j,
                                Color.FromArgb(255,
                                (int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11),
                                (int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11),
                                (int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11)));
                        }
                        else
                            ret.SetPixel(i, j, c);
                    }
                    else
                        ret.SetPixel(i, j, Color.White);
                }
            }
        }
        return ret;
    }
    public override System.Windows.Forms.Control GetParameterWindow()
    {
        return new SobelEdgeDetectorParmForm();
    }
}

这是针对3X3矩阵.........我想实现5X5 Matrix .......... 实际上我想要选择点的切线斜率。 。 。 。 tangent = Atan2(dx,dy)。 使用3X3矩阵我得到正确的切线,但它没有更准确的切线。 我对计算5X5矩阵的dx和dy感兴趣。我不想检测边缘,我想检测是Atan2的切线(dx,dy)........ 请帮帮我......

1 个答案:

答案 0 :(得分:0)

用于垂直方向的英特尔性能基元(IPP)的Sobel 5x5矩阵核心(水平方向旋转):

1   4   6   4   1
2   8  12   8   2
0   0   0   0   0
-2  -8 -12  -8  -2
-1  -4  -6  -4  -1

注意Atan2函数参数顺序:Atan2( y,x