簡體   English   中英

Android / Tensorflow將Bitmap.getPixels轉換為RGB像素值會導致顏色不正確

[英]Android/Tensorflow converting Bitmap.getPixels to RGB pixel values results in colours not being correct

我已經使用Tensorflow構建了圖像分類器,該分類器正在使用Android Tensorflow庫在Android上運行。 我的問題是,在Android上對圖像進行分類時,預測的類完全關閉了。 但是,當使用具有相同模型的Python對圖像進行分類時,預測的類是正確的。

下面的方法是我將位圖轉換為RGB像素值數組的方法(這是我從sample-tensorflow-imageclassifier此處獲取的 )。

    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)接收的像素的float數組轉換回Bitmap,並注意到顏色的不同,所以我猜這是問題所在嗎? 有沒有一種方法可以轉換像素而不丟失顏色信息?

隨附原始圖像和應用上述功能后我轉換回的圖像。

原始圖片

用上述方法轉換的圖像

任何幫助將不勝感激。

事實證明,Bitmap.getPixels反轉了RGB通道,因此更改為BGR是可行的。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM