How do I normalize the pixel value of an image to 0~1?

The type of my train_data is 'Array of unit 16'. The size is (96108,7,7). Therefore, there are 96108 images.

The image is different from the general image. My image has a sensor of 7x7 and 49 pixels contain the number of detected lights. And one image is the number of light detected for 0 to 1 second. Since the sensor detects randomly for a unit time, the maximum values of the pixel are all different.

If the max value of all images is 255, I can do 'train data/255', but I can't use the division because the max value of the image I have is all different. I want to make the pixel value of all images 0 to 1. What should I do?


You can gather the maximum values with np.ndarray.max across multiple axes: here axis=1 and axis=2 (i.e. on each image individually). Then normalize the initial array with it. To avoid having to broadcast this array of maxima yourself, you can use the keepdims option:

>>> x = np.random.rand(96108,7,7)

>>> x.max(axis=(1,2), keepdims=True).shape
(96108, 1, 1)

While x.max(axis=(1,2)) alone would have returned an array shaped (96108,)...

Such that you can then do:

>>> x /= x.max(axis=(1,2), keepdims=True)

import numpy as np

data = np.random.normal(loc=0, scale=1, size=(96108, 7, 7))
data_min = np.min(data, axis=(1,2), keepdims=True)
data_max = np.max(data, axis=(1,2), keepdims=True)

scaled_data = (data - data_min) / (data_max - data_min)

EDIT: I have voted for the other answer since that is a cleaner way (in my opinion) to do it, but the principles are the same.

EDIT v2: I saw the comment and I see the difference. I will rewrite my code so it is "cleaner" with less extra variables but still correct using min/max:

data -= data.min(axis=(1,2), keepdims=True)
data /= data.max(axis=(1,2), keepdims=True)

First the minimum value is moved to zero, thereafter one can take the maximum value to get the full range (max-min) of the specific image.

After this step np.array_equal(data, scaled_data) = True.