Histograms in Image Processing with skimage-Python | by Mathanraj Sharma | Towards Data Science

Visualizations are always been an efficient way to represent and explain many statistical details. In image processing histograms are used to depict many aspects regarding the image we are working with. Such as,
Exposure
Contrast
Dynamic Range
Saturation
and many more. By visualizing the histogram we can improve the visual presence of an image and also we can find out what type of image processing could have been applied by comparing the histograms of an image.
Images are stored as pixels values, each pixel value represents a color intensity value. Histograms are frequency distribution of these intensity values that occur in an image.
h(i) = the number of pixels in I(image) with the intensity value i
For example, if i = 0, the h(0) is the number of pixels with a value of 0.
Grayscale Image
from skimage import ioimport matplotlib.pyplot as pltimage = io.imread('~/Desktop/Lenna_gray.png')ax = plt.hist(image.ravel(), bins = 256)plt.show()Output: Figure-1
In the above code, we have loaded the grayscale image of Lenna and generated its histogram using matplotlib. Since the image is stored in the form of a 2D ordered matrix we converted it to a 1D array using the ravel() method.
Color Image
In color images, we have 3 color channels representing RGB. In Combined Color Histogram the intensity count is the sum of all three color channels.
h(i) = h_red(i) + h_green(i) + h_blue(i)
from skimage import ioimport matplotlib.pyplot as pltimage = io.imread('~/Desktop/Lenna.png')_ = plt.hist(image.ravel(), bins = 256, color = 'orange', )_ = plt.hist(image[:, :, 0].ravel(), bins = 256, color = 'red', alpha = 0.5)_ = plt.hist(image[:, :, 1].ravel(), bins = 256, color = 'Green', alpha = 0.5)_ = plt.hist(image[:, :, 2].ravel(), bins = 256, color = 'Blue', alpha = 0.5)_ = plt.xlabel('Intensity Value')_ = plt.ylabel('Count')_ = plt.legend(['Total', 'Red_Channel', 'Green_Channel', 'Blue_Channel'])plt.show()Output: Figure-2
Usually, the range of intensity values of images is from [0–255] in 8bits representation(28). But images can be also represented using 216, 232 bits and so on. In such cases the intensity range is high and it is hard to represent each intensity value in a histogram.
We use binning to overcome the above problem. Here we quantize the range into several buckets. For example,
if we quantize 0-255 into 8 bins, here our bins will be0-31, 32-63, 64-95, 96-127, 128-159, 160-191, 192-223, 224-255
Now we need to find a way to put each intensity value into the appropriate bins. We can simply solve this,
k = 256 #number of possible integer values in 8 bit representationb = 8   #number of binsj = floor( (h(i)*b)/k )#j is the bin number of the intensity value at position i
let us plot the histogram of grayscale Lenna image again but this time with 8 bins,
from skimage import ioimport matplotlib.pyplot as pltimage = io.imread('~/Desktop/Lenna_gray.png')_ = plt.hist(image.ravel(), bins = 8 )_ = plt.xlabel('Intensity Value')_ = plt.ylabel('Count')plt.show()Output: Figure-3
The cumulative histogram is a special histogram that can be derived from the normal histogram. We find the counts of each intensity value from 0–255 and then add each subsequent counts,
if i = 0 then H(i) = h(0)else H(i) = H(i-1) + h(0)
Cumulative histograms are useful in many image processing applications like histogram equalization and so on.
from skimage import ioimport matplotlib.pyplot as pltimage = io.imread('~/Desktop/Lenna_gray.png')_ = plt.hist(image.ravel(), bins = 256, cumulative = True)_ = plt.xlabel('Intensity Value')_ = plt.ylabel('Count') plt.show()Output: Figure-4
I hope you understand what is histogram and how to create histograms using skimage. Let us meet again and I’ll tell you some use cases of histograms in Image Processing.