How to detect fast moving soccer ball with OpenCV, Python, and Raspberry Pi?
<p>This is the code. I am trying to detect different types of soccer ball using OpenCV and python. Soccer ball could of different colors. I can detect the ball if it is not moving. But, the code does not work if the ball is moving fast.</p>

<pre><code>from picamera.array import PiRGBArray
from picamera import PiCamerafrom picamera.array import PiRGBArray
from picamera import PiCamera
import time, cv2, sys, imutils, cv
import cv2.cv as cv

# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (400, 300)
camera.framerate = 30
rawCapture = PiRGBArray(camera, size=(400, 300))

for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
    image = frame.array
    img   = cv2.medianBlur(image, 3)
    imgg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    #imgg  = cv2.blur(imgg, (3,3))
                #imgg = cv2.dilate(imgg, np.ones((5, 5)))
                #imgg = cv2.GaussianBlur(imgg,(5,5),0)
    circles = cv2.HoughCircles(imgg, cv.CV_HOUGH_GRADIENT, 1, 20, param1=100, param2=40, minRadius=5, maxRadius=90)

    cv2.imshow("Frame", imgg)

    # clear the stream in preparation for the next frame
    rawCapture.truncate(0)

    key = cv2.waitKey(1) &amp; 0xFF    
    # if the `q` key was pressed, break from the loop
    if key == ord("q"):
        break

    if circles is None:
        continue        

    for i in circles[0,:]:
        cv2.circle(imgg,(i[0],i[1]),i[2],(0,255,0),1) # draw the outer circle
        cv2.circle(imgg,(i[0],i[1]),2,(0,0,255),3) # draw the center of the circle
        cv2.imshow("Framed", imgg)
</code></pre>

<p>May I suggest you read this post?</p>

<p><a href="http://www.pyimagesearch.com/2015/09/14/ball-tracking-with-opencv/" rel="nofollow">http://www.pyimagesearch.com/2015/09/14/ball-tracking-with-opencv/</a> </p>

<p>There are also a few comments below indicating how to detect multiple balls rather than one.</p>

python opencv raspberry-pi detection object-detection