如何以编程方式检测图像边框?

use*_*060 1 automation

我正在寻找一个检测图像边框的程序,例如我有一个正方形,程序检测到X/Y-Coords

例:

替代文字http://img709.imageshack.us/img709/1341/22444641.png

And*_*res 5

这是一个非常简单的边缘检测器.它适用于二进制图像.它只计算水平和垂直像素之间的差异,如image.pos [1,1] = image.pos [1,1] - image.pos [1,2]和垂直差异相同.请记住,您还需要在值0..255的范围内对其进行标准化.

但!如果您只需要一个程序,请使用Adobe Photoshop.

用C#编写的代码.

public void SimpleEdgeDetection()
{
    BitmapData data = Util.SetImageToProcess(image);
    if (image.PixelFormat != PixelFormat.Format8bppIndexed)
        return;

    unsafe
    {
        byte* ptr1 = (byte *)data.Scan0;
        byte* ptr2;
        int offset = data.Stride - data.Width;
        int height = data.Height - 1;
        int px;

        for (int y = 0; y < height; y++)
        {
            ptr2 = (byte*)ptr1 + data.Stride;
            for (int x = 0; x < data.Width; x++, ptr1++, ptr2++)
            {
                px = Math.Abs(ptr1[0] - ptr1[1]) + Math.Abs(ptr1[0] - ptr2[0]);
                if (px > Util.MaxGrayLevel) px = Util.MaxGrayLevel;
                ptr1[0] = (byte)px;
            }
            ptr1 += offset;
        }
    }
    image.UnlockBits(data);
}
Run Code Online (Sandbox Code Playgroud)

Util类的方法

static public BitmapData SetImageToProcess(Bitmap image)
{
    if (image != null)
        return image.LockBits(
            new Rectangle(0, 0, image.Width, image.Height),
            ImageLockMode.ReadWrite,
            image.PixelFormat);

    return null;
}
Run Code Online (Sandbox Code Playgroud)

如果您需要更多解释或算法,只需询问更多信息,而不是那么一般.