I’ve found a couple of examples that you might find helpful.
Take a look at:
The first link will not give a code to do specifically what you want but rather an example of how to approach object detection programming. The second link has lots of documentation on OpenCV which you might find helpful. http://www.intorobotics.com/how-to-detect-and-track-object-with-opencv/
There you’ll find examples of code used to track color such as your application and others that might help you get an idea of how you should start building your code.
Intel Galileo Gen 2 Eurobot videos :-
i have done object detection using colors the issue is that if an object is far from the camera its appearing as black.Also do u have any tutorial on differtiating object on bases of shape like a cup,tennis ball,golf ball etc.