title: Find all circles in image
body: <p>I am new to python &amp; image processing. I am working on a hobby project in which I want to find ALL circles in image and then figure out which one have cross ('X') marked inside it. I have put some code together to find circles so far (below). It works on one image but fails to recognize all circles on another one. Please guide me how i can improve performance of find_circles algorithm.</p>
  
  <p>Test Image: </p>
  
  <p><img src="https://i.stack.imgur.com/EVNlt.png" alt="test image"></p>
  
  <p>Result Image: </p>
  
  <p><img src="https://i.stack.imgur.com/yePsV.png" alt="result image"></p>
  
  <pre><code>import cv2
  import cv
  import numpy as np
  import operator
  from PIL import Image
  
  def find_circles(img):
      im_gray = cv2.imread(img, cv2.CV_LOAD_IMAGE_GRAYSCALE)
      (thresh, im_bw) = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
      img_bw = cv2.threshold(im_gray, thresh, 255, cv2.THRESH_BINARY)[1]
      cv2.imwrite('img_bw.png',img_bw)
      rows, cols =img_bw.shape
      circles = cv2.HoughCircles(img_bw,cv.CV_HOUGH_GRADIENT,1,rows/32, param1=100,param2=40,minRadius=0,maxRadius=100)
      circles = np.uint16(np.around(circles))
      return circles
  
  def draw_circles(img, circles):
      img = cv2.imread(img,0)
      cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
      for i in circles[0,:]:
      # draw the outer circle
          cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
          # draw the center of the circle
          cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
          cv2.putText(cimg,str(i[0])+str(',')+str(i[1]), (i[0],i[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.4, 255)
      return cimg 
  
  def main():
      img = "query_circle9.png"
      circles = find_circles(img)
      img_circle = draw_circles(img,circles)
      cv2.imwrite('cricle.png',img_circle) 
  
  if __name__=='__main__':
      main()
  </code></pre>