Application example: Separate out regions of an image corresponding to objects which we want to analyze. This separation is based on the variation of intensity between the object pixels and the background pixels.

To differentiate the pixels we are interested in from the rest (which will eventually be rejected), we perform a comparison of each pixel intensity value with respect to a threshold (determined according to the problem to solve).

Once we have separated properly the important pixels, we can set them with a determined value to identify them (i.e. we can assign them a value of (black), (white) or any value that suits your needs).

We can effectuate types of Thresholding operations with this function. We will explain them in the following subsections.

To illustrate how these thresholding processes work, let’s consider that we have a source image with pixels with intensity values . The plot below depicts this. The horizontal blue line represents the threshold (fixed).

After compiling this program, run it giving a path to an image as argument. For instance, for an input image as:

First, we try to threshold our image with a binary threhold inverted. We expect that the pixels brighter than the will turn dark, which is what actually happens, as we can see in the snapshot below (notice from the original image, that the doggie’s tongue and eyes are particularly bright in comparison with the image, this is reflected in the output image).

Now we try with the threshold to zero. With this, we expect that the darkest pixels (below the threshold) will become completely black, whereas the pixels with value greater than the threshold will keep its original value. This is verified by the following snapshot of the output image: