Most image processing or computer vision based robotics competitions have a visually distinct arena boundary. You might want to exclude whatever is outside this boundary. Maybe to avoid detecting colors in that area. Or, maybe to prevent motion detection in those regions. Here's a simple technique that should work perfectly in any image processing based competition!
The main assumption is you can detect the arena boundary. Obviously. If you cannot detect the boundary, you just cannot crop it. Anyway, here's what I plan on doing. We'll remove the area outside the thick white boundary in the image below:
We take this image, and discard any information outside the boundary. After all processing, it would look like this:
The technique might need to be modified slightly, based on how you get your boundary on the arena. However, the key ideas will remain the same.
We need the white boundaries. So we extract everything that is white. This is done by ANDing all three channels of the image. What you get is something like this:
There's a lot of stray unwanted white in the result we get. So filters need to be applied to get rid of these stray white pixels. In this case, the opening morphological operation (erosion followed by a dilation) does the trick. Here's the output after opening the image:
This is yet another step for cleaning up the image. We'll keep pixels only above a certain value. This value depends on the histogram you have for you image (based on lighting conditions, surface material, etc). After thresholding, the output looks something like this:
Now we do the hough transform and find lines in the thresholded image. Here's a peek of all the lines that are detected:
Using all those lines, we generate a single boundary for each side of the arena. This is done by "averaging" these lines together. Here's what you get then:
These are lines, not line segments. So we calculate the four corners of the arena (marked green) by solving for their intersection. And get ready for the final step.
With the four points in hand, we can easily generate a mask that covers the entire arena. Something like this:
The above series of steps needs to be done only once (ie if the camera remains still). So, to get the cropped image, all you need to do is AND the mask with the original image! It's super efficient and gets the job done pretty decently! The output you get looks like this:
In the next article, we'll get into how to implement this with OpenCV.