public static Point SmartFindBitmap(MyRawBitmapData src, Utils.RECT rect, MyRawBitmapData pattern, int step = 5, double error1 = 10000, double error2 = 100000) { List <Point> lst = GetSubPositions(src, pattern, rect, step, error1); if (lst.Count > 0) { return(lst[0]); } return(new Point(0, 0)); }
public static List <Point> GetSubPositions(MyRawBitmapData main, MyRawBitmapData sub, Utils.RECT rect, int step = 1, double colorError = 1, bool flgSingle = true) { List <Point> possiblepos = new List <Point>(); int subwidth = sub.width; int subheight = sub.height; int movewidth = rect.right - subwidth; int moveheight = rect.bottom - subheight; int maxX = rect.left + movewidth - subwidth; int maxY = rect.top + moveheight - subheight; int cX = subwidth / 2; int cY = subheight / 2; MyColor firstColor = sub.GetColor(0, 0); MyColor CenterColor = sub.GetColor(cX, cY); for (int y = rect.top; y < moveheight; ++y) { for (int x = rect.left; x < movewidth; ++x) { MyColor curcolor = main.GetColor(x, y); if (possiblepos.Count > 0) { foreach (var item in possiblepos.ToArray()) { int xsub = x - item.X; int ysub = y - item.Y; if (xsub >= subwidth || ysub >= subheight || xsub < 0) { continue; } MyColor subcolor = sub.GetColor(xsub, ysub); if (!curcolor.nearColor(subcolor, colorError)) { possiblepos.Remove(item); } } } // add part // we should not add pixel that will out of bound if (x > maxX) { continue; } if (y > maxY) { continue; } // add if first pixel match if (curcolor.nearColor(firstColor, colorError)) { if (CenterColor.nearColor(main.GetColor(x + cX, y + cY), colorError)) { possiblepos.Add(new Point(x, y)); } } } if (flgSingle && (possiblepos.Count > 0)) { if (y - subheight > possiblepos[0].Y) { //Console.WriteLine("found break"); break; } } //Console.WriteLine("Y=" + y + " Count=" + possiblepos.Count); } return(possiblepos); }