- #1
Superposed_Cat
- 388
- 5
Hey all, I'm currently working on an app that requires the a sub routine to extract 'blob"s from images, ie if I have multiple black shapes on a white background split them into separate images. Google turned up nothing, my soultion was to
1. flood fill pixel(0,0) with color[0]
2.add color to used color list
3. iterate over all pixels, if currently pixels's color isn't in the used array then flood fill that and add that colro to the used array.
Then after this id separate the images by color, make sense? Problem, steps 1-3 arn't working.
Anyone see any issues? When the code runs after i=8 j=63 it's used 23 colors, and there are only 17 shapes in the image, if i pause the code there the background is purple (color 23) and the other shapes are still black. Any help apreciated.
1. flood fill pixel(0,0) with color[0]
2.add color to used color list
3. iterate over all pixels, if currently pixels's color isn't in the used array then flood fill that and add that colro to the used array.
Then after this id separate the images by color, make sense? Problem, steps 1-3 arn't working.
Code:
List<Color> used=new List<Color>();
Bitmap b1= (Bitmap)pictureBox1.Image;
int u = 0;
LexicalAnalyzer.FloodFill((Bitmap)b1, 0, 0, Binarize.colors[0]);
used.Add(Binarize.colors[0]);
for (int i = 0; i < b1.Width; i++)
{
for (int j = 0; j < b1.Height; j++)
{
if (!In(used.ToArray(), b1.GetPixel(i, j)))
{
u++;
LexicalAnalyzer.FloodFill((Bitmap)b1, 0, 0, Binarize.colors[u]);
Thread.Sleep(100);
used.Add(Binarize.colors[u]);
}
}
}
pictureBox1.Image = b1;
}
bool In(Color[] y, Color c)
{
for (int i = 0; i < y.Length; i++)
{
if (c.R != y[i].R) return false;
if (c.G != y[i].G) return false;
if (c.B != y[i].B) return false;
}
return true;
}
Anyone see any issues? When the code runs after i=8 j=63 it's used 23 colors, and there are only 17 shapes in the image, if i pause the code there the background is purple (color 23) and the other shapes are still black. Any help apreciated.