OpenCV Ball Detection – Color Based


This is our first contribution about computer vision and image processing domain, so let us start with a simple example. In this sample, we talk about ball detection and tracking using simple color based. In particular, the red color would be detected. Firstly, we show you a interesting demo. Then we explain how we do that contains Preparation of Hardware Software, Implementation and explanation. The article is finished with some other applications using the same algorithm and the conclusion.


Preparation of Hardware and Software
  • 1 USB Camera
  • Ubuntu 14.04
  • OpenCV 2.4.11
  • C++ library, make, etc

Implementation and explanation

Capture the video from webcam:

    VideoCapture cap(0);

As you can imagine, this code use the camera connected with the computer. In case we have not any camera, so we can not use this example and this code will consider an error. And in case you have 2 cameras, this code uses the first camera. For sure which camera is the first, you need to test. 😀

Capture a temporary image from the camera:

    Mat imgTmp;;

Convert the captured frame from BGR to HSV:

	cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV);

For color detection, with our experience, the color HSV is easier to process and detect. So we convert is to HSV in our method.

Threshold the image

Mat imgThresholded;
inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgThresholded);

As the name suggest, iLowH, iLowS and iLowV are the minimum color range for H, S and V. And similar, iHighH, iHighS, iHighV are the maximum color range for H, S and V.

Morphological opening (removes small objects from the foreground):

	erode(imgThresholded, imgThresholded, getStructuringElement(CV_SHAPE_ELLIPSE, Size(3, 3) ));
	dilate( imgThresholded, imgThresholded, getStructuringElement(CV_SHAPE_ELLIPSE, Size(3, 3)) );

Morphological closing (removes small holes from the foreground)

	dilate( imgThresholded, imgThresholded, getStructuringElement(CV_SHAPE_ELLIPSE, Size(3, 3)) );
	erode(imgThresholded, imgThresholded, getStructuringElement(CV_SHAPE_ELLIPSE, Size(3, 3)) );

Calculate the moments of the thresholded image

	Moments oMoments = moments(imgThresholded);

	double dM01 = oMoments.m01;
	double dM10 = oMoments.m10;
	double dArea = oMoments.m00;

If the area >= 5000, I consider that the there are no object in the image and it’s because of the noise, the area is not zero:

	if (dArea > 5000)
	    //Calculate the position of the ball
	    int posX = dM10 / dArea;
	    int posY = dM01 / dArea;

	    if (iLastX >= 0 && iLastY >= 0 && posX >= 0 && posY > 0)
		//Draw a red line from the previous point to the current point
		line(imgLines, Point(posX, posY), Point(iLastX, iLastY), Scalar(0,0,255), 2);

	    iLastX = posX;
	    iLastY = posY;

Compile and run

     g++ -o ball_detection ball_detection.cpp -L/home/user_name/opencv-2.4.11/build/install/lib -lopencv_core -lopencv_imgproc -lopencv_highgui

You can also have a look on our Github

git clone

Other interesting applications

Video application this module for Parrot Jumping Sumo red ball following

For this application, we used the algorithm, but changed the architecture. And we use the client – server architecture. In particular, the image processing part runs on the server side and android phone + the robot provide the client side. With every frame, the client side send image to the server side. Then the image processing code runs on the server. Next the server returns the image processing result to the client side. The client side will process this result for control the robot. In particular, the result of this algorithm is the position of the red ball. So the Jumping Sumo drone follows the ball until the ball is in centre of drone’s camera. When the ball is out of the screen camera, drone will turn around for fetch ball.
We also applied this algorithm for Parrot Jumping Sumo drone line tracking:


OpenCV ball detection or OpenCV ball tracking are a topic simple in the image processing and computer vision domain. But I think with this example, you could easily understand how we will do a project for object detection and/or object tracking.
If you have any comment, question, suggestion, please leave here a comment. I will reply you as soon as possible.

Share This:

14 Replies to “OpenCV Ball Detection – Color Based”

  1. Betul

    I am also interested in similar thing. I wonder about how do you upload your ball tracking code to the jumping sumo? Does application consist of this feature? I also downloaded the application but I could not see any feature or addition like that.
    I will be really pleasure if you help me about that part!

    • admin

      Hi @Betul,
      This code is just for the algorithm. For apply on Jumping Sumo, I putted the code on my server with some modifications for architecture client – server.
      If you interest on this architecture, I could explain more to you?

      • Wago

        I am making an ad-hoc network of some jumping sumo and raspberry pi, and looking for sample applications on them.
        I’m very interested in your video too. Does it run on android? Needs other server?
        Would you have a plan to publish your codes shown in the video?
        Thanks in advance.

        • admin

          Hi @Wago,
          Yes, this demo needs another server.
          I use client-server architecture for this demo. The code I shared is running on my server and the android application will send request to my server.
          I will write a post for explain about my client-server architecture as soon as possible.

    • admin

      Hi @Keith Romero,
      Thanks for your sharing, I will write more.
      I am working in this domain and hope to share something to others. However, my site is just starting, so need improve so much.

  2. Beverly Garza

    I don’t even know how I finished up here, however I assumed this submit was good. I do not realize who you are but definitely you are going to a well-known blogger in the event you aren’t already 😉 Cheers!

  3. BrockHBaltes

    Excellent post. Keep writing such kind of info on your site.
    Im really impressed by your blog.
    Hey there, You’ve done an incredible job. I will definitely digg it and for my part suggest to
    my friends. I’m confident they will be benefited from this web site.

  4. SheilaYVella

    I blog quite often and I truly appreciate your
    content. This great article has truly peaked my
    interest. I’m going to book mark your blog and keep
    checking for new information about once per week. I subscribed to your Feed too.

  5. NinfaCMurphy

    Would you mind if I quote a few of your posts provided that
    I provide credit and sources to your site? My blog site is
    with the very same area of interest as yours and
    my visitors would really reap the benefits of several of the information you provide here.

    Please inform me if it alright with you. Thanks a whole lot!

Leave a Reply

Your email address will not be published. Required fields are marked *