What is Quantisation?
Quantization is basically mapping values from a large set to a smaller set. Image quantization is performed by every imaging device since intensity values captured are continuous signal but for storing it we need to perform discretization and Image quantization is used to achieve this.Quantization |
In the figure above the signal will be quantized into 4 different values. These values are known as gray levels. Gray level for practical purposes is chosen as a power of 2 which is,
Reducing Gray Levels
We may need to reduce gray levels for various reasons like for storing and displaying images by devices that support a limited number of colors we can not use the larger set of colors and they need to be mapped to a smaller set.
Now, how to reduce gray levels?
Considering that an image has 256 level by default (this is the actual default value in many cases)
We can reduce the gray level using this simple formula-
I_new=floor(I_old/k)*k
where I= pixel value and floor function rounds the value to the nearest smallest integer.
k is calculated as
k=256/required_gray_levels
Python Code
import cv2
import numpy as np
def reduceGrayLevels(image,reduce_to=256):
image2=image.copy()
value=256//reduce_to
image2=(np.floor((image2/value))*value).astype('uint8')
return image2
image=cv2.imread("../default.png")
reduce_to=input("Enter the power to which intensity values will be reduced to...")
image2=reduceIntensity(image,reduce_to)
cv2.imshow('Image',image)
cv2.imshow('Image2',image2)
cv2.waitKey(0)
cv2.destroyAllWindows()Results