Skip to content

Instantly share code, notes, and snippets.

@frogermcs frogermcs/ImageUtils.java
Last active Mar 29, 2019

Embed
What would you like to do?
/**
* 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
You can’t perform that action at this time.