Monday, January 3, 2011

Step 2 : Black and white

Process to find grid and numbers

As we do not need the color information, we convert the bitmap to gray with this code :


Bitmap bmwork = Bitmap.createBitmap(512, 512, Bitmap.Config.RGB_565);
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0);
Paint paint = new Paint();
ColorMatrixColorFilter cmcf = new ColorMatrixColorFilter(colorMatrix);
paint.setColorFilter(cmcf);
Canvas drawingCanvas = new Canvas(bmwork);
drawingCanvas.drawBitmap(bitmap, 0, 0, paint);


We must now convert from grayscale to black and white, black becoming the background (0) and white becoming the foregound(1). Doing so, the grid and the numbers will be converted to 1. To do that, we need to threshold. A lot of information is given in the references given in the second post. So, we will not repeat this information here.

First, to improve performance, we convert the bitmap to an array like this :

int imaget [][] = new int [512][512];    
for (i = 0; i < 512; i++)
       bmwork.getPixels(imaget [i], 0, 512, 0, i, 512, 1);  

We use a threshold algorithm using 12x12 pixel regions to find the grid and also the numbers. For each region, we calculate the average gray level and we use a threshold of 1.08.  So, for the threshold, we  have this code :

if (imaget[i][j] < 1.08 * sum_pixels / 144)
    image1[i][j] = 1;
else
    image1[i][j] = 0;

Notes :
  1. The three small buttons switch from black to magenta when the thread is finished, meaning that the grid and numbers have all been found and are ready
  2. When a photo is taken, we store the photo in the file named photo.jpg in the internal storage of the Android phone. You can press the menu button to store the photo with another name or open an old photo. The photo you store or retrieve are placed in the external storage in the directory named com.rogerlebo.sudokuvision. If you want to retrieve the last photo taken, just press the back button on the camera screen

Display

Here are the screen shots of the photos shown earlier :





No comments:

Post a Comment