Introduction: Raspberry Pi Ball Tracking

Picture of Raspberry Pi Ball Tracking

(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

Instructions:

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)

Picture of 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

Picture of 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

Comments

MrLunk made it! (author)2017-04-10

Cool thanks, works on Raspi 3b

video proof :)

MrLunk (author)MrLunk2017-04-28

Can anyone tell me how to make this script work in OpenCV 3.0 ?

SajjadK11 (author)2017-02-03

hello man, thanks for sharing knowledge.

how many frames per seconds could it take, i.e. what is the maximum speed of motion that could be captured.

alirezag3 made it! (author)2016-08-05

Very simple and great code... i wonder if its possible to filter with RGB channel instead of HSL ... is there any code for that?

amey_s (author)alirezag32017-01-16

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.

If you are interested more read up on following:
http://stackoverflow.com/questions/16472716/why-is-color-segmentation-easier-on-hsv

and

https://en.m.wikipedia.org/wiki/Color_model

Traceback (most recent call last):

File "C:\Users\Apisit\Desktop\python\bdtct.py", line 2, in <module>

import cv2.cv as cv

ImportError: No module named cv

That probably means you don't have OpenCV installed on your pi.

noorthamer (author)2016-11-28

thank you very much
i want a tutorial steps please
can you help me ?

CjN2 (author)2015-12-25

what did you use in to simulate it

AditiS3 (author)2015-04-14

Wow. Thank you for the instructions , it really helped me in completing me final year Engineering project . :)

About This Instructable

43,367views

90favorites

License:

More by amey_s:Raspberry Pi Ball trackingsnapshots
Add instructable to: