Using OpenCV and Python on the Raspberry Pi for simple object detection

I wrote some time ago about the robot which task it is to find a blue ball and move to it.
In this post I will give a little bit more information on how it detects the blue ball. The blue color helps us because we can filter image for blue parts and then only process those, but that is not all there is to it.

Detecting a simple object of a certain color is a very different task from detecting complex feature rich objects.  Here we can get away with a lot of simple and computationally inexpensive operations.

We will do this with roughly the following operations.

  1. Convert image from RGB to HSV.
  2. Filter the color blue from the HSV transformed image.
  3. Logically AND the filtered blue color with the original image then split the image channels because we only need the value channel.
  4. Apply erosion, which aids in removing artifacts and unwanted parts in the image.
  5. Find an enclosing circle for the part of the image that is left.

This is the base image for our example:


Original image


Now the operations in slightly more detail:

1. Convert the color image from RGB (should be what most web cams return) to HSV (Hue Saturation Value) format.
In our case HSV is the better format because it separates the color of the image from the intensity (read more about HSV here).

2. Now we create a mask which filters only the blue color.
The hue value can be between 90 and 130.
We also accept a range in saturation and value.

You can play a little with the values here. In some cases 110 in place of 90 may be working better for you.

3. Now that we have a mask that filters blue color we logically AND it with the original image. After this we have only the blue parts of the image left. Next we split the channels of the HSV image and keep only the value which gives us an gray scale image which we then blur.

Blurring the image may seem counterproductive but in comparison to the human vision system which has no trouble finding something in a noisy image the computer has a lot of trouble with noise in images (see also).


Blue filtered image

Saturation channel of blue filtered image

4. Next is the erosion and thresholding.
First we erode the image, this helps us to clear the image of the rest that is almost blue, or is also blue but not our main object.


Erosion with 1 iteration


Erosion with 2 iterations

We choose an ellipse as a structuring element with a size of 15×15 and one iteration. You can play around with these parameters. The images above are results from another source image with more blue parts in the image.

After the erosion we threshold it. This gives us a binary image (0 empty, 1 part of object).
We only need the binary image now because we only want to find shapes and the brightness, color, etc. is not needed.


Thresholded binary image

5. At this point we can try to find contours in our binary image.
However if you look at the code you will notice that we only try to find an enclosing circle and not a circular shape. This is done because it is the most robust method for the robot and changing surroundings.


Final result


This entry was posted in computer vision, Linux, python, raspberry pi. Bookmark the permalink.

Leave a Reply

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