3 - Thresholding and Histogram

Resources

Full tutorial (in Acrobat pdf format):

Tutorial_03.pdf

C# project (zip file):

Tutorial_03_IpTestBed.zip

Summary

As we have described, pixels in a greyscale image are represented by one of the 256 grey-levels 0 to 255, where 0 is black and 255 is white. The technique of 'thresholding' compares each pixel value to a particular threshold value and if the pixel value is less than the threshold valve, the pixel is set 0. If the pixel value is greater than or equal to the threshold value, the pixel is set to 255.

Thresholding can be described mathematically as follows:

s(x,y) = {0 if r(x,y) < T; 255 Otherwise.

This means that for any pixel s(x,y) in a resulting image, the value is set to 0 if the corresponding pixel in the source image is less than the threshold value, T, and 255 otherwise. As an example for a threshold value of T=100, if a pixel has a value of 54 it is set to black and if it has a value of 177 it is set to white.

The value of T must be chosen to achieve the desired result. A method of choosing a suitable value is described later in this tutorial.

While you were experimenting with different threshold values with the 'Threshold' function, you may have found that you didn't know exactly what value to use to extract a particular feature of your sample image. What you needed was some way of seeing what grey-levels are present in your image. This is where the 'Image Histogram' comes in.

The image histogram is a bar-chart which has on the x-axis the values 0 to 255 for the grey-levels in the image. The chart has a vertical bar at each grey-level whose height represents the number of pixels in the image with that grey-level. Below is shown an image and its histogram.

In the image histogram shown above the ticks on the x-axis are for greyscale values 0, 64, 128, 192 and 255. The grey-level that has the largest count of pixels is about 10 (the spike near the left). This is scaled to fit the maximum height of the histogram. The counts for the other grey-levels are then shown as a proportion of this maximum count.

As can be seen from this histogram, the image mostly contains dark pixels - this is because most of the histogram is towards the left side of the graph. You can also see a peak near the centre of the histogram. This is made by the mid-grey pixels making up the lilies themselves.

Please download the tutorial in the Resources section above and work through it using Microsoft C#. If you have any trouble creating the progam from the code given in the tutorial, please download the full C# project and try to work out what has gone wrong with yours.

When you are ready go onto the next tutorial.

Resources

Full tutorial (in Acrobat pdf format):

Tutorial_03.pdf

C# project (zip file):

Tutorial_03_IpTestBed.zip