如何使用灰度像素值从位图创建ByteBuffer?

时间:2018-08-25 18:57:07

标签: java android bitmap

我正在尝试在我的Android应用中使用tflite模型。当我必须从位图创建一个ByteBuffer并将其用作模型的输入时,就会出现问题。

问题:位图是ARGB_8888(32位),而我需要(8位)灰度图像。

将位图转换为ByteBuffer的方法:

mImgData = ByteBuffer
                .allocateDirect(4 * 28 * 28 * 1);

private void convertBitmapToByteBuffer(Bitmap bitmap) throws NullPointerException {
    if (mImgData == null) {
        throw new NullPointerException("Error: ByteBuffer not initialized.");
    }

    mImgData.rewind();

    for (int i = 0; i < DIM_IMG_SIZE_WIDTH; i++) {
        for (int j = 0; j < DIM_IMG_SIZE_HEIGHT; j++) {
            int pixelIntensity = bitmap.getPixel(i, j);
            unpackPixel(pixelIntensity, i, j);
            Log.d(TAG, String.format("convertBitmapToByteBuffer: %d -> %f", pixelIntensity, convertToGrayScale(pixelIntensity)));
            mImgData.putFloat(convertToGrayScale(pixelIntensity));
        }
    }

}

private float convertToGrayScale(int color) {
    return (((color >> 16) & 0xFF) + ((color >> 8) & 0xFF) + (color & 0xFF)) / 3.0f / 255.0f;
}

但是,所有像素值均为-1或-16777216。请注意,here中提到的unpackPixel方法不起作用,因为所有值无论如何都具有相同的int值。 (发布以下更改以供参考。)

private void unpackPixel(int pixel, int row, int col) {
    short red,green,blue;
    red = (short) ((pixel >> 16) & 0xFF);
    green = (short) ((pixel >> 8) & 0xFF);
    blue = (short) ((pixel >> 0) & 0xFF);
}

1 个答案:

答案 0 :(得分:1)

您可以调用Color.red()或像素值上的绿色/蓝色,它将返回灰度强度。然后使用putFloat()将其放入字节缓冲区。同样,使用bitmap.getPixels()获取单个数组中的所有像素值比bitmap.getPixel(i, j)更快。这是我在tflite模型中加载灰度图像的方法:

private ByteBuffer getByteBuffer(Bitmap bitmap){
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    ByteBuffer mImgData = ByteBuffer
            .allocateDirect(4 * width * height);
    mImgData.order(ByteOrder.nativeOrder());
    int[] pixels = new int[width*height];
    bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
    for (int pixel : pixels) {
        mImgData.putFloat((float) Color.red(pixel));
    }
    return mImgData;
}

如果需要归一化的值,只需除以255:

float value = (float) Color.red(pixel)/255.0f;
mImgData.putFloat(value);

然后您可以在解释器中使用以下代码:

ByteBuffer input = getByteBuffer(bitmap);
tflite.run(input, outputValue);

希望这可以帮助人们将来寻找它!