我已经使用tensorflow构建了一个图像分类器,我正在android上使用android tensorflow库运行它。我的问题是,当在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
任何帮助都将不胜感激。

最佳答案

结果发现,rgb通道被bitmap.getpixels反转,因此更改为bgr有效。

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

10-08 01:51