6 Segmentation by histogram thresholding#

Objectives#

  • display and manipulate a histogram

  • define a threshold from a histogram

Image histogram#

../_images/945d4762de887da3d3db98d9885ecf9ed112640b62ab5494a0a1069d5137a049.png

The histogram is obtained with matplotlib.pyplot.hist. As this function calculates the image from a vector, it must be used on the vectorized image, and not the image itself. The vectorized image is obtained with numpy.ravel. The pixels in a vectorized image are arrange in one dimension.

The histogram of the image is not unique: the number of bins must be specified. Generally, we choose as many bins as gray levels, but this is not mandatory. The following histograms are obtained with 16, 64 and 256 bins. Note that the intensities of the image are not between 0 and 255 but between 0 and 1: do you know why (ask the teacher if you cannot get the solution)?

../_images/b47ae0841aa9a6272e3e464e5b624197b612042d9fc3629c3706ea4f5a88ea81.png ../_images/b1ad83ee379e2fca254f290f44f63a4a368efd8482c6c121b461b987ed952bb1.png ../_images/91ae6d53d8ec059e4e1af332ee4f04dad13dac789d120f8d109c1906554f9ec4.png

On the one hand, we easily observe that if the number of bars is low, then the distribution is coarse and some details (such as the presence of two modes after 0.5) are not visible. On the other hand, if the bin number is high, then we may have too much detail to analyse the result. In conclusion, the bin number does not necessarily have to be equal to the number of gray levels in the image, it depends on what we are going to do with the histogram!

Segmentation in 2 classes#

The presence of several modes in the histogram implies several possible thresholds. The two images below correspond to the thresholds obtained around the three modes of the histogram.

../_images/7874e801b5ce040b22e12fe491f90329bdd27c18c55ad465e11c27513990d27b.png ../_images/eff2c2ec0d948f621c68de2e0c7d14e9daa2739b70e568267ee5773503920bf9.png

Segmentation in 3 classes#

The histogram is a tool that represents the distribution of the intensities in an image. We used it in this exercise to define a threshold on the image, then segmenting the image into two classes. Furthermore, we can define several thresholds to obtain a segmentation with more than two classes, as for example in the image below.

How can you get this result? Besides, which modes in the histogram correspond to the three identified zones?

../_images/2c71f3ce04275ac272371a48cb49eb130bb77a568e291a779e84b9b83b28f3c2.png

There are methods in the literature that are able to determine automatically the value of the threshold(s), such as Otsu’s method.