Friday, 15 July 2011

Image Processing - Part 1

As I said in an older news, I'm currently working at the same time on these two features, that's why I'm also writting this news.

So, the Image Processing is done using ImageJ Java library.
This library contains a software to process images I used to know how to do.
On a Java program, you can use ImageJ plugins, or "macros" files to directly use the tools from the software.
Even if macros are more limited than plugins, they're also more simple to use, so I've started with this.

As I'm currently working with only one set of pictures, sunlight changements may make the program not to work.
By the way, this set of picture is available on my google docs.

So, in order to simplify Image Processing, it will be divided in two parts :

- First, on the beginning only, locate hoops and pegs on a capture with no balls, no robot.
As these elements aren't supposed to move, you don't need to recalculate positions each time.
However, if they accidentally move, a button will enable to recalculate them.

-Then, each time you need it, you will calculate position of balls, and position + direction of the robot.

I'm basically using Binary images, with Erosion/Dilatation/Open/Close/Reconstruction operators, as I've already used them at school and they seem to fit there.

I - Hoops & Peg

  • Detection


 The camera enables enough contrast between board and elements so that Find Edge command is useful.
Then, a Binarization gives the third picture.


The first thing to remove are sides.
For this, as hoops are supposed to be far enough from sides, I'm using a home-made mask to remove sides.

The last thing to remove are the "cracks" between the two parts of the board.
First, I use Fill Holes command to make hoops and peg bigger ( cracks are not circles ).
Then, an enough Erosion gives the first image.
Depending on the quality of the shot, peg or hoops may disappear as well, so erosions must be done carefully.
Then, using a reconstruction ( Dilatation + MIN operator) gives a cleaned image.


Then, I fill holes again, and add a dilatation to increase the chance of having 7 related elements.
A XOR 11111111 reverse bit values, in order to get positions with Find Maxima values.

ImageJ software enables users to save results in a text file. This is what I'm doing with my macro. 
Then, using file streams, I get my 7 points.
  • Identification
At first, I was worrying about this, but the solution was easy :


If you position elements like in the classic game, and if you keep the camera on the same place, the board will always be organized more or less like on this picture.
So, with two levels of sort, you can identify all your elements :

{1, 2, 3, 4, 5, 6, P}
  • A first sort using Y coordinates of points will divide the elements in 3 groups.
{3, 4} ; { 5, 6, P } ; { 1, 2 }
  •  Then one sort using X coordinates in each group allows you to identify each element :
{3} ;  {4} ; {5} ; {P} ; {6} ; {2} ; {1}

Knowing the ratio pixels/cms, you can then determine the position of hoops and peg.



II - Robot and Balls


For the moment I'll only speak about the major interest of dividing the process :
If you save the complete binarized image of hoops, pegs, sides and "cracks", you can then substract it from a binarized picture with Balls and Robots : this will remove most of the undesirable shapes.

No comments:

Post a Comment