如何在5X5矩阵的sobel边缘检测中计算dx和dy? [英] How to calculate dx and dy in sobel edge detection in 5X5 matrix?

查看:799
本文介绍了如何在5X5矩阵的sobel边缘检测中计算dx和dy?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在sobel边缘检测方法中如何计算dx和dy有点困惑....
我用它搜索了一下,发现了.......

I am bit confused how to calculate dx and dy in sobel edge detection method ....
I googled it and i found that.......

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矩阵..........
实际上,我想将切线倾斜到所选点. . . .
tangent = Atan2(dx,dy).
使用3X3矩阵,我得到了正确的切线,但是它没有更精确的切线.
我对5X5矩阵的dx和dy的计算感兴趣.我不想检测边缘,我想检测是Atan2(dx,dy)的切线.....
请帮助我....


this is for 3X3 Matrix .........I want to implement 5X5 Matrix..........
Actually I want slop of tangent line to selected point. . . .
tangent= Atan2(dx,dy).
Using 3X3 matrix i am getting correct tangent, but it not more accurate tangent.
I am interested in calculation of dx and dy for 5X5 matrix. I don''t want to detect edge, i want to detect tangent that is Atan2(dx,dy)........
Please help me....

推荐答案

您可以使用此来源中的系数来计算dx和dy: ^ ]
You can use the coefficients from this source to compute dx and dy: http://www.cim.mcgill.ca/~image529/TA529/Image529_99/assignments/edge_detection/references/sobel.htm[^]


这篇关于如何在5X5矩阵的sobel边缘检测中计算dx和dy?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆