Histogram#
Definition#
The histogram (French: histogramme) may be the simplest tool for image processing; it can be seen on cameras and even some smartphones, when shooting.
It depicts how the intensities of the pixels are distributed.
It is the discrete function
where
The code below shows an image and two associated histograms.
The histograms are displayed as a bar plot, constituted as a set of bins.
The number (hence the width) of the bins are chosen by the user;
in the example below, we choose 128 bins and 16 bins.
Both histograms lie on
import numpy as np
import skimage.io as io
import matplotlib.pyplot as plt
# Load the image
img = io.imread("../_static/figs/daisy.png", as_gray=True) * 255
# Display the image
fig, ax = plt.subplots(1, 1, figsize=(5,5))
ax.imshow(img, cmap="gray")
plt.show()
# Prepare the figure for the histograms
fig, axs = plt.subplots(1, 2, figsize=(12,4))
# Plot the first histogram
axs[0].hist(
img.ravel(), # The image must be flattened to use function hist
bins=range(0,256,2) # Define 128 bins (of width 2) between 0 and 255
)
axs[0].set_xlabel("Intensities")
axs[0].set_ylabel("Number")
# Plot the second histogram
axs[1].hist(
img.ravel(), # The image must be flattened to use function hist
bins=range(0,256,16) # Define 16 bins (of width 16) between 0 and 255
)
axs[1].set_xlabel("Intensities")
axs[1].set_ylabel("Number")
# Show the figure
plt.show()
We distinguish two “modes” on the histogram. The one on the left (intensities around 75) corresponds to the dark tones in the image (mainly the background). The one on the right (intensities around 180) corresponds to the light tones (the petals and the center).
Some remarks#
If the histogram
is normalized (i.e. the bins are divided by the pixel number ), then it can be seen as a discrete probability density function :The histogram gives a global information about the pixel intensities of an image but loses the spatial information in the image. In consequence, two different images can have the same histogram (cf. Fig. 23).
Histogram transformations#
A histogram transformation consists of applying a mathematical function to the intensity distribution. Generally, the transformations are useful to improve the visual quality of an image, but are rarely needed inside an automatic processing.
The transform, denoted
where
Below are some common transformations (we assume the pixel intensities to lie in
Negative image#
Gamma correction#
Histogram spreading#
(French: étalement d’histogramme)
where
Histogram equalization#
(French: égalisation d’histogramme)
where
Thresholding#
The histogram is sometimes very useful to segment the image in two classes,
that is to distinguish the objects in the image with respect to their gray level.
Indeed, if the histogram shows clearly two modes (i.e. two “bumps”),
a threshold
if the pixel level is lower that
, then the pixel is in class 0 (displayed in black in Fig. 28),otherwise, the pixel is in class 1 (displayed in white in Fig. 28).
Such a thresholding yields a binary image whose pixels have only two values. Several methods exist that compute the threshold automatically, such as Otsu’s method.