Search code examples
pythonimage-processingcomputer-visionimage-segmentation

Segmenting multiple unfull ellipses from each other in an image


I have an image of multiple not fully drawn ellipses, and I am trying to separate each one from the other to further calculate the diameter of each one, so in other words to get another 3 images containing each ellipse, I want to make the algorithm know which one is the outer circle, middle circle and inner one.

Example of an image: enter image description here

I have tried detecting the outer circle only and having it as a reference for the other circles to be inside it but I didn't get it to fully work like that.

Also in a real image scenario (I can't share for confidentiality reasons), it will not have a clear background, rather it will have many random dots and noise.

I am not looking exactly for a code on how to do it, rather than different approaches and algorithms that can be used to do this.


Solution

  • Result:

    result

    Approach:

    • cv.threshold() with inversion flag, so the lines are white
    • extract cv.connectedComponents()
    • for each component
      • cv.findNonZero() to get a set of points
      • cv.fitEllipse() through that set of points

    The rest is cosmetics. I drew complete ellipses slightly offset to the inside and outside so we can see the original lines better.

    Here are the statistics:

    center: [944.5 707.4] - size: [1250.2  941.7] - angle: -0.0
    center: [943.3 707.1] - size: [855.8 645.4] - angle: -0.4
    center: [944.5 708.5] - size: [288.2 340.1] - angle: 0.0
    center: [945.3 708.8] - size: [858.5 647.4] - angle: -0.1
    

    This approach requires each arc to span a good fraction of the full ellipse. if it's not enough, the results are still plausible, but not "true".

    In this example, I erased the entire left half of the picture:

    left half erased