Rescaling image formula with arrays

Viewed 86

I'm doing an exercise about matplotlib in which they show me the procedure to improve image intensity. After converting the image to an array and detecting the minimum and maximum values (RGB - 0 to 255):

# Extract minimum and maximum values from the image: pmin, pmax
pmin, pmax = image.min(), image.max()
print("The smallest & largest pixel intensities are %d & %d." % (pmin, pmax))

They propose the following:

# Rescale the pixels: rescaled_image
rescaled_image = 256*(image - pmin) / (pmax - pmin)
print("The rescaled smallest & largest pixel intensities are %.1f & %.1f." % 
      (rescaled_image.min(), rescaled_image.max()))

What is the logic behind this formula? 256 * (image - pmin) / (pmax - pmin)

Thanks :)

2 Answers

enter image description here

The original 2D Array is an image array with a thin histogram. The rescaled 2D Array is the final image after you apply the formula [256 * (image - pmin) / (pmax - pmin)]. You get the final image with a wider histogram. Note the word "image" in formula refers to a pixel.

The idea behind this formula is to increase the dynamic range of the image's colors. While the original dynamic range of the pixels is between 0-255, pixels in the extreme edges of this range don't necessarily exist in any image. Using the proposed formula ensures that there exists at least one pixel with an intensity of 255 and another pixel with an intensity of 0.

Related