0

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.

mathematics-and-caffeine
  • 1,664
  • 2
  • 15
  • 19

1 Answers1

0

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;
     }
 }
mathematics-and-caffeine
  • 1,664
  • 2
  • 15
  • 19