2

I would like to find the "inner" circle of a countour calculated in CV2.
Lets assume I have found a contour like the red line in this picture:

Circles

It is close to a perfect circle, but in the top-right area it has a "defect". I want to find that defect within that contour. So I can find the min. enclosing circe around that contour via cv2.minEnclosingCircle(contour), which results in the blue circle. Now it would be great to also have something like the "opposite", something like the "max. circle within the contour", i.e. the green circle in the image above. Any ideas how to achieve this?

Or is there maybe an easier way to identify a problematic area in the original contour, the one that makes the circle non-perfect?

Matthias
  • 9,817
  • 14
  • 66
  • 125
  • 1
    If most of the contour IS a circle you could use houghCircle or a ransac circle detection and conpare it to the contour itself, e.g. compute hausdorff distance ir per-pixel distance. If you really want something like biggest inner circle for any shape you can give distanceTransform a try, where the circle center will be the point with biggest distance inside the contour value and the radius is the distance value. – Micka Dec 06 '21 at 20:20
  • Here is an example of a ransac circle detection. You can find more variants in some other answers https://stackoverflow.com/a/20734263/2393191 – Micka Dec 06 '21 at 20:22
  • You may use `cv2.findContours()` with `cv2.RETR_TREE`, and get the [hierarchy](https://docs.opencv.org/4.x/d9/d8b/tutorial_py_contours_hierarchy.html) of the outer contours and the inner contours. – Rotem Dec 06 '21 at 21:08
  • Put a small green circle inside the contour and expand it until it touches the contour. Put a big red circle outside the contour and shrink it until it touches the contour. Some judicious adjustment of the centres of the red and green circles will help, basically moving the centre away (green) or towards (red) the point of first contact with the contour. – rossum Dec 07 '21 at 13:30

1 Answers1

0

Ok for the records: I ended up using this approach to calculate the center of a circle with three points from that circle. I do this three times, every times with 120 degrees rotated, so that even if I would "hit" the broken part of the circle once, I do get the correct circle center two out of three times. Then I take those two results that are the same and I am done.

Matthias
  • 9,817
  • 14
  • 66
  • 125