【发布时间】: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