title: houghcircles - finding parameters
body: <p>Is there a manner in which to make circle detection parameter-less? - And why do the parameters in <code>cv2.HoughCircles()</code> not get affected sometimes after changing them?  I  have a for loop to alter parameters but it will not affect the outcome all of the time.</p>
  <p>It seems that Hough Circles requires a lot of parameter optimization by the user, which is an awful problem if the user is wanting to use the system in a very generalized manner. Therefore, I am attempting to create an algorithm to find the best parameters for opencv's houghcirlces function automatically.
  The houghcirlces function at the moment takes in a maximum and minimum radius for circles that it will find in an image with some thresholds for how good of a match the circles are that are found.</p>
  <h2>My hope is to split up different ranges for minimum and maximum radii to search within, then to use the found circles across each section to what parameters I should use for the final houghcircles transform.  (If anyone has a better approach to this I would love to hear it.)</h2>
  <p>However I ran into this problem:</p>
  <h2><strong>It doesn't appear that the minimum and maximum radii parameters, or the Canny and Hough space thresholds are being respected</strong>.</h2>
  <hr />
  <p>For example, we may take this image:</p>
  <p><a href="https://i.stack.imgur.com/OyJk7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OyJk7.png" alt="circles" /></a><br />
  <sub>(source: <a href="https://precollegiate.stanford.edu/circle/math/logoNew12circles.png" rel="nofollow noreferrer">stanford.edu</a>)</sub></p>
  <p>and run the following code I have on it:</p>
  <pre><code>import numpy as np
  import cv2
  
  file_path = '1.png'
  img = cv2.imread(file_path)
  gray = cv2.imread(file_path, cv2.IMREAD_GRAYSCALE)
  # Begin using the largest dimesion of the image as the largest possible radius
  largest_dimension = np.max([gray.shape[0], gray.shape[1]])
  for i in range(1,100):
      # For the first iteration we use the largest possible radius
      if i == 1:
          maxRadius = largest_dimension
      # THe next iterations we use what the minimum radius was last iteration 
      else:
          maxRadius = minRadius
      # Reduce the size by 2 everytime
      minRadius = maxRadius/2
      dp = 1 # Inverse ratio of thresholded image resolution ?? Not sure what to do with this
      minDist = minRadius # Not likely to be much closer than the minimum radius...?
      param1 = 200 # Canny edge threshold - image OTSU threshold dependence ?? 
      param2 = 100 # Hough space threshold - radius dependance?  np.pi*maxRadius**2 ??
      circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, dp, minDist, param1, param2, minRadius, maxRadius)
  
      # Display some inforation about the results
      if circles is not None:
          img_copy = img.copy()
          radii = np.array([c[2] for c in circles[0]])
          num = len(circles[0])
          avg = np.mean(radii)
          print str(num) + ' found: avg radius: ' + str(avg)
          if any(radii &lt; minRadius) or any(radii &gt; maxRadius):
              print 'The minimum and maximum radii given are not being respected' 
          for c in circles[0]:
              cv2.circle(img_copy, (c[0],c[1]), c[2], (255,0,255),2)
  
          cv2.namedWindow('', cv2.WINDOW_NORMAL)
          cv2.imshow('',img_copy)
          cv2.waitKey(0)
  </code></pre>
  <p>We will get the output shown below and that radii are not respected by the function everytime.  Also, if we change param1 and param2 (Canny edge detection and Hough space thresholds) from 3 to 6000 almost nothing changes.</p>
  <p>Clearly the image shown below has detected far more circles than any person typically would, and it only gets worse.</p>
  <p><img src="https://i.imgur.com/CY2NFzV.png" alt="cicrlces" /></p>
  <p>I may mention that the images I would like to use this script on in the future will appear as something such as this:</p>
  <p><img src="https://i.imgur.com/RkD49bz.png" alt="" />
  <img src="https://i.imgur.com/Ehyfmhl.png" alt="" />
  <img src="https://i.imgur.com/Oc1NE5k.png" alt="" />
  <img src="https://i.imgur.com/8IQCpir.png" alt="" /></p>