Monday, January 3, 2011

Step 7 : Show numbers on the photo

Process to find grid and numbers


All the numbers are found now. We just have to display.

Display

So, now, we just want to display the numbers on the original photo. The challenge is to give the original perspective to the numbers found and to apply this perspective on the photo. To do that, we use this time the miraculous setPolyToPoly of Matrix class. But, at first, it is not very easy to understand. It took me a lot of time to use the class correctly.That is why we present the exact Java code of our program.

Here are some variables already set :

bitmap : the original 512X512 photo
sudoku_dim : the dimension of the square grid extracted (sudoku_dim = width = height)
(x1,y1), (x2,y2), (x3,y3), (x4,y4) : the coordinates of the 4 corners of the grid in the original photo
sudoku_capture : the 81 numbers recognized (a empty square is represented by a blank)

Here is the complete code :

Bitmap bitmap_to_display = bitmap.copy(Bitmap.Config.RGB_565, true);
    
float orig [] = new float [] {
            0, 0,
            sudoku_dim, 0,
            sudoku_dim, sudoku_dim,
            0, sudoku_dim
            };        
float dest [] = new float [] {
            (float) x1, (float) y1,
            (float) x2, (float) y2,
            (float) x3, (float) y3,
            (float) x4, (float) y4
            };

Matrix matrice = new Matrix();
matrice.setPolyToPoly(orig, 0, dest, 0, 4);
       
Canvas canvas;                  
canvas = new Canvas(bitmap_to_display);
canvas.setMatrix(matrice);
       
int width_square, height_square;                       
width_square = sudoku_dim / 9;
height_square = width_square;
                 
Paint paint_chiffre = new Paint();
paint_chiffre.setTextSize(34);
paint_chiffre.setTypeface(Typeface.DEFAULT_BOLD);
paint_chiffre.setColor(Color.GREEN);
     
String le_nombre;

int iimage, jimage;
int deb_x_square, deb_y_square;

int idx = -1;

for (iimage = 0; iimage < 9; iimage++) {
      deb_y_square = iimage * height_square + 12 + iimage * 2 / 3 ;
      for (jimage = 0; jimage < 9 ; jimage++){
        deb_x_square = jimage * width_square + 2 + jimage / 3; 
        idx++;           
        le_nombre = sudoku_capture.substring(idx,idx + 1);
        if (!le_nombre.equals(" "))
            canvas.drawText(le_nombre,
                        deb_x_square + 8, deb_y_square + 18,
                        paint_chiffre);                         
      }
}

image.setImageBitmap(bitmap_to_display);
And it works!

To show the numbers at any any angle from 0° to 360°, we have to change the points (x,y) accordingly.

In the screen shots of the same photos, we now see the numbers with the right perspective  :







No comments:

Post a Comment