简体   繁体   中英

Java normalize image to [0,1]

How can you normalize a given Bitmap in Java?

Like this:

Bitmap b = BitmapFactory.decodeFile(picturePath);
// b = b / 255.0 

So that my image isn't RGB from 0 to 255 but normalized to RGB from 0 to 1.

I've figured it out:

float[][][][] input = new float[1][DIM_X][DIM_Y][3];
for (int x = 0; x < DIM_X; x++) {
     for (int y = 0; y < DIM_Y; y++) {
         int pixel = bitmap.getPixel(x, y);
         // Normalize channel values to [0.0, 1.0]
         input[0][x][y][0] = Color.red(pixel) / 255.0f;
         input[0][x][y][1] = Color.green(pixel) / 255.0f;
         input[0][x][y][2] = Color.blue(pixel) / 255.0f;
     }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM