Troubleshooting Simple Algorithm for Image Extraction in App | Help Needed

  • Thread starter Thread starter Superposed_Cat
  • Start date Start date
  • Tags Tags
    Algorithm
Click For Summary
SUMMARY

The forum discussion centers on troubleshooting a flood fill algorithm for image extraction in a C# application. The user initially implemented a method to separate black shapes from a white background using a flood fill technique. The issue arose when the algorithm incorrectly identified colors, leading to more colors being used than expected. The user identified the problem as using fixed coordinates (0,0) instead of the current pixel coordinates (i,j) in the flood fill function, which resolved the issue.

PREREQUISITES
  • Understanding of C# programming language
  • Familiarity with image processing concepts
  • Knowledge of Bitmap manipulation in .NET
  • Experience with color representation in digital images
NEXT STEPS
  • Explore advanced image processing techniques using OpenCV for C#
  • Learn about the A* algorithm for more efficient pathfinding in image segmentation
  • Investigate multi-threading in C# to optimize image processing tasks
  • Study the implementation of different flood fill algorithms and their performance impacts
USEFUL FOR

Software developers, particularly those working on image processing applications, graphic designers involved in automated image manipulation, and anyone interested in optimizing flood fill algorithms in C#.

Superposed_Cat
Messages
388
Reaction score
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.

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.
 
Technology news on Phys.org
Code:
  public static void FloodFill(Bitmap bitmap, int x, int y, Color color)
        {
            BitmapData data = bitmap.LockBits(
                new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            int[] bits = new int[data.Stride / 4 * data.Height];
            Marshal.Copy(data.Scan0, bits, 0, bits.Length);

            LinkedList<Point> check = new LinkedList<Point>();
            int floodTo = color.ToArgb();
            int floodFrom = bits[x + y * data.Stride / 4];
            bits[x + y * data.Stride / 4] = floodTo;

            if (floodFrom != floodTo)
            {
                check.AddLast(new Point(x, y));
                while (check.Count > 0)
                {
                    Point cur = check.First.Value;
                    check.RemoveFirst();

                    foreach (Point off in new Point[] {
                new Point(0, -1), new Point(0, 1),
                new Point(-1, 0), new Point(1, 0)})
                    {
                        Point next = new Point(cur.X + off.X, cur.Y + off.Y);
                        if (next.X >= 0 && next.Y >= 0 &&
                            next.X < data.Width &&
                            next.Y < data.Height)
                        {
                            if (bits[next.X + next.Y * data.Stride / 4] == floodFrom)
                            {
                                check.AddLast(next);
                                bits[next.X + next.Y * data.Stride / 4] = floodTo;
                            }
                        }
                    }
                }
            }

            Marshal.Copy(bits, 0, data.Scan0, bits.Length);
            bitmap.UnlockBits(data);
        }
That's the flood fill,
 
"LexicalAnalyzer.FloodFill((Bitmap)b1, 0, 0, Binarize.colors);"
Found my problem, should be "i,j" not "0,0". Apologies.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 75 ·
3
Replies
75
Views
7K
Replies
8
Views
5K
Replies
1
Views
14K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K