我想编写基本算法,它将在另一个位图中搜索位图。我有RGB值的字节数组。此刻我试过'for'循环,但我的算法有点慢。
我的意思是什么?
13 53 53 12 43 32 23 32 12 43
15 50 32 53 12 69 43 105 32 86
我想找到
的X和Y. 14 45 33
55 14 71
他们在这里:
12 43 32
53 12 69
所以Y = 0 X = 4
但我们只需要一些容忍度来找到它们。
任何提示如何快速完成?
我的愚蠢代码:
for (int y = 0;y<bigData.Height-smallData.Height + 1;y++)
{
int x = 0;
for (x = 0; x < bigData.Width - smallData.Width + 1; x++)
{
tx = x;
int color1 = y * bigBmpStride;
int bigColorB = bigBmpRgb[color1 + (x * 3)];
int bigColorG = bigBmpRgb[color1 + (x * 3) + 1];
int bigColorR = bigBmpRgb[color1 + (x * 3) + 2];
int smallColorB, smallColorG, smallColorR;
if (!move)
{
int color2 = 0 * bigBmpStride;
smallColorB = bigBmpRgb[color2 + (0 * 3)];
smallColorG = bigBmpRgb[color2 + (0 * 3) + 1];
smallColorR = bigBmpRgb[color2 + (0 * 3) + 2];
}
else
{
int color2 = tempY * bigBmpStride;
smallColorB = bigBmpRgb[color2 + (tempX * 3)];
smallColorG = bigBmpRgb[color2 + (tempX * 3) + 1];
smallColorR = bigBmpRgb[color2 + (tempX * 3) + 2];
}
if (IsInRange(bigColorB, smallColorB, tolerance) && IsInRange(bigColorG, smallColorG, tolerance) && IsInRange(bigColorR, smallColorR, tolerance))
{
move = true;
if (tempY != 0)
Debug.WriteLine(tempX.ToString() + "|" + tempY.ToString() + " " + smallData.Height + "|" + smallData.Width);
if (tempX == smallData.Height)
break;
tempX++;
continue;
}
else
{
move = false;
tempX = 0;
tempY = 0;
continue;
}
}
if (tempX == smallData.Height && tempY == smallData.Width)
{
ret = new Point(tx, y);
Debug.WriteLine("found!!");
found = true;
break;
}
if (found)
break;
if (move)
tempY++;
else
{
tempY = 0;
tempX = 0;
}
}