Se puede usar esta simple algoritmo, basado en la idea de las inundaciones llenar de mapa de bits ayudante:
// backColor is an INT representation of color at fillPoint in the beginning.
// result in pixels of enclosed shape.
private int GetFillSize(Bitmap b, Point fillPoint)
{
int count = 0;
Point p;
Stack pixels = new Stack();
var backColor = b.GetPixel(fillPoint.X, fillPoint.Y);
pixels.Push(fillPoint);
while (pixels.Count != 0)
{
count++;
p = (Point)pixels.Pop();
b.SetPixel(p.X, p.Y, backColor);
if (b.GetPixel(p.X - 1, p.Y).ToArgb() == backColor)
pixels.Push(new Point(p.X - 1, p.Y));
if (b.GetPixel(p.X, p.Y - 1).ToArgb() == backColor)
pixels.Push(new Point(p.X, p.Y - 1));
if (b.GetPixel(p.X + 1, p.Y).ToArgb() == backColor)
pixels.Push(new Point(p.X + 1, p.Y));
if (b.GetPixel(p.X, p.Y + 1).ToArgb() == backColor)
pixels.Push(new Point(p.X, p.Y + 1));
}
return count;
}
ACTUALIZACIÓN
El código anterior sólo funciona esta áreas cerradas vinculados cuádruplemente. El siguiente código funciona con áreas cerradas vinculadas con octuply.
// offset points initialization.
Point[] Offsets = new Point[]
{
new Point(-1, -1),
new Point(-0, -1),
new Point(+1, -1),
new Point(+1, -0),
new Point(+1, +1),
new Point(+0, +1),
new Point(-1, +1),
new Point(-1, +0),
};
...
private int Fill(Bitmap b, Point fillPoint)
{
int count = 0;
Point p;
Stack<Point> pixels = new Stack<Point>();
var backColor = b.GetPixel(fillPoint.X, fillPoint.Y).ToArgb();
pixels.Push(fillPoint);
while (pixels.Count != 0)
{
count++;
p = (Point)pixels.Pop();
b.SetPixel(p.X, p.Y, Color.FromArgb(backColor));
foreach (var offset in Offsets)
if (b.GetPixel(p.X + offset.X, p.Y + offset.Y).ToArgb() == backColor)
pixels.Push(new Point(p.X + offset.X, p.Y + offset.Y));
}
return count;
}
La imagen siguiente demuestra claramente lo que quiero decir. También se podrían agregar más puntos lejanos para compensar la matriz con el fin de poder llenar áreas con espacios.
[comenzar con la fórmula para encontrar el área de un polígono] (http://en.wikipedia.org/wiki/Polygon#Area_and_centroid) – Servy
pensar en píxeles que encierran sus regiones como de los polígonos a continuación, ver http://stackoverflow.com/questions/2034540/calculating-area-of-irregular-polygon-in-c-sharp sobre cómo obtener el área del polígono – m0s
Lo siento, algo se perdió en la traducción ... Tengo que descubrir el número de Regiones (5) no el área de ellas. – user873432