Image filtering using scikit image thresholding methods

Images are just collections of numbers called pixels. These numbers range from 0 to 255 where a 0 value represents a dark pixel whereas a value 255 denotes a full bright pixel. These numbers are represented in the form of a matrix or table where an intersection of rows and columns denotes a unique pixel in the image. By the way, this is for grey images as shown below:

For a colour image, it is a superimposition of 3 layers or matrices or tables representing a red channel matrix, a green channel matrix and a blue channel matrix. These 3 channels are commonly referred to as RGBcoding of an image as shown in the following image:

However, RGB is not the only colour image encoding mechanism. There are other image colour spaces such as YCbCr and CMYK. List of color spaces and their uses shows an extensive list of such colour spaces.

In this post, the RGB colour space would be used.


Thresholding is a simple image preprocessing or filtering method using which each pixel value in the image matrix is replaced with 0 (representing dark) if the existing pixel is less than a constant value K, and is replaced with 255 (representing a bright pixel) if the existing pixel value is more than the constant value K. This constant K is known as the threshold value in the thresholding operation.

The result of a thresholding operation is a binary image since the thresholded image only contains two-pixel values: 0 and 255. Following is the thresholded image of the above leopard image.

Following is the code snippet to obtain the above binary image:

# Import the skimage threshold_otsu package
from skimage.filters import threshold_otsu 

# get the global optimal threshold for the leopard image
globalthreshold = threshold_otsu(leopardGrayImg) 

# apply the threshold to the gray image to obtain a binary imageleopardBinaryImg =leopardGrayImg > globalthreshold

# finally show the binary 

Complete jupyter notebook is available in the following repo: