OpenCV Image Thresholding Techniques – Python

OpenCV Image Thresholding is one of the popularly used technique in OpenCV. Thresholding is the assigning of pixel values in relation to the given threshold value.

It is a segmentation technique, it is used to separate objects. It has two segments on its side which are Below threshold and Above threshold. If the given pixel value is less than the threshold, the pixel is set to zero(0) else it is set to its highest value.

We use the function cv2.threshold for Thresholding in Python. Cv2.threshold accepts 4 arguments.

SYNTAX: cv2.threshold(origination, thresholdvalue, highestvalue, Thresholdmethod) 

  • Origination: Input which is given, image array(first converted in Greyscale)
  • ThresholdValue: We define the below and above value of the threshold, so that pixel value will change depending on it.
  • Highest value: Highest value a pixel can get (255)
  • ThresholdingMethod: The method we are applying.

Basic Thresholding

The basic Thresholding is also called Binary thresholding. In this for every threshold value, we apply the same pixel. If the pixel value is less than the set threshold, it is set to 0, else to the highest value.

Some techniques/methods are :

  1. cv2.THRESH_BINARY – Value is set to 255 only when pixel intensity is more than set threshold value, else 0.
  2. cv2.THRESH_BINARY_INV – This is inverted or opposite of the above.
  3. cv2.THRESH_TOZERO – If pixel intensity is less than the threshold value than pixel intensity is set to zero(0).
  4. cv2.THRESH_TOZERO_INV – Inverted/opposite of above.
  5. cv2.THRESH_TRUNC – When pixel intensity is more than the threshold value, it is than truncated to the threshold value. Other value remains the same.

Python Code for OpenCV Image Thresholding Techniques

Below is the Python program for Image Thresholding Techniques using the OpenCV library:

import cv2  
import numpy as np
  
#Image is added 
# We load it with imread command  

picture = cv2.imread('Test.jpg')  

# cv2.cvtColor is used 
# to convert the image in grayscale and
# with given parameters 

img = cv2.cvtColor(picture,cv2.COLOR_BGR2GRAY) 

  
# Now we apply a different threshold
# methods and if pixel value greater than 100 
# it will be set to 255

ret, threshtest = cv2.threshold(img, 100, 255, cv2.THRESH_BINARY) 

ret, threshtest1 = cv2.threshold(img, 100, 255, cv2.THRESH_BINARY_INV) 

ret, threshtest2 = cv2.threshold(img, 100, 255, cv2.THRESH_TOZERO) 

ret, threshtest3 = cv2.threshold(img, 100, 255, cv2.THRESH_TOZERO_INV) 

ret, threshtest4 = cv2.threshold(img, 100, 255, cv2.THRESH_TRUNC)   

#  output images after 
# each method applied to the input images 

cv2.imshow('1 Binary Threshold output', threshtest) 

cv2.imshow('2 Binary Threshold Inverted output', threshtest1) 

cv2.imshow('3 Threshold Set to 0 output', threshtest2) 

cv2.imshow('Threshold Set to 0 Inverted output', threshtest3) 

cv2.imshow('Truncated Threshold output', threshtest4) 

    
# Free the memory, Deallocating

if cv2.waitKey(0) & 0xff == 25:  

    cv2.destroyAllWindows()
  • Here we have applied each and every method we discussed above.
  • You may set other value instead of 100
  • Some function is inbuilt in Cv2.
  • The image must be set properly otherwise it may not be able to call it.

 

Output

The left corner image is an original image 
And the rest is the different methods applied

Now if we run the code, we will able to see output images that you can see below:

Image Thresholding

 

Try to run the code and if you have any doubt you may drop a comment. Your feedback will be appreciated.

Leave a Reply