Skip to content
campbellsan edited this page Oct 25, 2014 · 40 revisions

PiTeR Home

PiTeR uses OpenCV for his vision system. OpenCV is a really powerful library of computer vision utilities which can run on Linux, Windows and Mac OSX. You can use OpenCV to make your robot do all kinds of extra-ordinary things. Find out much more about OpenCV here. Many of the examples found here were based on tutorials found on this site under the OpenCV Tutorials link.

To use OpenCV on your Raspberry Pi, you need to install the packages python-opencv and uv4l.

Userspace Video for Linux (uv4l) enables easy access to the Raspberry Pi camera. If you do a websearch, you will find other ways to access the camera from OpenCV, including some which have been highlighted on the Raspberry Pi Foundation blog. However in my opinion, using uv4l is by far the easiest way to get going. To install uv4l, follow the instructions found here:

http://www.linux-projects.org/modules/sections/index.php?op=viewarticle&artid=14

OpenCV itself can be installed using apt:

sudo apt-get update

sudo apt-get upgrade
sudo apt-get install python-opencv

The python programs which test PiTeRs visual system are found here in the repository. You don’t need a robot to try these programs for yourself, you just need a Pi camera or webcam.

What follows here are some notes that relate the programs themselves to an OpenCV article published in the MagPi magazine issue 26, November 2014.

imageCap.py - This program covers the first three steps covered in the article:

import cv2
cap = cv2.VideoCapture(-1)
success, frame = cap.read()
cv2.imshow(frame)

colour.py - This program displays six sliders to control the range of the colour values; Hue, Saturation and Value. We use these values rather than the more familiar Red, Green and Blue because they are more suited to setting up a colour range. Each colour value has a high and low which mark the ends of the range that will be accepted as the part of the image we’re looking for.

This program opens other windows, one shows the original view, another shows the result of applying the OpenCV inRange function and a third shows the final mask after noise reduction. The image capture and filtering happen inside a while loop, so that you can adjust the sliders and see the effect of your adjustments.

Note that the objective here is to locate a patch in the image, the mask does not have to be perfect. We're going to use this patch to crop the image further on down, and the symbol recognition will be done on the cropped part of the original image, so no detail will be lost.

patch.py - This program incorporates more of the concepts discussed in the article. It takes a set of fixed colour range values and outputs the image captured by the camera. If it finds a patch of colour which passes the filter (as illustrated by colour.py above), it uses the findContours and boundingRect functions discussed in the article to draw a green rectangle around the relevant area in the output image.

tracking.py - Finally, this program builds on the previous three examples to perform full symbol recognition. It uses detectAndCompute to find the key points in the image and the knnMatch function to compute a match and locate the symbol. If the match is good enough, the matched points are used to compute a transformation of a rectangle within which the symbol is located.

If everything worked perfectly, the transformed rectangle shown in blue should look as if it lies right on top of the symbol in the camera image, even if the symbol appears at an angle to the camera. It doesn’t always get it perfectly right, but it does so often enough to be impressive in my opinion. Try it! If you have trouble, try stronger lighting. Don't forget though that if you change the lighting, you will likely need to retune the HSV parameters.

The main robot control program PiTeR.py, will integrate these experimental programs and use their output to control PiTeR to autonomously find and visit each symbol in a trail. This will appear in GitHub as I complete the integration work ahead of the cub scout meeting in November.

Once that is complete, I hope to post some pictures here, so watch this space!

Wish me luck! :-)

PiTeR Home