Friday, December 27, 2024
Google search engine
HomeLanguagesImage Thresholding in Python OpenCV

Image Thresholding in Python OpenCV

Image Thresholding is an intensity transformation function in which the values of pixels below a particular threshold are reduced, and the values above that threshold are boosted.  This generally results in a bilevel image at the end, where the image is composed of black and white pixels. Thresholding belongs to the family of point-processing techniques. In this article, you will learn how to perform Image Thresholding in OpenCV.

There are various ways of performing thresholding (Adaptive, Inverse, etc.), but the primary focus of this article will be on binary thresholding and would touch upon other thresholding methods in the end. For binary thresholding, we would be using the cv2.THRESH_BINARY flag in cv2.threshold function. 

For demonstration purposes, we would be using an image named test.jpg. Let’s see the code to show the image.

Python3




import cv2
 
# Loading the image named test.jpg
img = cv2.imread(r"ex2.jpg")
 
# Converting color mode to Grayscale
# as thresholding requires a single channeled image
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('Image', img)


Output:

 

Binary Thresholding

The function takes in argument a source image, threshold at which the cutoff has to take place, maximum intensity value represented by the color space, the mode of thresholding and returns an integer value (denoting result of the operation) and an image object containing the resultant image after the processing. 

Python3




# Thresholding the image placing 127 intensity level as threshold
# Pixel values below 127 would be changed to Black
# Pixel values above 127 would be changed to White (255)
ret, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
 
# Displaying the output image
cv2.imshow('Binary Threshold', thresh)


Output: 

 

Binary – Inverse Thresholding

In this, the output will be the inverse of above output i.e. white pixel will be black and vice-versa.

Python3




ret, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)
 
# Displaying the output image
cv2.imshow('Binary Inverse Threshold', thresh)


Output:

 

Truncate Thresholding

Similarly, the code for Truncate will be

Python3




ret, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_TRUNC)
 
# Displaying the output image
cv2.imshow('Truncate Threshold', thresh)


Output:

 

Zero Thresholding

Similarly, the code for Zero Thresholding will be :

Python3




ret, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO)
 
# Displaying the output image
cv2.imshow('Truncate Threshold', thresh)


Output:

 

Inverse Thresholding to Zero

In this , we will do for Inverse Thresholding to zero .

Python3




ret, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO_INV)
 
# Displaying the output image
cv2.imshow('Truncate Threshold', thresh)


Output:

 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments