所以从在线教程我有以下几行代码
public float[] getPixelData(Bitmap imageBitmap) {
if (imageBitmap == null) {
return null;
}
int width = imageBitmap.getWidth();
int height = imageBitmap.getHeight();
// Get 28x28 pixel data from bitmap
int[] pixels = new int[width * height];
imageBitmap.getPixels(pixels, 0, width, 0, 0, width, height);
float[] retPixels = new float[pixels.length];
for (int i = 0; i < pixels.length; ++i) {
// Set 0 for white and 255 for black pixel
int pix = pixels[i];
int b = pix & 0xff;
retPixels[i] = (float) ((0xff - b) / 255.0);
}
return retPixels;
}
这行代码为我提供了模型的黑白颜色,但我自己的模型需要RGB颜色,我在android studio中得到以下错误
引起:java.lang.IllegalArgumentException:输入和过滤器必须具有相同的深度:1 vs 3
任何人都知道如何将代码转换为我的keras模型的RGB数组? 谢谢!
答案 0 :(得分:1)
int width = imageBitmap.getWidth();
int height = imageBitmap.getHeight();
float[] floatValues = new float[width * height * 3];
int[] intValues = new int[width * height];
imageBitmap.getPixels(intValues, 0, width, 0, 0, width, height);
for (int i = 0; i < intValues.length; ++i) {
final int val = intValues[i];
floatValues[i * 3 + 0] = (((val >> 16) & 0xFF) - imageMean) / imageStd;
floatValues[i * 3 + 1] = (((val >> 8) & 0xFF) - imageMean) / imageStd;
floatValues[i * 3 + 2] = ((val & 0xFF) - imageMean) / imageStd;
}