0
所以我無法理解如何去調整我找到的代碼。基本上我想在圖像中尋找圖像(識別我的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像素。
謝謝。
如果您的捕獲數據存儲爲一個位圖,你可以穿越它之前先裁剪圖像 – jenovachild