Search code examples
machine-learningscipycomputer-visionmnist

Image normalization


In my perspective, image normalization is to make every pixel to be normalized with an value between 0 and 1, am I right?

But what does the following code mean?

image_size = 28      # Pixel width and height.
pixel_depth = 255.0  # Number of levels per pixel.

for image in image_files:
  image_file = os.path.join(folder, image)

  try:
    image_data = (ndimage.imread(image_file).astype(float) - 
                pixel_depth / 2) / pixel_depth  # WHY ??
    if image_data.shape != (image_size, image_size):
      raise Exception('Unexpected image shape: %s' % str(image_data.shape))
    dataset[num_images, :, :] = image_data
    num_images = num_images + 1

  except IOError as e:
    print('Could not read:', image_file, ':', e, '- it\'s ok, skipping.')

Solution

  • Image normalization is merely the process of changing the range of pixel intensity values.

    The choice of the new range is up to you.

    In the case you've shown, it looks like the range -0.5 .. 0.5 has been chosen.