Troubleshooting Simple Algorithm for Image Extraction in App | Help Needed

  • Thread starter Superposed_Cat
  • Start date
  • Tags
    Algorithm
In summary, the conversation is about a person working on an app that requires a subroutine to extract 'blobs' from images by separating black shapes on a white background. They were having trouble with steps 1-3 not working and asked for help. The code provided shows the use of a flood fill function to achieve this task. The issue was found to be an error in the code, which was corrected.
  • #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.

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
  • #2
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,
 
  • #3
"LexicalAnalyzer.FloodFill((Bitmap)b1, 0, 0, Binarize.colors);"
Found my problem, should be "i,j" not "0,0". Apologies.
 

Related to Troubleshooting Simple Algorithm for Image Extraction in App | Help Needed

1. Why is my simple algorithm not producing the expected output?

There could be several reasons why your algorithm is not working. Some common causes include logical errors in the code, incorrect use of syntax, or missing variables. It is important to carefully review your code and check for any mistakes.

2. Can a simple algorithm be optimized for better performance?

Yes, a simple algorithm can often be optimized for better performance. This can be done by improving the efficiency of the code, reducing the number of steps needed to complete the algorithm, or using more efficient data structures. However, it is important to consider the trade-off between performance and readability of the code.

3. How can I debug my simple algorithm?

To debug your simple algorithm, you can use various tools such as a debugger, print statements, or logging functions. These can help you track the flow of your code and identify any errors or unexpected behaviors. It is also helpful to break down your algorithm into smaller parts and test each part separately.

4. Can I use a different programming language for my simple algorithm?

Yes, you can use any programming language to create a simple algorithm. However, some languages may be better suited for certain types of algorithms. It is important to choose a language that you are familiar with and that has the necessary features for your algorithm.

5. Are there any resources available to help me create a simple algorithm?

Yes, there are many online resources, tutorials, and coding communities that can help you create a simple algorithm. You can also refer to textbooks or seek guidance from experienced programmers. It is important to thoroughly understand the problem and plan your algorithm before starting to code.

Similar threads

  • Programming and Computer Science
Replies
1
Views
885
  • Programming and Computer Science
Replies
9
Views
1K
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
4
Views
946
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
3
Views
2K
  • Astronomy and Astrophysics
Replies
2
Views
1K
Replies
8
Views
4K
Back
Top