C# Calculating area of circles in black-white image

Chris Spect 1 Reputation point

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?


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.


Thank YOU.

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
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Cheong00 3,471 Reputation points

    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.

    0 comments No comments

  2. Castorix31 81,346 Reputation points
    0 comments No comments

  3. Timon Yang-MSFT 9,571 Reputation points

    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.

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments