c# - 使用 Lockbits C# 进行边缘检测

标签 c# image-processing edge-detection lockbits

我做了一个实现边缘检测算法的程序, 但需要很长时间来处理。 我读过有关使用锁位和不安全状态而不是 getpixel 和 setpixel 的信息,但我仍然不明白如何使用它。

这是我的示例代码:

private Bitmap SobelEdgeDetect(Bitmap original)
        {
            Bitmap b = original;
            Bitmap bb = original;
            int width = b.Width;
            int height = b.Height;
            int[,] gx = new int[,] { { -1, 0, 1 }, { -2, 0, 2 }, { -1, 0, 1 } };
            int[,] gy = new int[,] { { 1, 2, 1 }, { 0, 0, 0 }, { -1, -2, -1 } };

            int[,] allPixR = new int[width, height];
            int[,] allPixG = new int[width, height];
            int[,] allPixB = new int[width, height];

            int limit = 128 * 128;

            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    allPixR[i, j] = b.GetPixel(i, j).R;
                    allPixG[i, j] = b.GetPixel(i, j).G;
                    allPixB[i, j] = b.GetPixel(i, j).B;
                }
            }

            int new_rx = 0, new_ry = 0;
            int new_gx = 0, new_gy = 0;
            int new_bx = 0, new_by = 0;
            int rc, gc, bc;
            for (int i = 1; i < b.Width - 1; i++)
            {
                for (int j = 1; j < b.Height - 1; j++)
                {

                    new_rx = 0;
                    new_ry = 0;
                    new_gx = 0;
                    new_gy = 0;
                    new_bx = 0;
                    new_by = 0;
                    rc = 0;
                    gc = 0;
                    bc = 0;

                    for (int wi = -1; wi < 2; wi++)
                    {
                        for (int hw = -1; hw < 2; hw++)
                        {
                            rc = allPixR[i + hw, j + wi];
                            new_rx += gx[wi + 1, hw + 1] * rc;
                            new_ry += gy[wi + 1, hw + 1] * rc;

                            gc = allPixG[i + hw, j + wi];
                            new_gx += gx[wi + 1, hw + 1] * gc;
                            new_gy += gy[wi + 1, hw + 1] * gc;

                            bc = allPixB[i + hw, j + wi];
                            new_bx += gx[wi + 1, hw + 1] * bc;
                            new_by += gy[wi + 1, hw + 1] * bc;
                        }
                    }
                    if (new_rx * new_rx + new_ry * new_ry > limit || new_gx * new_gx + new_gy * new_gy > limit || new_bx * new_bx + new_by * new_by > limit)
                        bb.SetPixel(i, j, Color.Black);


                    else
                        bb.SetPixel(i, j, Color.Transparent);
                }
            }
            return bb;
        }

我正在使用 fastbitmap 类,我是这样实现的:

private Bitmap SobelEdgeDetectTwo(Bitmap original)
        {
            int width = original.Width;
            int height = original.Height;
            Bitmap result = new Bitmap(width,height);
            FastBitmap b = new FastBitmap(original);
            FastBitmap bb = new FastBitmap(result);

            b.LockBitmap();
            bb.LockBitmap();

            int[,] gx = new int[,] { { -1, 0, 1 }, { -2, 0, 2 }, { -1, 0, 1 } };
            int[,] gy = new int[,] { { 1, 2, 1 }, { 0, 0, 0 }, { -1, -2, -1 } };

            int[,] allPixR = new int[width, height];
            int[,] allPixG = new int[width, height];
            int[,] allPixB = new int[width, height];

            int limit = 128 * 128;

            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    var pixel = b.GetPixel(i,j);
                    allPixR[i, j] = pixel.Red;
                    allPixG[i, j] = pixel.Green;
                    allPixB[i, j] = pixel.Blue;

                }
            }

            int new_rx = 0, new_ry = 0;
            int new_gx = 0, new_gy = 0;
            int new_bx = 0, new_by = 0;
            int rc, gc, bc;
            for (int i = 1; i < width - 1; i++)
            {
                for (int j = 1; j < height - 1; j++)
                {

                    new_rx = 0;
                    new_ry = 0;
                    new_gx = 0;
                    new_gy = 0;
                    new_bx = 0;
                    new_by = 0;
                    rc = 0;
                    gc = 0;
                    bc = 0;

                    for (int wi = -1; wi < 2; wi++)
                    {
                        for (int hw = -1; hw < 2; hw++)
                        {
                            rc = allPixR[i + hw, j + wi];
                            new_rx += gx[wi + 1, hw + 1] * rc;
                            new_ry += gy[wi + 1, hw + 1] * rc;

                            gc = allPixG[i + hw, j + wi];
                            new_gx += gx[wi + 1, hw + 1] * gc;
                            new_gy += gy[wi + 1, hw + 1] * gc;

                            bc = allPixB[i + hw, j + wi];
                            new_bx += gx[wi + 1, hw + 1] * bc;
                            new_by += gy[wi + 1, hw + 1] * bc;
                        }
                    }

                    if (new_rx * new_rx + new_ry * new_ry > limit || new_gx * new_gx + new_gy * new_gy > limit || new_bx * new_bx + new_by * new_by > limit)
                    {
                        PixelData p = new PixelData(Color.Black);
                        bb.SetPixel(i, j, p);

                    }
                    else
                    {
                        PixelData p = new PixelData(Color.Transparent);
                        bb.SetPixel(i, j, p);

                    }

                }
            }


            b.UnlockBitmap();
            bb.UnlockBitmap();

            return result;
        }

但是,图像根本没有改变。 您能否就我的代码的哪一部分错误提出建议?

最佳答案

使用类似 FastBitmap 的类是最简单的.只需添加一个 FastBitmap 并在该类上而不是在您的 Bitmap 上使用 GetPixel(),其余的可以相同。

像这样:

Bitmap dstBmp = new Bitmap(width, height, original.PixelFormat);
FastBitmap fastBitmap = new FastBitmap(dstBmp);
fastBitmap.LockBitmap();
//...
var pixel = fastBitmap.GetPixel(x,y);
//...
fastBitmap.UnlockBitmap();

关于c# - 使用 Lockbits C# 进行边缘检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16747257/

相关文章:

image - 处理图像并找到外边缘。查找算法

c++ - 我需要修改用于对角线边缘检测的 Sobel 内核。 OpenCV 和 C++

matlab - 为什么积分图像包含额外的零行和零列?

c# - 在 C# 中使用 json 对象

c# - 18 位唯一 ID - 代码可靠性

c# - 加号登录查询字符串

matlab - 在以下情况下如何使用 Matlab 的 princomp 函数?

python - 可以在Canny Edge框架上显示彩色文本吗?

python - 用python或Halcon获取图像的绝对值导数

c# - 等待 2 个线程中的 1 个完成