Simple Thresholding using OpenCV
Last Updated :
18 Aug, 2025
Thresholding is a foundational technique in computer vision and image processing used to segment objects from the background. It works by comparing each pixel value of a grayscale image against a specified threshold value. Based on this comparison, pixels are assigned new values, usually 0 (black) or 255 (white).In OpenCV with Python, the function cv2.threshold is used for thresholding.
In thresholding, for every pixel at position (x,y) with an intensity value f(x,y):
- If f(x,y) < T, set the pixel to 0 (black).
- If f(x,y) ≥ T, set it to the maximum value (typically 255, white).
- Here, T is the threshold value, and the process is usually performed on a grayscale version of the image
Technique | Description |
---|
cv2.THRESH_BINARY | Above threshold -> 255, below -> 0. |
cv2.THRESH_BINARY_INV | Above threshold -> 0, below -> 255 (inverse of binary). |
cv2.THRESH_TRUNC | Above threshold -> set to threshold, below -> unchanged. |
cv2.THRESH_TOZERO | Below threshold -> 0, above -> unchanged. |
cv2.THRESH_TOZERO_INV | Above threshold -> 0, below -> unchanged (inverse of TOZERO). |
Step-by-Step Implementation
Let's implement the various types of simple thresholding techniques,
Step 1: Import libraries and Image Preparation
Sample image can be downloaded from here.
Let's import the required libraries and load our image on which we will perform the operations,
- cv2: Handles image reading, processing, and applies thresholding techniques.
- numpy: Supports efficient array operations, enabling fast image data handling.
- matplotlib.pyplot: Displays images and results in Colab notebooks.
Python
import cv2
import numpy as np
import matplotlib.pyplot as plt
image = cv2.imread('input.png')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
Step 2: Helper Function
Define the helper function which helps in displaying the images,
Python
def show_image(img, title):
plt.imshow(img, cmap='gray')
plt.title(title)
plt.axis('off')
plt.show()
Step 3: Display the Original Image
Python
show_image(gray_image, 'Original Grayscale Image')
Output:
Original Grayscale ImageStep 4: Binary Threshold
- If a pixel’s intensity is above the threshold, it is assigned the maximum value (usually 255; white)—otherwise, it becomes 0 (black).
- Useful for separating bright objects from a dark background.
Python
_, thresh_binary = cv2.threshold(gray_image, 120, 255, cv2.THRESH_BINARY)
show_image(thresh_binary, 'Binary Threshold ')
Output:
Image after Binary ThresholdStep 5: Binary Threshold Inverted
- This works opposite to the binary threshold. Pixels above the threshold become 0 (black), and those below become the maximum value (255; white).
- Highlights background over foreground.
Python
_, thresh_binary_inv = cv2.threshold(
gray_image, 120, 255, cv2.THRESH_BINARY_INV)
show_image(thresh_binary_inv, 'Binary Threshold Inverted ')
Output:
Image after Binary Threshold InvertedStep 6: Truncated Threshold
- Pixels above the threshold take the threshold value itself. Pixels below the threshold remain unchanged.
- Limits the maximum brightness, useful for flattening overexposed regions.
Python
_, thresh_trunc = cv2.threshold(gray_image, 120, 255, cv2.THRESH_TRUNC)
show_image(thresh_trunc, 'Truncated Threshold')
Output:
Image after Truncated ThresholdStep 7: To Zero Threshold
- Pixels below the threshold are set to 0. Pixels above the threshold keep their original intensity.
- Extracts the brighter parts of the image while suppressing darker regions.
Python
_, thresh_tozero = cv2.threshold(gray_image, 120, 255, cv2.THRESH_TOZERO)
show_image(thresh_tozero, 'Set to 0 ')
Output:
Image after pixels below Threshold are set to 0Step 8: To Zero Inverted Threshold
- Pixels above the threshold are set to 0. Pixels below the threshold keep their original value.
- Retains darker details while masking the brighter ones.
Python
_, thresh_tozero_inv = cv2.threshold(
gray_image, 120, 255, cv2.THRESH_TOZERO_INV)
show_image(thresh_tozero_inv, 'Set to 0 Inverted')
Output:
Image after pixels above Threshold are set to 0
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice