【问题标题】:How to get Android pixel RGB array for Keras Model如何为 Keras 模型获取 Android 像素 RGB 数组
【发布时间】:2017-12-29 05:39:42
【问题描述】:

所以从在线教程中我有以下代码行

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 数组吗? 谢谢!

【问题讨论】:

    标签: android tensorflow keras pixels


    【解决方案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;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-09-01
      • 2011-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-13
      • 2012-04-25
      相关资源
      最近更新 更多