扫描特定图像的图像,限制范围

时间:2016-11-14 18:35:56

标签: c# image image-processing

所以我无法通过调整我找到的代码来理解如何去做。基本上我想在图像中寻找图像(识别我的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 个答案:

答案 0 :(得分:0)

选项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...
    }
}