Hi,

I have a problem. I have this image with various regions detected and coloured differntly. What I now need to do is, to find the mathematical center of each of these regions.

I have the code for this.

int labelnew = 2,centerx,centery;
            List<Point> l = new List<Point>();
            Point p1 = new Point();
            Point p2 = new Point();
            while (labelnew < label)
            {
                for (i = 1; i < bd.Height; i++)
                {
                    for (j = 1; j < bd.Width; j++)
                    {
                        if (I[j, i] == labelnew)
                        {
                            l.Add(p1);
                        }
                    }
                }
                l.Sort();
                p1 = l.ElementAt(0);
                p2 = l.ElementAt(l.Count - 1);
                centerx = (p1.X + p2.X) / 2;
                centery = (p1.Y + p2.Y) / 2;
                b.SetPixel(centerx, centery, Color.Red);
                l.Clear();
                ++labelnew;
            }

label contains the value for each region, starting from 2 and not 0. When I run the code, I get the following error :
Failed to compare two elements in the array
Can someone help me out n tell me what my mistake is

What is I[j, i] ? a point defined in the l lList? You cannot compare ints with Points if that is the case. Suggestion : Give more meaningfull names to your variables instead of I or l.

I have fixed the errors and got the problem solved. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.