Blogs/Image Thresholding

Image Thresholding

mesakarghm Jul 15 2021 1 min read 150 views
Computer Vision
Image Thresholding.jpeg

Thresholding is a basic image operation. During segmentation, we first seperate pixels into two or more categories. Basically, we can classify pixels into two groups based on a threshold. Pixels that exceed a given intensity will be placed in one group and pixels that do not exceed the given intensity will be placed in another group. 

Global Thresholding is a type of thresholding where we use a single threshold to seperate pixels and map them to two colors. For example, we can set the pixel value of pixels which exceed the given intensity to 255 and we can set the pixel value of pixels which do not exceed the given intensity to 0. This will result in finding objects from the background sorrounding them. In the above given example, the objects will have the pixel value of 255 while the background will have a pixel value of 0. 

But we have to be careful before applying thresholding. This is because not all images will have a clear threshold based on which the image pixels can be mapped into two categories. 

Below is given a simple implementation of Global Thresholding where we group pixel to either 255 or 0 based upon a common threshold. 

 

Learn and practice this concept here: 

https://mlpro.io/problems/image-thresholding/

import numpy as np
def threshold(mat, thresh = 110):
    print(mat.shape) 
    r,g,b = mat.shape[0],mat.shape[1],mat.shape[2]
    for x in range(0,r):
        for y in range(0,g):
            for z in range(0,b):        
                mat[x][y][z] =  255 if mat[x][y][z] > thresh else 0 
    return mat