title: How to detect circular erosion/dilation
body: <p>I would like to detect circular erosions and dilations on a line. For dilations, I tried to recursively erode the image and on every recursion, I check width/height aspect ratio. If the ratio is smaller than 4, I assume that it the contour is circular and for each such contour I calculate circle center and radius from moments and area. This is the function that detects circular dilations:</p>
  
  <pre><code>def detect_circular_dilations(img, contours):
      contours_current, hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
      if len(contours_current) == 0:
          return get_circles_from_contours(contours)
      for c in contours_current:
          x, y, w, h = cv2.boundingRect(c)
          if w &gt; h:
              aspect_ratio = float(w) / h
          else:
              aspect_ratio = float(h) / w
          if aspect_ratio &lt; 4 and w &lt; 20 and h &lt; 20 and w &gt; 5 and h &gt; 5:
              contours.append(c)
      return detect_circular_dilations(cv2.erode(img, None, iterations=1), contours)
  </code></pre>
  
  <p>An example of circular dilations that I want to detect are the following:</p>
  
  <p><a href="https://i.stack.imgur.com/bNOTn.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bNOTn.jpg" alt="Circular Dilation"></a></p>
  
  <p>Another problem that I haven't solve is the detection of circular erosions. An example of circular erosion is the following:</p>
  
  <p><a href="https://i.stack.imgur.com/YuxDO.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YuxDO.jpg" alt="Circular Erosion"></a></p>
  
  <p>Here I've marked the circular erosion I would like to detect with red rectangle. There might be some smaller circular patterns (on the left) that shouldn't be treated as actual circular erosion.</p>
  
  <p>Does anyone know what is the best way to detect such circular shapes? For circular dilations, I would appreciate any comment/suggestion in order to potentially make detection more robust.</p>
  
  <p>Thank you!</p>