static void detectObj(Bitmap im, List<AnalyzedObject> objects) { bool[,] markMat = new bool[im.Width, im.Height]; for (int i = 0; i < im.Width; ++i) { for (int j = 0; j < im.Height; ++j) { Color c = im.GetPixel(i, j); if (c.B == 0 && c.R == 0 && c.G == 0) { continue; } // we are on a non white pixel BoundingBox bBox = new BoundingBox(); bBox.topLeft.x = 10000; bBox.topLeft.y = 10000; bBox.bottomRight.x = 0; bBox.bottomRight.y = 0; ColorStat cSum = new ColorStat(); ColorStat cSum2 = new ColorStat(); ObjPoint coordSum = new ObjPoint(); int depth = 0; int size = getObj(im, i, j, markMat, depth, c, bBox, cSum, cSum2, coordSum); if (size == 0) continue; AnalyzedObject obj = new AnalyzedObject(); obj.decision = true; obj.leftTop.x = i; obj.leftTop.y = j; obj.size = size; obj.color = c; obj.bBox = bBox; cSum.R /= size; cSum.G /= size; cSum.B /= size; cSum2.R = Math.Sqrt(cSum2.R / size - cSum.R * cSum.R); cSum2.G = Math.Sqrt(cSum2.G / size - cSum.G * cSum.G); cSum2.B = Math.Sqrt(cSum2.B / size - cSum.B * cSum.B); obj.colorAv = cSum; obj.colorStdev = cSum2; coordSum.x = (int)(coordSum.x / size); coordSum.y = (int)(coordSum.y / size); obj.centroid = coordSum; objects.Add(obj); } } }
static int getObj(Bitmap im, int x, int y, bool[,] markMat, int depth, Color c, BoundingBox bBox, ColorStat cSum, ColorStat cSum2, ObjPoint coordSum) { if (depth > 3000) return 1; if (y < 0 || y >= im.Height || x < 0 || x >= im.Width) return 0; if (markMat[x, y]) return 0; Color thisP = im.GetPixel(x, y); if (thisP.R != c.R || thisP.G != c.G || thisP.B != c.B) return 0; // new object ++depth; markMat[x, y] = true; int size = 1; // set bounding box if (x < bBox.topLeft.x) bBox.topLeft.x = x; if (x > bBox.bottomRight.x) bBox.bottomRight.x = x; if (y < bBox.topLeft.y) bBox.topLeft.y = y; if (y > bBox.bottomRight.y) bBox.bottomRight.y = y; // set sums cSum.R += thisP.R; cSum.G += thisP.G; cSum.B += thisP.B; cSum2.R += thisP.R * thisP.R; cSum2.G += thisP.G * thisP.G; cSum2.B += thisP.B * thisP.B; coordSum.x += x; coordSum.y += y; for (int i = x - 1; i <= x + 1; ++i) { for (int j = y - 1; j <= y + 1; ++j) { size += getObj(im, i, j, markMat, depth, c, bBox, cSum, cSum2, coordSum); } } return size; }
public AnalyzedObject createInitObj() { BoundingBox bBox = new BoundingBox(); bBox.topLeft.x = 10000; bBox.topLeft.y = 10000; bBox.bottomRight.x = 0; bBox.bottomRight.y = 0; AnalyzedObject obj = new AnalyzedObject(); obj.decision = true; //obj.leftTop.x = i; //obj.leftTop.y = j; //obj.size = size; //obj.color = c; obj.bBox = bBox; return obj; }