Skip to content

Instantly share code, notes, and snippets.

@xamox
Created April 21, 2012 18:05
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save xamox/2438871 to your computer and use it in GitHub Desktop.
Save xamox/2438871 to your computer and use it in GitHub Desktop.
Object Tracking with SimpleCV (White Ball)
'''
This is how to track a white ball example using SimpleCV
The parameters may need to be adjusted to match the RGB color
of your object.
The demo video can be found at:
http://www.youtube.com/watch?v=jihxqg3kr-g
'''
print __doc__
import SimpleCV
display = SimpleCV.Display()
cam = SimpleCV.Camera()
normaldisplay = True
while display.isNotDone():
if display.mouseRight:
normaldisplay = not(normaldisplay)
print "Display Mode:", "Normal" if normaldisplay else "Segmented"
img = cam.getImage().flipHorizontal()
dist = img.colorDistance(SimpleCV.Color.BLACK).dilate(2)
segmented = dist.stretch(200,255)
blobs = segmented.findBlobs()
if blobs:
circles = blobs.filter([b.isCircle(0.2) for b in blobs])
if circles:
img.drawCircle((circles[-1].x, circles[-1].y), circles[-1].radius(),SimpleCV.Color.BLUE,3)
if normaldisplay:
img.show()
else:
segmented.show()
@marcviader
Copy link

First of all, many thanks for your example, very useful! :-)
I have only one doubt: listening to your video, I understand that circles[-1].x correspond to the bigger blob circle, is it right? so what exactly do/means circles[-1]? what is -1?
Many thanks again

Marc

@MarcoForte
Copy link

@marcviader circles[-1] refers to the last item in the circles list

@irtiq7
Copy link

irtiq7 commented Aug 27, 2014

Hi,

I recently got interested in computer vision. Regarding your code, how do I track multiple circles? So far your code only detects one circle.

@xamox
Copy link
Author

xamox commented Aug 27, 2014

Really just depends. This is a very simple example and maybe shouldn't even be considered tracking persay, but more detection like you mention. You could just change the line:

if circles:
circles.draw()

That will highlight all the circles found in the image that are white. If you want to say track frame to frame you probably want to keep a queue or list of the previous x,y values of all the circles found, then use something like scipy's euclidean distance (http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.spatial.distance.euclidean.html), to find the next closest circle in the frame. This is still pretty simple as you are assuming frame to frame it is has moved. There are also more "true" type tracking examples located in the simpleCV repo:
https://github.com/sightmachine/SimpleCV/tree/master/SimpleCV/examples/tracking

@ravalenc
Copy link

ravalenc commented Sep 9, 2014

When I run the code, I get an error that says "ImportError: No module named SimpleCV," and I already have SimpleCV installed on my RPi...any help or suggestions?

@nusrat-lima
Copy link

Thanks a lot dude... this was really helpful!!!! Please continue to share more things as such so we(the beginners) can from your experiences and codes!

@capcom-r
Copy link

This is awesome, thank you!!

@KhanradCoder
Copy link

how would you change the colour of the ball it is trying to track?

@judas79
Copy link

judas79 commented May 29, 2015

This is truly great, but my windows is lagging compared to your video. I'm going to try this on a PCDuino mini, with a wifi arduCAM once I recieve my order from China; it will be a while. I tried editing the line, as posted above and got a black screen:
if circles:
circles.draw()
img.drawCircle((circles[-1].x, circles[-1].y), circles[-1].radius(),SimpleCV.Color.BLUE,3)

I also tried it without the parameters with the same black screen result, can you please fill in what I have omitted?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment