Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static List<Point> Find(Bitmap bigBmp, Bitmap smallbmp, int tolerance)
- {
- List<Point> res = new List<Point>();
- BitmapData smallData = smallbmp.LockBits(new Rectangle(0, 0, smallbmp.Width, smallbmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
- BitmapData bigData = bigBmp.LockBits(new Rectangle(0, 0, bigBmp.Width, bigBmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
- int smallStride = smallData.Stride;
- int bigStride = bigData.Stride;
- int bigWidth = bigBmp.Width;
- int bigHeight = bigBmp.Height - smallbmp.Height + 1;
- int smallWidth = smallbmp.Width * 3;
- int smallHeight = smallbmp.Height;
- Point location = new Point();
- int margin = tolerance;
- unsafe
- {
- byte* pSmall = (byte*)(void*)smallData.Scan0;
- byte* pBig = (byte*)(void*)bigData.Scan0;
- int smallOffset = smallStride - smallbmp.Width * 3;
- int bigOffset = bigStride - bigBmp.Width * 3;
- bool matchFound = true;
- for (int y = 0; y < bigHeight; y++)
- {
- for (int x = 0; x < bigWidth; x++)
- {
- byte* pBigBackup = pBig;
- byte* pSmallBackup = pSmall;
- //Look for the small picture.
- for (int i = 0; i < smallHeight; i++)
- {
- int j = 0;
- matchFound = true;
- for (j = 0; j < smallWidth; 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 += smallStride * (1 + i);
- pBig += bigStride * (1 + i);
- }
- //If match found, we return.
- if (matchFound)
- {
- location.X = x;
- location.Y = y;
- res.Add(location);
- }
- //If no match found, we restore the pointers and continue.
- pBig = pBigBackup;
- pSmall = pSmallBackup;
- pBig += 3;
- }
- if (matchFound) break;
- pBig += bigOffset;
- }
- bigBmp.UnlockBits(bigData);
- smallbmp.UnlockBits(smallData);
- return res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement