Android – Converting Bitmap(RGB_565) to Grayscale(8 bit) Android

androidandroid-camerabitmapgrayscaleimage processing

I am doing a Face Recognition project for my final year. I have to do face Recognition on Android using Local Binary Pattern(LBP).

I searched a lot on the internet for codes for face recognition using LBP in java, but couldn't find anything implementable on android. So now I am forced to writing it all, and I am a kind of a newbie to android as well as image processing.

I have found out a way to capture images from the camera.

public class CameraCapture extends Activity {

protected boolean _taken = true;
File sdImageMainDirectory;

protected static final String PHOTO_TAKEN = "photo_taken";

@Override
public void onCreate(Bundle savedInstanceState) {

    try {

        super.onCreate(savedInstanceState);         
                File root = new File(Environment
                        .getExternalStorageDirectory()
                        + File.separator + "myDir" + File.separator);
                root.mkdirs();
                sdImageMainDirectory = new File(root, "myPicName");


            startCameraActivity();
        }
     catch (Exception e) {
        finish();
        Toast.makeText(this, "Error occured. Please try again later.",
                Toast.LENGTH_SHORT).show();
    }

}

protected void startCameraActivity() {

    Uri outputFileUri = Uri.fromFile(sdImageMainDirectory);

    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

    startActivityForResult(intent, 0);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (resultCode) {
    case 0:
        finish();
        break;

    case -1:

        try {
            StoreImage(this, Uri.parse(data.toURI()),
                    sdImageMainDirectory);
        } catch (Exception e) {
            e.printStackTrace();
        }

        finish();
        startActivity(new Intent("com.piit.lbp.form.LBPFORMADD"));

    }

}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    if (savedInstanceState.getBoolean(CameraCapture.PHOTO_TAKEN)) {
        _taken = true;
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putBoolean(CameraCapture.PHOTO_TAKEN, _taken);
}

    public static void StoreImage(Context mContext, Uri imageLoc, File imageDir) {
    Bitmap bm = null;

    try {
        bm = Media.getBitmap(mContext.getContentResolver(), imageLoc);
        bmGray = toGrayScale(bm);
        FileOutputStream out = new FileOutputStream(imageDir);
        bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
        bm.recycle();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

Question : What is the "Bitmap bm" – like RGB_565, or something else ?? and how many bits.

Before compressing bitmap to jpeg I call a RGB to Grayscale method with the following code

public static Bitmap toGrayscale(Bitmap bmpOriginal)
{        
    int width, height;
    height = bmpOriginal.getHeight();
    width = bmpOriginal.getWidth();    

    Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    Canvas c = new Canvas(bmpGrayscale);
    Paint paint = new Paint();
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
    paint.setColorFilter(f);
    c.drawBitmap(bmpOriginal, 0, 0, paint);
    return bmpGrayscale;
}

And then I wish to use bmGray for applying LBP operator.
Question : When you convert to Grayscale using ColorMatrix, is it 8bits per pixel ??

Cos I want to convert bmGray to Byte Array and want to extract only 1 byte at a time. And also I want to know if can convert the grayscale image to a 2d matrix of pixel values, I know that it is already a 2d matrix of pixel values, but how can i work on it.. like selecting the pixel above and below the current pixel. ??

Best Answer

for the CameraPicture this applies:

If setPreviewFormat(int) is never called, the default will be the YCbCr_420_SP (NV21) format. http://developer.android.com/reference/android/hardware/Camera.Parameters.html#setPreviewFormat%28int%29

Bitmap bmp you can configure like:

Bitmap bmp = Bitmap.createBitmap(frameWidth, frameHeight, Bitmap.Config.ARGB_8888); //or RGB_565 if you prefer.

for accessing bmp data I only found something like this: http://upload.wikimedia.org/wikipedia/commons/c/c4/BMPfileFormat.png

hope it helps