-1

我需要用指定的颜色找到它找到的第一个像素的点/坐标(x,y)。我使用了 GetPixel() 方法,但它有点太慢了,正在研究 LockBits。我怎么不知道这是否真的可以解决我的问题。我可以使用 LockBits 返回找到的像素的点吗?

这是我当前的代码:

public Point FindPixel(Image Screen, Color ColorToFind)
{
    Bitmap bit = new Bitmap(Screen);
    BitmapData bmpData = bit.LockBits(new Rectangle(0, 0, bit.Width, bit.Height),
                                    ImageLockMode.ReadWrite,
                                    PixelFormat.Format32bppPArgb);
    unsafe
    {
        byte* ptrSrc = (byte*)bmpData.Scan0;
        for (int y = 0; y < bmpData.Height; y++)
        {
            for (int x = 0; x < bmpData.Width; x++)
            {
                Color c = bit.GetPixel(x, y);
                if (c == ColorToFind)
                    return new Point(x, y);
            }
        }
    }
    bit.UnlockBits(bmpData);
    return new Point(0, 0);
}
4

2 回答 2

2

你没有停止使用 GetPixel() 所以你没有领先。改为这样写:

    int IntToFind = ColorToFind.ToArgb();
    int height = bmpData.Height;    // These properties are slow so read them only once
    int width = bmpData.Width;
    unsafe
    {
        for (int y = 0; y < height; y++)
        {
            int* pline = (int*)bmpData.Scan0 + y * bmpData.Stride/4;
            for (int x = 0; x < width; x++)
            {
                if (pline[x] == IntToFind)
                    return new Point(x, bit.Height - y - 1);
            }
        }
    }

看起来很奇怪的 Point 构造函数是必要的,因为线在位图中是倒置的。并且不要在失败时返回 new Point(0, 0),这是一个有效的像素。

于 2012-02-12T17:34:20.637 回答
1

您的代码几乎没有问题:

  1. 您正在使用 PixelFormat.Format32bppPArgb - 您应该使用图像的像素格式,如果它们不匹配,则无论如何都会在引擎盖下复制所有像素。
  2. 您仍在使用GetPixel,因此所有这些麻烦不会给您带来任何优势。

为了LockBits有效地使用,你基本上想要锁定你的图像,然后使用不安全的指针来获取像素值。执行此操作的代码对于不同的像素格式会有所不同,假设您确实将有 32bpp 格式,蓝色位于 LSB,您的代码可能如下所示:

for (int y = 0; y < bmpData.Height; ++y)
{ 
    byte* ptrSrc = (byte*)(bmpData.Scan0 + y * bmpData.Stride);
    int* pixelPtr = (int*)ptrSrc;

    for (int x = 0; x < bmpData.Width; ++x)
    {
        Color col = Color.FromArgb(*pixelPtr);

        if (col == ColorToFind) return new Point(x, y);

        ++pixelPtr; //Increate ptr by 4 bytes, because it is int
    }
}

几点说明:

  • 对于每一行,使用 Scan0 + 步幅值计算新的 ptrSrc。这是因为仅增加指针可能会失败,如果Stride != bpp * width,可能就是这种情况。
  • 我假设蓝色像素表示为 LSB,而 alpha 表示为 MSB,我认为情况并非如此,因为那些 GDI 像素格式是.. 奇怪 ;),只要确保你检查它,如果它是另一种方式,反字节在使用FromArgb()方法之前。
  • 如果您的像素格式是 24bpp,那就有点棘手了,因为出于显而易见的原因,您不能使用 int 指针并将其增加 1(4 个字节)。
于 2012-02-12T17:33:19.727 回答