Roomba Finding - Pitt-RAS/iarc7_common GitHub Wiki

Overview of current usage and application

To date, we have been using OpenCV 2.4.13's version of the General Hough Transform (as the Jetson TX2 has a number of optimizations for that specific version of OpenCV. For details, check out eLinux). However, we will be moving to version 3, since we haven't been getting much out of the 2.4.13 specific optimizations. So most of the information I give you right now will be somewhat outdated, but it will be useful for understanding code that has been written already.

First of all, what is the General Hough Transform? It helps to look at descriptions for the Hough Line/Circle transform to understand what a Hough Transform is.

And if you really want to know how the General Hough Transform works on a detailed level, I recommend you read this paper.

But in short, it is an algorithm that allows you to search an image for a template that you define (much in the same way that Hough Circles looks for circles and Hough Lines looks for lines).

So for instance, in our application, we want to detect roombas. So we give OpenCV's implementation a template of the top plate of a roomba and tell the GHT (General Hough Transform) to look for those.

How to use the GHT

Now for the interface that OpenCV gives you (i.e. the part of this documentation that is only necessary because OpenCV mysteriously did not write documentation for the 2.4 version of their GHT).

Most of this was figured out by reading the source code at generalized_hough.cpp

You instantiate a pointer to a GeneralizedHough class by writing:

Ptr< GeneralizedHough > variable_name = cv::GeneralizedHough::create(config_options);

Please note that variable_name can be any name that you want, but for the next few examples, we will be pretending that this variable name is "general_hough".

config_options can be any combination of 3 flags or'ed together. We use "GHT_POSITION | GHT_ROTATION" because it has gotten us the best results. Position is required and is the minimal implementation of the GHT - it searches the image for objects that match the template you define and outputs their positions. Adding the rotation flag allows you to to look for rotated versions of the same template. This is critical for our application, since we want to be able to tell which way the roomba is heading. You can add GHT_SCALE, which is supposed to add the capability of detecting smaller or larger versions of your template, but adding that has never worked in practice for some reason. Plus, we can scale the images manually anyway.

You then use the set method to configure the general hough transform. For example:

generalHough->set("levels", 100);

For a list of all of the configurations you have available to you, you can look at line 31 at generalized_hough.cpp. Note that not all of the configuration options will be available to you in certain modes.

Obviously, you need to give the GHT a template.

generalHough->setTemplate(roomba_template, 100); 

setTemplate only accepts grayscale images. This is why the image we use as a template is in grayscale.

Also, if you go to line 12 of RoombaGHT.cpp, you will see that it reads in the image as a grayscale.

More specifically, the template methods will throw an error if the image it gets is not of a data type 8UC1. 8U means that each pixel value is expressed as an unsigned byte, and C1 means that it is only one channel (i.e. not 3 channels like BGR and HSV).

Finally, you use the detect method to receive an array of positions (which you get no matter which version of the GHT you use) and an array of angles (the result of adding the GHT_ROTATION flag). These are lumped together in a 2d array and returned to you by reference.

For example, if you go to line 61 at RoombaGHT.cpp, you will see:

ght->detect(hsv_channels[1], position, votes, camera_canny_threshold);

position[0][0] holds the x coordinate of the first object it sees

position[0][1] holds the y coordinate of the first object it sees

And position[0][2] holds the angle of the first object relative to the template.

Votes just holds the number of votes for each object it detects, which you can think of as the number of common features between the template and the detected object. Obviously, the higher the number of votes, the more you can be certain that the GHT has actually found a roomba.

For a better explanation of votes, you can go to page 8 of this pdf.