【问题标题】:Scanning images for a specific image, limiting range扫描特定图像的图像,限制范围
【发布时间】:2016-11-15 09:43:46
【问题描述】:

所以我很难通过调整找到的代码来理解如何进行。基本上我想在图像中寻找图像(识别我的 IP cam 制作的照片中的某些对象)。我在网上找到了代码,除了我只想查看图像上的某个区域之外,这很有效。目前正在扫描整个图像,我认为这是不必要的。

代码如下:

    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 像素以内。

谢谢。

【问题讨论】:

  • 如果您的捕获数据存储为位图,您可以先裁剪图像,然后再遍历它

标签: c# image image-processing


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2020-10-07
    • 1970-01-01
    • 2016-09-08
    • 2011-05-08
    • 2012-08-28
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多