Troubleshooting Simple Algorithm for Image Extraction in App | Help Needed

  • Thread starter Thread starter Superposed_Cat
  • Start date Start date
  • Tags Tags
    Algorithm
AI Thread Summary
The discussion revolves around troubleshooting an algorithm for extracting shapes from images using a flood fill method. The user initially implemented a routine to separate black shapes on a white background but encountered issues with color tracking, resulting in incorrect shape identification. After running the code, it was found that the flood fill was incorrectly initiated at a fixed point (0,0) instead of the current pixel coordinates (i,j). This mistake led to an excessive number of colors being used, despite having fewer shapes. The user resolved the issue by correcting the flood fill parameters, demonstrating the importance of precise coordinate handling in image processing algorithms.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top