I want do make an image classifier using android. To classify the image Tensorflow(the classifying library) needs a 1 dim float array. To save processing time I have written a function that takes the bitmap greyscales it(to make it 1dim), cuts the middle square from it(to take away unnecessary parts), and then scales the sqaure down to 28x28 pixels. Okay this works just fine.
To get the pixelarray from the bitmap there is the function Bitmap.getPixels which fills up an existing int array with the pixel values. Link: https://developer.android.com/reference/android/graphics/Bitmap.html#getPixels(int[], int, int, int, int, int, int)
int[]pixels = new int[processedBitmap.getHeight()*processedBitmap.getWidth()];
processedBitmap.getPixels(pixels, 0, processedBitmap.getWidth(), 0, 0, processedBitmap.getWidth(), processedBitmap.getHeight());
Problem 1: Everytime I execute it my app just stopps. Thats the smaller Problem. There might be just a tiping error(code above and error below)
Process: com.example.juliusdebus.project1, PID: 21529 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.juliusdebus.project1/com.example.juliusdebus.project1.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x310 at android.app.ActivityThread.deliverResults(ActivityThread.java:3790) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3833) at android.app.ActivityThread.access$1700(ActivityThread.java:152) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1390) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5538) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x310 at android.content.res.HwResources.getText(HwResources.java:1252) at android.widget.TextView.setText(TextView.java:4172) at com.example.juliusdebus.project1.MainActivity.onActivityResult(MainActivity.java:76) at android.app.Activity.dispatchActivityResult(Activity.java:6238) at android.app.ActivityThread.deliverResults(ActivityThread.java:3786) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3833) at android.app.ActivityThread.access$1700(ActivityThread.java:152) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1390) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5538) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Problem 2: I need black to be 1 and white to be 0, so I have to scale the values by deviding by the highest possible value 128 for example. How do I find out that highest possible value? Is there a simple get function? Because the Bitmap is created in my processBitmap function(mentioned above) maybe the determined debth can be found there so here is my code
private Bitmap processBitmap(Bitmap colorBitmap) {
Bitmap cutBitmap;
Bitmap finalBitmap;
Bitmap grayscaleBitmap = Bitmap.createBitmap(
colorBitmap.getWidth(), colorBitmap.getHeight(),
Bitmap.Config.RGB_565);
Canvas c = new Canvas(grayscaleBitmap);
Paint p = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(cm);
p.setColorFilter(filter);
c.drawBitmap(colorBitmap, 0, 0, p);
if (grayscaleBitmap.getWidth() >= grayscaleBitmap.getHeight()){
cutBitmap = Bitmap.createBitmap(
grayscaleBitmap,
grayscaleBitmap.getWidth()/2 - grayscaleBitmap.getHeight()/2,
0,
grayscaleBitmap.getHeight(),
grayscaleBitmap.getHeight()
);
}else{
cutBitmap = Bitmap.createBitmap(
grayscaleBitmap,
0,
grayscaleBitmap.getHeight()/2 - grayscaleBitmap.getWidth()/2,
grayscaleBitmap.getWidth(),
grayscaleBitmap.getWidth()
);
}
int width = cutBitmap.getWidth();
int height = cutBitmap.getHeight();
float scaleWidth = ((float) PIXEL_WIDTH) / width;
float scaleHeight = ((float) PIXEL_WIDTH) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// "RECREATE" THE NEW BITMAP
finalBitmap = Bitmap.createBitmap(
cutBitmap, 0, 0, width, height, matrix, false);
cutBitmap.recycle();
return finalBitmap;
Thank you very much!