Fundamentals of image thresholding and masking



Image thresholding is way of converting a color image into a binary image based on certain threshold of pixel intensity. This is very useful in extracting dominant foreground and background objects. It can also be used to create a sketch like images. OpenCV and Python offers various ways of achieving this.

Let me illustrate this with few examples.

Lets first read a color image using opencv into numpy array and display it

This is 750x1250 pixel image of horse with scenic background. We would like to extract foreground object (horse) and background (scenes).

Before you apply thresholding, you must convert a 3 channel color image (RGB) into grayscale image with one channel as follows.

Now we can start applying thresholding. There are 4 techniques

Simple Thresholding : Here we need to specify simple threshold for pixels intensity manually. Then opencv converts anything above threshold T to white (255)and anything < T, it converts it to black (0). Here is an example. This threshold is applied to all pixels in the image.

Simple Inverse Thresholding : Here we need to specify simple threshold for pixel intensity manually. Then opencv converts anything above threshold T to black (0)and anything < T, it converts it to white (255). Here is an example. This threshold is applied to all pixels in the image.

Othsu’s Thresholding: Here threshold is calculated dynamically by Othsu’s method. This works well where we bi modal distribution of pixel intensities. Othsu’s method finds 2 peaks in image histograms and determines threshold. This can be combined with Simple or inverse thresholding to get black or white mask. This method ix extremely useful when you have millions of photos taken with varying light conditions and manual determination of threshold for each image is very time consuming. This threshold is applied to all pixels in the image.

Extracting Foreground Object: We can use image obtained after using thresholding as mask by bitwise anding with original image.

Here is example of extracting foreground object. We use mask obtained after simple thresholding.

Extracting Background Object:

Here is example of extracting background object. We use the mask obtained after inverse thresholding

Adaptive Thresholding: Unlike any of the preceding technique where same threshold is applied to all pixels in the image, adaptive threshold computes threshold for a small neighborhood (5x5, 7x7 etc)at at time y using simple of gaussian mean and then applies thresholding to that area. The process is repeated for all other neighborhood until entire image is process. The result looks like a sketch effect as shown below

If you would like to learn step by step on how to leverage thresholding and apply with examples, you can sign up for my course here. Email me at evergreenllc2020@gmail.com for discounts.


About Author Evergreen Technologies:

Active in teaching online courses in Computer vision , Natural Language Processing and SaaS system development. Over 20 years of experience in fortune 500 companies.

•Blog: https://www.evergreentech.online

•Linked in: @evergreenllc2020

•Twitter: @tech_evergreen

•Udemy: Evergreen Technologies

•Github: https://github.com/evergreenllc2020/