# C# Calculating area of circles in black-white image

1 Reputation point
2021-06-22T20:38:35.577+00:00

I have a question about image processing. I get an output similar to the picture in the example. I want to calculate the area of the white areas here. I want to sort the flats by size. How can I calculate areas? ( No libiary)

I have questions about image processing. Given an area with multiple non-intersecting circles (see image link), how can I obtains a list of circles sorted by size?

Specifically:

How can I identify circle locations?
How can I identify circle sizes?
How can I sort the list of circles identified?
I need a programmatic solution in C#, without the use of 3rd-party libraries.

EXAMPLE IMAGE

Thank YOU.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,165 questions

1. 3,471 Reputation points
2021-06-23T04:25:14.923+00:00

The following is how I would attack this problem. Not optimized approach.

From top to bottom, scan for pixels with white color and "pixel above in black".

If the pixel is surrounded by black pixels, then these are "1 pixel circles", and you can put it into array.

If the pixel do not have consecutive white pixel at the side, this is the "north-most pixel".

If the multiple pixels on the row are white, the middle one is "north-most pixel".

Once "north-most pixel" is found, scan directly below it for the first pixel that is white and "pixel below is black", that would be "south-most pixel".

The middle point of "north-most pixel" and "south-most pixel" is the center of the circle. The distance between center and either point is the radius. Now put it into array.

Once you found all the "circles" you can proceed to sort it and calculate area, etc.

2. 81,346 Reputation points
2021-06-23T05:51:01.85+00:00

3. 9,571 Reputation points
2021-06-23T06:50:03.833+00:00

I'm a little curious why you don't want to use other packages.

When I used the code of Wolfgang Radl in this link for testing, it basically worked, but each circle was recognized as two, which may be caused by severe jagged edges.

I added a line to his code to output the area:

``````            for (int i = 0; i < contours.Size; i++)
{
Ellipse ellipse = new Ellipse(CvInvoke.FitEllipse(contours[i]));
result.Draw(ellipse, new Bgr(Color.Red), 1);

Console.WriteLine(ellipse.RotatedRect.Center.X * ellipse.RotatedRect.Center.Y *Math.PI);
}
``````

Of course, if it is for learning purposes, it makes sense not to use other packages but to do it ourselves.