【问题标题】:Android/Tensorflow converting Bitmap.getPixels to RGB pixel values results in colours not being correctAndroid/Tensorflow 将 Bitmap.getPixels 转换为 RGB 像素值导致颜色不正确
【发布时间】:2018-07-12 06:31:19
【问题描述】:

我使用 Tensorflow 构建了一个图像分类器,我使用 Android Tensorflow 库在 Android 上运行该分类器。我的问题是,在 Android 上对图像进行分类时,预测的类完全关闭。但是当使用 Python 对图像进行分类时,使用相同的模型预测的类是正确的。

以下方法是我如何将位图转换为 RGB 像素值数组。(我取自 sample-tensorflow-imageclassifierhere)。

    public static float[] getPixels(Bitmap bitmap) {

        final int IMAGE_SIZE = 168;

        int[] intValues = new int[IMAGE_SIZE * IMAGE_SIZE];
        float[] floatValues = new float[IMAGE_SIZE * IMAGE_SIZE * 3];

        if (bitmap.getWidth() != IMAGE_SIZE || bitmap.getHeight() != IMAGE_SIZE) {
            // rescale the bitmap if needed
            bitmap = ThumbnailUtils.extractThumbnail(bitmap, IMAGE_SIZE, IMAGE_SIZE);
        }

        bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());

        for (int i = 0; i < intValues.length; ++i) {
            final int val = intValues[i];
            // bitwise shifting - without our image is shaped [1, 168, 168, 1] but we need [1, 168, 168, 3]
            floatValues[i * 3] = Color.red(val);
            floatValues[i * 3 + 1] = Color.green(val);
            floatValues[i * 3 + 2] = Color.blue(val);
        }
        return floatValues;
    }

我尝试将从 getPixels(bitmap:Bitmap) 接收到的浮点像素数组转换回位图,并注意到颜色有所不同,所以我猜这是问题所在?有没有办法在不丢失颜色信息的情况下转换像素?

附件是原图和我应用上述功能后转换回来的图像。

Original Image

Image converted with above method

任何帮助将不胜感激。

【问题讨论】:

    标签: android tensorflow


    【解决方案1】:

    原来 RGB 通道被 Bitmap.getPixels 反转,因此更改为 BGR 有效。

    floatValues[i * 3 + 2] = Color.red(val);
    floatValues[i * 3 + 1] = Color.green(val);
    floatValues[i * 3] = Color.blue(val);
    

    【讨论】:

      猜你喜欢
      • 2013-12-10
      • 2017-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-08
      • 2013-06-15
      • 1970-01-01
      • 2011-05-05
      相关资源
      最近更新 更多