3.ii.3. Thresholding¶

Visual inspection of a lot of images reveals that the objects of interests (the coins in the image below) have a grayness value that is different from the groundwork. In these cases the histogram will frequently bear witness a bimodal distribution.

../../../_images/thresholding.png

In the to a higher place image showing particles embedded in a substrate, the particles are brighter (more white) than the background. This is as well clearly visible in the histogram. On the correct in the higher up figure the thresholded image is shown. The threshold value is manually selected at \(t=0.5\).

There are many algorithms known that automatically calculate a suitable threshold for an image. In this section we will simply expect at 1 of them.

three.2.3.1. Isodata Thresholding¶

Isodata thresholding is a way to automatically find a threshold for a given gray value image \(f\). Consider a threshold \(t\) somewhere in the range of gray values in the paradigm. And so nosotros consider the hateful of all pixels in the image with a gray value less then or equal to \(t\), call it \(m_L\) and the hateful of all pixels with grayness value greater than \(t\), let'south telephone call that \(m_H\).

Isodate thresholding then finds a threshold such that

\[t = \frac{m_L+m_H}{ii}\]

i.due east. the value of \(t\) is such that \(t\) is halfway inbetween \(m_L\) and \(m_H\). Delight not that both \(m_L\) and \(m_H\) are functions of \(t\). Therefor the above equation tin can be written as

\[t = \frac{m_L(t)+m_H(t)}{2} = m(t)\]

defining the function \(1000\). A indicate \(t\) such that \(t=m(t)\) is chosen a fixed point of the function \(m\). Starting from an initial estimate \(t_0\) the stock-still point tin be plant with fixed point iteration:

\[t_{i+1} = m(t_i)\]

In python this would consequence in the following code:

                            In [1]:                            f              =              plt              .              imread              (              'images/cermet.png'              )              In [2]:                            t              =              0.ii              In [3]:                            epst              =              0.01              In [4]:                            while              1              :                              ...:                            mL              =              f              [              f              <=              t              ]              .              mean              ()                              ...:                            mH              =              f              [              f              >              t              ]              .              hateful              ()                              ...:                            t_new              =              (              mL              +              mH              )              /              2                              ...:                            if              abs              (              t              -              t_new              )              <              epst              :                              ...:                            pause                              ...:                            t              =              t_new                              ...:                            print              (              t              )                              ...:                            0.349102735519              0.451217293739              0.480459988117            

The threshold value that is calculated is \(t=0.488\) and close to what we had manually selected.

In an exercise you lot are asked to implement the isodata thresholding algorithm using the histogram information instead of accessing all pixels in the unabridged image in every iteration.

iii.2.iii.ii. Exercises¶

  1. The isodata threshold algorithm was implemented in a previous department using the image direct. This obviously is overkill. We don't need all individual pixel values, all we need is the histogram of the paradigm.
    1. Given a histogram h of the epitome. The mid points of the bins are in array m . Give the expressions to calculate \(m_L\) and \(m_H\) as used in the isodata algorithm.
    2. Implement the isodata threshold algorithm using the histogram and non the individual pixel values (in one case the histogram is calculated of grade). Note that epst should exist related to the bin width of the histogram.
  2. It is non that difficult to prove that the isodata threshold algorithm actually is 2-means clustering in disguise.
    1. Tin you prove that?
    2. Using k-means clustering we can endeavor to observe more than just a background and object cluster. This might be usefull to segment images where more than two dominant gray values are present.
    3. Formulating thresholding every bit 2-means clustering also opens the possibility to 'threshold' colour images direct without start looking at just the luminance values.