Thresholding is a kind of image segmentation method which is used to simplify the image( or video ) by pixels manipulation.

In thresholding, we convert the image which can either be colored or in greyscale into binary image( only black and white color is used to represent any pixel ). But in computer vision, thresholding is done on grayscale images only. So initially, the image has to be converted in grayscale color space.

Every pixel will either have 0 to represent black or maximum value to represent white color. 

Two methods are used in openCV for thresholding:

  1. Simple thresholding
  2. Adaptive thresholding

Let’s discuss them one by one:

Simple thresholding:

In this technique, the same threshold value is applied for every pixel.

cv2.threshold(source, thresholdValue, maxVal, thresholdingTechnique) function is used to perform simple thresholding with openCV. The function has four arguments:

  1. source: it signifies the source image which has to be processed.
  2. thresholdValue: the value of threshold.
  3. maxVal: it signifies the maximum value which can be assign to a pixel.
  4. thresholdingTechnique: the type of thresholding to be performed. There are five simple thresholding technique:
    1. cv2.THRESH_BINARY
    2. cv2.THRESH_BINARY_INV
    3. cv2.THRESH_TRUNC
    4. cv2.THRESH_TOZERO
    5. cv2.THRESH_TOZERO_INV

Implemented code in Python:

import cv2

img = cv2.imread('flower.jpg',0);

#different thresholding techniques on the image 
 
retval, thresh1 = cv2.threshold(img, 125, 255, cv2.THRESH_BINARY);
retval, thresh2 = cv2.threshold(img, 125, 255, cv2.THRESH_BINARY_INV);
retval, thresh3 = cv2.threshold(img, 125, 255, cv2.THRESH_TRUNC);
retval, thresh4 = cv2.threshold(img, 125, 255, cv2.THRESH_TOZERO);
retval, thresh5 = cv2.threshold(img, 125, 255, cv2.THRESH_TOZERO_INV);

cv2.imshow('ORIGINAL', img);
cv2.imshow('THRESH_BINARY', thresh1);
cv2.imshow('THRESH_BINARY_INV', thresh2);
cv2.imshow('THRESH_TRUNC', thresh3);
cv2.imshow('THRESH_TOZERO', thresh4);
cv2.imshow('THRESH_TOZERO_INV', thresh5);
	
#wait for 10 seconds 
cv2.waitKey(10000);
cv2.destroyAllWindows();

OUTPUT:

Adaptive thresholding:

In this technique, we can have different threshold values for different regions.

cv2.adaptiveThreshold(source, maxVal, adaptiveMethod, thresholdingTechnique, blocksize, constant) function is used in openCV to perform adaptive thresholding. The function has six arguments:

  1. source: it signifies the source image which has to be processed.
  2. maxVal: it signifies the maximum value which can be assign to a pixel.
  3. adaptiveMethod: it signifies the way to calculate threshold value. There are two ways for calculating threshold value:
    1. cv2.ADAPTIVE_THRESH_MEAN_C
    2. cv2.ADAPTIVE_THRESH_GAUSSIAN_C
  4. thresholdingTechnique: the type of thresholding to be performed. There are five simple thresholding technique:
    1. cv2.THRESH_BINARY
    2. cv2.THRESH_BINARY_INV
    3. cv2.THRESH_TRUNC
    4. cv2.THRESH_TOZERO
    5. cv2.THRESH_TOZERO_INV
  5. blocksize: it signifies the size of region used to calculate threshold value.
  6. constant: it signifies the constant value which subtracted from the mean or weighted mean.

Implemented code in Python:

#importing libraries
import cv2

img = cv2.imread('flower.jpg',0);

thresh10 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY, 101, 2); 
thresh11 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV, 101, 2);

thresh21 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY, 101, 2);
thresh22 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV, 101, 2); 
 
cv2.imshow('ORIGINAL', img);
cv2.imshow('BINARY_ADAPTIVE_THRESH_MEAN_C', thresh10);
cv2.imshow('BINARY_INV__ADAPTIVE_THRESH_MEAN_C', thresh11);
cv2.imshow('BINARY_ADAPTIVE_THRESH_GAUSSIAN_C', thresh21);
cv2.imshow('BINARY_INV_ADAPTIVE_THRESH_GAUSSIAN_C', thresh22);

#wait for 10 seconds 
cv2.waitKey(10000);
cv2.destroyAllWindows();

OUTPUT:

Thank you Rishika Gupta for this article.


0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Insert math as
Block
Inline
Additional settings
Formula color
Text color
#333333
Type math using LaTeX
Preview
\({}\)
Nothing to preview
Insert