Change language

Python | Threshold Methods Using OpenCV | Set-1 (simple thresholding)

| | |

Thresholding — it is a technique in OpenCV that is the assignment of pixel values ​​in relation to a provided threshold value. In a threshold value, each pixel value is compared to a threshold value. If the pixel value is less than the threshold value, it is set to 0, otherwise it is set to the maximum value (usually 255). Thresholding — a very popular segmentation technique used to separate the foreground object from the background. Threshold — it is the value that has two areas on either side, that is, below the threshold or above the threshold. 
Computer Vision performs this thresholding on grayscale images. Therefore, initially the image must be converted to a grayscale color space.

 If f (x, y)" T then f (x, y) = 0 else f (x, y) = 255 where f (x, y) = Coordinate Pixel Value T = Threshold Value. 

OpenCV with Python uses the cv2.threshold function for the threshold.

Syntax: cv2.threshold ( source, thresholdValue, maxVal, thresholdingTechnique)

-"  source : Input Image array (must be in Grayscale).
-"  thresholdValue : Value of Threshold below and above which pixel values ​​will change accordingly.
-"  maxVal : Maximum value that can be assigned to a pixel.
-"  thresholdingTechnique : The type of thresholding to be applied.

Simple Threshold

Basic Thresholding — binary threshold. The same threshold is applied to each pixel. If the pixel value is less than the threshold value, it is set to 0, otherwise it is set to the maximum value.

Various simple thresholding techniques:

  • cv2. THRESH_BINARY : If the pixel intensity exceeds the set threshold, the value is set to 255, otherwise it is set to 0 (black).
  • cv2.THRESH_BINARY_INV : Inverted or opposite case cv2.THRESH_BINARY .
  • cv.THRESH_TRUNC : If the pixel intensity value exceeds the threshold, it is truncated to the threshold. The pixel values ​​are set equal to the threshold. All other values ​​remain the same.
  • cv.THRESH_TOZERO : Pixel intensity is set cv.THRESH_TOZERO 0, for the intensity of all pixels is less than the threshold.
  • cv.THRESH_TOZERO_INV : inverted or opposite case cv2.THRESH_TOZERO .

Below is Python code explaining various simple threshold level techniques —

# Python illustration program
# simple type of threshold on the image

# organization of import

< p> import cv2 

import numpy as np 

# path to input image and
# the image is loaded using the imread command

image1 = cv2.imread ( ’input1.jpg’

# cv2.cvtColor is applied over
# input image with applied parameters
# convert image to grayscale

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

# applying different thresholds
# technique on input image
# all pixels above 120 will
# be set to 255

ret, thresh1 = cv2.threshold (img, 120 , 255 , cv2.THRESH_BINARY)

ret, thresh2 = cv2.threshold (img, 120 , 255 , cv2.THRESH_BINARY_INV )

ret, thresh3 = cv2. threshold (img, 120 , 255 , cv2.THRESH_TRUNC)

ret, thresh4 = cv2.threshold (img, 120 , 255 , cv2.THRESH_TOZERO)

ret, thresh5 = cv2.threshold (img, 120 , 255 , cv2.THRESH_TOZERO_INV)

# window with image output
# with corresponding threshold
# methods applied to input images

cv2.imshow ( ’Binary Threshold’ , thresh1)

cv2.imshow ( ’ Binary Threshold Inverted’ , thresh2)

cv2.imshow ( ’Truncated Threshold’ , thresh3)

cv2.imshow ( ’Set to 0’ , thresh4)

cv2.imshow ( ’Set to 0 Inverted’ , thresh5)

# Deselect any associated memory usage

if cv2.waitKey ( 0 ) & amp;  0xff = = 27

cv2.destroyAllWindows () 

Input data :


/ figure>