(Note: This is an advanced tutorial, it is not intended for a linux beginner.)

In this tutorial, I will demonstrate how to track table tennis balls using OpenCV on Raspberry Pi. It can be used to track any circular object as long as it can be detected well from its background. This can be helpful in ball tracking robots and similar projects.

Things you need:

1. Raspberry Pi (with latest Raspbian installed, you can get it from here: https://www.raspberrypi.org/downloads/ Follow installation instructions on the same site)

2. USB Webcam (test one that Rasppi supports)

3. A linux host (preferably Linux Mint/Ububtu ) I have tested with Linux Mint 17.

4. Experience with debian systems

Step 1: Initial Setup


1. Make Rpi connections: Keyboard, Mouse, Webcam, ethernet or wifi dongle, Display via HDMI, Power

2. Power on your Rpi

3. Boot into raspbian GUI on your Rpi (Raspbian boot to GUI)

4. Open terminal and install OpenCV (Follow Trevor Appleton's excellent guide to verify that OpenCV is installed correctly Install OpenCV on Rpi)

5. Copy the file "bdtct.py" to home folder in your rpi either by using scp or by using a flash drive.

Step 2: Run the Code (a.k.a the Fun Part)

1. In your rpi's terminal navigate to folder where you copied bdtct.py

2. Run the following command:

sudo python bdctc.py

That should open 5 windows like in above picture

3. Bring a table tennis ball(use a yellow one if possible) in front of your webcam.

5. The ball should be tracked in the window "tracking". If not, adjust the sliders in the windows "HueComp", "SatComp", "ValComp" respectively such that only the region of the table tennis ball appears white in the "closing" window (See the above picture for reference). You may need to experiment a little to get this working. Note down the values of the sliders for which it works for you, you can later edit them in bdtct.py .

Step 3: Understanding the Algorithm

Open the bdtct.py file in text editor.

The bdtct.py code is doing following:

1. Taking in the input by "cap = cv2.VideoCapture(-1)"

2. Re-sizing the video frame to smaller size of 320x240, so that our rpi can put out more frames per second.

3. Creating visual elements like windows "HueComp", "SatComp", "ValComp" with respective min and max sliders.

4. Converting the input from BGR to HSV system "hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)"

5. Splitting the hue, saturation and value components.

6. Thresholding each component according to threshold range defined by respective min and max sliders to obtain a binary thresholded image (see above picture)

7. Logically AND the thresholded hue, saturarion, value components together to get a rough binary image in which only the table tennis ball's pixels are white, rest everything is black. (see above picture)

8. Smoothing the ANDed image "closing = cv2.GaussianBlur(closing,(5,5),0)"

9. Using HoughCircles to detect circles in the image

10. Drawing detected circles on the original input frame.

Step 4: Conclusion

If you are on this page that means everything worked well. Now get yourself a cup of coffee. :D

<p>Cool thanks, works on Raspi 3b</p><p>video proof :)<br><iframe allowfullscreen="" frameborder="0" height="281" src="//www.youtube.com/embed/XFwi3xpphCs" width="500"></iframe></p>
<p>hello man, thanks for sharing knowledge.</p><p>how many frames per seconds could it take, i.e. what is the maximum speed of motion that could be captured.</p>
<p>Very simple and great code... i wonder if its possible to filter with RGB channel instead of HSL ... is there any code for that?</p>
Great to see that you got the code working. Yes, it is definitely possible to use a RGB model for the segmentation. However, one key advantage of HSV is that it separates color(HS) and luminance(V), which makes the segmentation easier to tune for humans.<br><br>If you are interested more read up on following:<br>http://stackoverflow.com/questions/16472716/why-is-color-segmentation-easier-on-hsv<br><br>and<br><br>https://en.m.wikipedia.org/wiki/Color_model
<p>Traceback (most recent call last):</p><p> File &quot;C:\Users\Apisit\Desktop\python\bdtct.py&quot;, line 2, in &lt;module&gt;</p><p> import cv2.cv as cv</p><p>ImportError: No module named cv</p>
<p>That probably means you don't have OpenCV installed on your pi.</p>
<p>thank you very much <br>i want a tutorial steps please <br>can you help me ?<br></p>
<p>what did you use in to simulate it </p>
Wow. Thank you for the instructions , it really helped me in completing me final year Engineering project . :)

About This Instructable




More by amey_s:Raspberry Pi Ball tracking snapshots 
Add instructable to: