title: How can i pythonically us opencv to find a a basketball in an image?
body: <p>I've been laboring on a pet project for a bit on how to find a simple basketball in an image. I've tried a bunch of permutations of using hough.circles and transform , etc for the last few weeks but I cant seem to come anywhere close to isolating the basketball with the code examples and my own tinkering.</p>
  
  <p>Here is an example photo:
  <img src="https://i.stack.imgur.com/NzXYo.jpg" alt="boy with a basketball">
  And here is the result after a simple version of circle finding code I've been tinkering with:
  <img src="https://i.stack.imgur.com/vSMZF.jpg" alt="houghcircle transform circle finding"></p>
  
  <p>Anyone have any idea where I have gone wrong and how I can get  it right?</p>
  
  <p>Here is the the code I am fiddling with:</p>
  
  <pre><code>import cv2
  import cv2.cv as cv # here
  import numpy as np
  
  def draw_circles(storage, output):
      circles = np.asarray(storage)
      for circle in circles:
          Radius, x, y = int(circle[0][3]), int(circle[0][0]), int(circle[0][4])
          cv.Circle(output, (x, y), 1, cv.CV_RGB(0, 255, 0), -1, 8, 0)
          cv.Circle(output, (x, y), Radius, cv.CV_RGB(255, 0, 0), 3, 8, 0)
  
   orig = cv.LoadImage('basket.jpg')
   processed = cv.LoadImage('basket.jpg',cv.CV_LOAD_IMAGE_GRAYSCALE)
   storage = cv.CreateMat(orig.width, 1, cv.CV_32FC3)
   #use canny, as HoughCircles seems to prefer ring like circles to filled ones.
   cv.Canny(processed, processed, 5, 70, 3)
   #smooth to reduce noise a bit more
   cv.Smooth(processed, processed, cv.CV_GAUSSIAN, 7, 7)
  
   cv.HoughCircles(processed, storage, cv.CV_HOUGH_GRADIENT, 2, 32.0, 30, 550)
   draw_circles(storage, orig)
  
   cv.imwrite('found_basketball.jpg',orig)
  </code></pre>