扫描图像为特定图像,限定范围

本文关键字:图像 范围 扫描 | 更新日期: 2023-09-27 18:17:47

所以我很难理解如何通过调整我发现的代码。基本上,我想在图像中寻找图像(识别我的IP摄像头拍摄的照片中的某些物体)。我在网上找到了代码,这是有效的,除了我想只在图像上的某个区域看。目前正在扫描整个图像,我认为这是不必要的。

代码如下:

    unsafe
            {
                byte* pSmall = (byte*)(void*)HealthbarData.Scan0;
                byte* pBig = (byte*)(void*)CaptureData.Scan0;
                int smallOffset = HealthbarStride - HealthbarImage.Width * 3;
                int bigOffset = CaptureStride - CaptureImage.Width * 3;
                bool matchFound = true;
                for (int y = 0; y < CaptureHeight; y++)
                {
                    for (int x = 0; x < CaptureWidth; x++)
                    {
                        byte* pBigBackup = pBig;
                        byte* pSmallBackup = pSmall;
                        //Look for the small picture.
                        for (int i = 0; i < HealthbarHeight; i++)
                        {
                            int j = 0;
                            matchFound = true;
                            for (j = 0; j < HealthbarWidth; j++)
                            {
                                //With tolerance: pSmall value should be between margins.
                                int inf = pBig[0] - Margin;
                                int sup = pBig[0] + Margin;
                                if (sup < pSmall[0] || inf > pSmall[0])
                                {
                                    matchFound = false;
                                    break;
                                }
                                pBig++;
                                pSmall++;
                            }
                            if (!matchFound)
                                break;
                            //We restore the pointers.
                            pSmall = pSmallBackup;
                            pBig = pBigBackup;
                            //Next rows of the small and big pictures.
                            pSmall += HealthbarStride * (1 + i);
                            pBig += CaptureStride * (1 + i);
                        }
                        //If match found, we return.
                        if (matchFound)
                        {
                            EnemyPosition.X = x;
                            EnemyPosition.Y = y;
                            break;
                        }
                        //If no match found, we restore the pointers and continue.
                        else
                        {
                            pBig = pBigBackup;
                            pSmall = pSmallBackup;
                            pBig += 3;
                        }
                    }
                    if (matchFound)
                        break;
                    pBig += bigOffset;
                }
            }

我可以在if (matchFound)下检查它是否在允许的范围内,但它仍然扫描整个图像。

谁能给我任何提示或如何做到这一点?比方说,它只检查图像中间300像素以内的区域。

谢谢。

扫描图像为特定图像,限定范围

选项1。裁剪原图像

让我们假设您捕获的图像CaptureImageBitmap

你可以在处理CaptureImage之前裁剪它。

Bitmap CaptureImage = MethodWhereImageDataComesFrom();
// you asked for ~300 pixels around the center, you can calculate that rectangle
int cropWidth = 300;
int cropHeight = 300;
int x = CaptureImage.Width / 2 - (cropWidth / 2);
int y = CaptureImage.Height / 2 - (cropHeight / 2);
// create your rectangle and bitmap of the cropped image bounds here
Rectangle rect = new Rectangle(x, y, cropWidth, cropHeight);
Bitmap croppedImage = new Bitmap(rect.Width, rect.Height);
// grab the graphics component of your cropped image so we can draw pixels from the capture image to it.
Graphics g = Graphics.FromImage(croppedImage);
// now draw the cropped pixels from capture
g.DrawImage(CaptureImage, new Rectangle(0, 0, croppedImage.Width, croppedImage.Height), rect, GraphicsUnit.Pixel);

现在你可以遍历croppedImage,它应该只有一个300x300像素的图像。

注意这段代码是未经测试的,尽管它应该可以工作。

<标题>选项2。在图像循环中指定范围

你应该能够在你的图像循环中指定宽度和高度范围。

// define the dimensions we want to search in pixels
int cropWidth = 300;
int cropHeight = 300;
// determine our start positions for x and y (this is the top left corner of the rectangle we're searching in)
int startWidth = CaptureWidth / 2 - (cropWidth / 2); // start for x
int startHeight = CaptureHeight / 2 - (cropHeight / 2); // start for y

现在在开始循环的代码中,您可以简单地指定以下内容:

for (int y = startHeight; y < startHeight + cropHeight; y++)
{
    for (int x = startWidth; x < startWidth + cropWidth; x++)
    {
        // your code here...
    }
}