bool IsPointValid(Point p, Color objectColor, UnsafeBitmap bmp, Rectangle boundRect, Point last) { if (!boundRect.Contains(p)) { return(false); //is the point in the image? } Color pCol = bmp.GetPixel(p.X, p.Y); if (CloseTo(pCol, objectColor)) { return(false); //is the point on the object? } if (p == last) { return(false); } if (PointNearby(p, bmp, boundRect, objectColor)) { return(true); //is the point next to the object? } return(false); //not next to the object, die. }
bool PointNearby(Point center, UnsafeBitmap bmp, Rectangle boundRect, Color objectColor) { Point p = new Point(center.X, center.Y - 1);//up if (boundRect.Contains(p)) if (CloseTo(bmp.GetPixel(p.X, p.Y), objectColor)) return true; p = new Point(center.X + 1, center.Y - 1);//upright if (boundRect.Contains(p)) if (CloseTo(bmp.GetPixel(p.X, p.Y), objectColor)) return true; p = new Point(center.X + 1, center.Y);//right if (boundRect.Contains(p)) if (CloseTo(bmp.GetPixel(p.X, p.Y), objectColor)) return true; p = new Point(center.X + 1, center.Y + 1);//downright if (boundRect.Contains(p)) if (CloseTo(bmp.GetPixel(p.X, p.Y), objectColor)) return true; p = new Point(center.X, center.Y + 1); //down if (boundRect.Contains(p)) if (CloseTo(bmp.GetPixel(p.X, p.Y), objectColor)) return true; p = new Point(center.X - 1, center.Y + 1); //downleft if (boundRect.Contains(p)) if (CloseTo(bmp.GetPixel(p.X, p.Y), objectColor)) return true; p = new Point(center.X - 1, center.Y); //left if (boundRect.Contains(p)) if (CloseTo(bmp.GetPixel(p.X, p.Y), objectColor)) return true; p = new Point(center.X - 1, center.Y - 1); //upleft if (boundRect.Contains(p)) if (CloseTo(bmp.GetPixel(p.X, p.Y), objectColor)) return true; return false; }
bool IsPointValid(Point p, Color objectColor, UnsafeBitmap bmp, Rectangle boundRect, Point last) { if (!boundRect.Contains(p)) return false; //is the point in the image? Color pCol = bmp.GetPixel(p.X, p.Y); if (CloseTo(pCol, objectColor)) return false; //is the point on the object? if (p == last) return false; if (PointNearby(p, bmp, boundRect, objectColor)) return true; //is the point next to the object? return false;//not next to the object, die. }
private void GetRectangles(Bitmap img, out List<Rectangle> rects, out UnsafeBitmap uBtm) { Rectangle boundRect = new Rectangle(0, 0, img.Width, img.Height); rects = new List<Rectangle>(); uBtm = new UnsafeBitmap(img); int w = img.Width; int h = img.Height; uBtm.LockBits(); List<Point> points = new List<Point>(); for (int i = 0; i < 1; i++) { for (int y = 0; y < h; y += 20) { for (int x = 0; x < w; x += 20) { Color c = uBtm.GetPixel(x, y); if (CloseTo(c, Color.FromArgb(0, 255, 0)) || CloseTo(c, Color.Blue) || CloseTo(c, Color.Red) || CloseTo(c, Color.Black)) { int newx = x; int newy = y; while (true) { newx = newx - 1; newy = newy - 1; Color newc = uBtm.GetPixel(newx, newy); if (!boundRect.Contains(newx, newy)) { break; } if (!CloseTo(newc, c)) { Point newp = new Point(newx, newy); bool pointExists = false; foreach (Rectangle r in rects) { if (r.Contains(newp)) { pointExists = true; break; } } if (pointExists) { break; } points = FindBorders(newp, c, uBtm); if (points.Count == 0) break; Point top, bottom, left, right; top = bottom = left = right = points[0]; foreach (Point p in points) { if (p.Y < top.Y) top = p; if (p.Y > bottom.Y) bottom = p; if (p.X < left.X) left = p; if (p.X > right.X) right = p; } Rectangle rect = new Rectangle(left.X, top.Y, right.X - left.X, bottom.Y - top.Y); rects.Add(rect); break; } } } } } } uBtm.UnlockBits(); }
bool PointNearby(Point center, UnsafeBitmap bmp, Rectangle boundRect, Color objectColor) { Point p = new Point(center.X, center.Y - 1);//up if (boundRect.Contains(p)) { if (CloseTo(bmp.GetPixel(p.X, p.Y), objectColor)) { return(true); } } p = new Point(center.X + 1, center.Y - 1);//upright if (boundRect.Contains(p)) { if (CloseTo(bmp.GetPixel(p.X, p.Y), objectColor)) { return(true); } } p = new Point(center.X + 1, center.Y);//right if (boundRect.Contains(p)) { if (CloseTo(bmp.GetPixel(p.X, p.Y), objectColor)) { return(true); } } p = new Point(center.X + 1, center.Y + 1);//downright if (boundRect.Contains(p)) { if (CloseTo(bmp.GetPixel(p.X, p.Y), objectColor)) { return(true); } } p = new Point(center.X, center.Y + 1); //down if (boundRect.Contains(p)) { if (CloseTo(bmp.GetPixel(p.X, p.Y), objectColor)) { return(true); } } p = new Point(center.X - 1, center.Y + 1); //downleft if (boundRect.Contains(p)) { if (CloseTo(bmp.GetPixel(p.X, p.Y), objectColor)) { return(true); } } p = new Point(center.X - 1, center.Y); //left if (boundRect.Contains(p)) { if (CloseTo(bmp.GetPixel(p.X, p.Y), objectColor)) { return(true); } } p = new Point(center.X - 1, center.Y - 1); //upleft if (boundRect.Contains(p)) { if (CloseTo(bmp.GetPixel(p.X, p.Y), objectColor)) { return(true); } } return(false); }
private void GetRectangles(Bitmap img, out List <Rectangle> rects, out UnsafeBitmap uBtm) { Rectangle boundRect = new Rectangle(0, 0, img.Width, img.Height); rects = new List <Rectangle>(); uBtm = new UnsafeBitmap(img); int w = img.Width; int h = img.Height; uBtm.LockBits(); List <Point> points = new List <Point>(); for (int i = 0; i < 1; i++) { for (int y = 0; y < h; y += 20) { for (int x = 0; x < w; x += 20) { Color c = uBtm.GetPixel(x, y); if (CloseTo(c, Color.FromArgb(0, 255, 0)) || CloseTo(c, Color.Blue) || CloseTo(c, Color.Red) || CloseTo(c, Color.Black)) { int newx = x; int newy = y; while (true) { newx = newx - 1; newy = newy - 1; Color newc = uBtm.GetPixel(newx, newy); if (!boundRect.Contains(newx, newy)) { break; } if (!CloseTo(newc, c)) { Point newp = new Point(newx, newy); bool pointExists = false; foreach (Rectangle r in rects) { if (r.Contains(newp)) { pointExists = true; break; } } if (pointExists) { break; } points = FindBorders(newp, c, uBtm); if (points.Count == 0) { break; } Point top, bottom, left, right; top = bottom = left = right = points[0]; foreach (Point p in points) { if (p.Y < top.Y) { top = p; } if (p.Y > bottom.Y) { bottom = p; } if (p.X < left.X) { left = p; } if (p.X > right.X) { right = p; } } Rectangle rect = new Rectangle(left.X, top.Y, right.X - left.X, bottom.Y - top.Y); rects.Add(rect); break; } } } } } } uBtm.UnlockBits(); }