gpt4 book ai didi

c# - 使用 lockbits 进行图像处理,替代 getpixel?

转载 作者:太空狗 更新时间:2023-10-29 21:18:30 25 4
gpt4 key购买 nike

我正在尝试使用锁位来增加我的图像检测类,但这会导致代码出现问题,因此它无法运行。我如何才能同时使用 lockbits 和 getpixel 以加快图像检测速度,或者有人可以告诉我一个同样快的替代方案吗?

代码:

static IntPtr Iptr = IntPtr.Zero;
static BitmapData bitmapData = null;
static public byte[] Pixels { get; set; }
static public int Depth { get; private set; }
static public int Width { get; private set; }
static public int Height { get; private set; }

static public void LockBits(Bitmap source)

{
// Get width and height of bitmap
Width = source.Width;
Height = source.Height;

// get total locked pixels count
int PixelCount = Width * Height;

// Create rectangle to lock
Rectangle rect = new Rectangle(0, 0, Width, Height);

// get source bitmap pixel format size
Depth = System.Drawing.Bitmap.GetPixelFormatSize(source.PixelFormat);


// Lock bitmap and return bitmap data
bitmapData = source.LockBits(rect, ImageLockMode.ReadWrite,
source.PixelFormat);

// create byte array to copy pixel values
int step = Depth / 8;
Pixels = new byte[PixelCount * step];
Iptr = bitmapData.Scan0;

// Copy data from pointer to array
Marshal.Copy(Iptr, Pixels, 0, Pixels.Length);

}


static public bool SimilarColors(int R1, int G1, int B1, int R2, int G2, int B2, int Tolerance)
{
bool returnValue = true;
if (Math.Abs(R1 - R2) > Tolerance || Math.Abs(G1 - G2) > Tolerance || Math.Abs(B1 - B2) > Tolerance)
{
returnValue = false;
}
return returnValue;
}


public bool findImage(Bitmap small, Bitmap large, out Point location)
{
unsafe
{
LockBits(small);
LockBits(large);
//Loop through large images width
for (int largeX = 0; largeX < large.Width; largeX++)
{
//And height
for (int largeY = 0; largeY < large.Height; largeY++)
{
//Loop through the small width
for (int smallX = 0; smallX < small.Width; smallX++)
{
//And height
for (int smallY = 0; smallY < small.Height; smallY++)
{
//Get current pixels for both image
Color currentSmall = small.GetPixel(smallX, smallY);
Color currentLarge = large.GetPixel(largeX + smallX, largeY + smallY);
//If they dont match (i.e. the image is not there)

if (!colorsMatch(currentSmall, currentLarge))
//Goto the next pixel in the large image

goto nextLoop;
}
}
//If all the pixels match up, then return true and change Point location to the top left co-ordinates where it was found
location = new Point(largeX, largeY);
return true;
//Go to next pixel on large image
nextLoop:
continue;
}
}
//Return false if image is not found, and set an empty point
location = Point.Empty;
return false;
}
}

最佳答案

您不会希望依赖 getPixel() 进行图像处理;偶尔调用以获取点值(例如在鼠标悬停时)是可以的,但通常最好在图像内存中或在必要时可以转换为位图的某些 2D 数组中进行图像处理。

首先,您可以尝试编写一个方法,使用 LockBits/UnlockBits 提取一个便于操作的数组。完成对数组的操作后,您可以使用不同的 LockBits/UnlockBits 函数将其写回位图。

这是我过去使用过的一些示例代码。第一个函数从位图中返回值的一维数组。由于您知道位图的宽度,因此可以将此一维数组转换为二维数组以进行进一步处理。完成处理后,您可以调用第二个函数将(修改后的)一维数组再次转换为位图。

public static byte[] Array1DFromBitmap(Bitmap bmp){
if (bmp == null) throw new NullReferenceException("Bitmap is null");

Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData data = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);
IntPtr ptr = data.Scan0;

//declare an array to hold the bytes of the bitmap
int numBytes = data.Stride * bmp.Height;
byte[] bytes = new byte[numBytes];

//copy the RGB values into the array
System.Runtime.InteropServices.Marshal.Copy(ptr, bytes, 0, numBytes);

bmp.UnlockBits(data);

return bytes;
}

public static Bitmap BitmapFromArray1D(byte[] bytes, int width, int height)
{
Bitmap grayBmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
Rectangle grayRect = new Rectangle(0, 0, grayBmp.Width, grayBmp.Height);
BitmapData grayData = grayBmp.LockBits(grayRect, ImageLockMode.ReadWrite, grayBmp.PixelFormat);
IntPtr grayPtr = grayData.Scan0;

int grayBytes = grayData.Stride * grayBmp.Height;
ColorPalette pal = grayBmp.Palette;

for (int g = 0; g < 256; g++){
pal.Entries[g] = Color.FromArgb(g, g, g);
}

grayBmp.Palette = pal;

System.Runtime.InteropServices.Marshal.Copy(bytes, 0, grayPtr, grayBytes);

grayBmp.UnlockBits(grayData);
return grayBmp;
}

这些方法对 Bitmap 像素格式做出的假设可能不适合您,但我希望总体思路是清楚的:使用 LockBits/UnlockBits 从 Bitmap 中提取字节数组,以便您可以编写和调试算法最很容易,然后再次使用 LockBits/UnlockBits 将数组再次写入位图。

为了可移植性,我建议您的方法返回所需的数据类型,而不是在方法本身内操作全局变量。

如果您一直在使用 getPixel(),那么如上所述转换为数组或从数组转换可以显着加快您的代码速度,只需少量的编码工作投入。

关于c# - 使用 lockbits 进行图像处理,替代 getpixel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12168654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com