Skip to content

Instantly share code, notes, and snippets.

@frogermcs
Last active March 29, 2019 04:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frogermcs/b953a1632de105200aa7ce368ac5d6bf to your computer and use it in GitHub Desktop.
Save frogermcs/b953a1632de105200aa7ce368ac5d6bf to your computer and use it in GitHub Desktop.
/**
* Make bitmap appropriate size, greyscale and inverted. MNIST model is originally teached on
* dataset of images 28x28px with white letter written on black background.
*/
public static Bitmap prepareImageForClassification(Bitmap bitmap) {
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0);
colorMatrix.postConcat(BLACKWHITE);
colorMatrix.postConcat(INVERT);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(colorMatrix);
Paint paint = new Paint();
paint.setColorFilter(f);
Bitmap bmpGrayscale = Bitmap.createScaledBitmap(
bitmap,
MnistModelConfig.INPUT_IMG_SIZE_WIDTH,
MnistModelConfig.INPUT_IMG_SIZE_HEIGHT,
false);
Canvas canvas = new Canvas(bmpGrayscale);
canvas.drawBitmap(bmpGrayscale, 0, 0, paint);
return bmpGrayscale;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment