OpenCV Tutorial
Image Processing
Feature Detection and Description
Drawing Functions
Video Processing
Applications and Projects
Thresholding is a process in image processing where pixel values in an image are set to a specific value based on a certain condition. Simple thresholding as the name suggests involves applying a single threshold value. If the pixel value is above the threshold, it's set to one value (possibly white), and if it's below the threshold, it's set to another value (maybe black).
In OpenCV, this can be achieved with the cv2.threshold()
function.
Make sure you have OpenCV installed:
pip install opencv-python
Then, import the necessary libraries:
import cv2 import numpy as np
For thresholding, it's usually necessary to work with a grayscale image:
# Read the image image = cv2.imread('path_to_image.jpg') # Convert to grayscale gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
Use the cv2.threshold()
function:
# Apply thresholding ret, thresh1 = cv2.threshold(gray_image, 127, 255, cv2.THRESH_BINARY)
Here:
gray_image
is the grayscale image.127
is the threshold value.255
is the max value that is used to represent white.cv2.THRESH_BINARY
is the type of thresholding. This will result in a binary image where pixels with a value greater than 127 are set to 255 (white) and others are set to 0 (black).cv2.imshow('Original Image', gray_image) cv2.imshow('Binary Threshold', thresh1) cv2.waitKey(0) cv2.destroyAllWindows()
OpenCV provides various thresholding methods, and you can experiment with them:
cv2.THRESH_BINARY
cv2.THRESH_BINARY_INV
(Inverse binary thresholding)cv2.THRESH_TRUNC
(Truncates values above the threshold)cv2.THRESH_TOZERO
(Set to zero below the threshold)cv2.THRESH_TOZERO_INV
(Inverse of the above)Here's the complete code for simple binary thresholding:
import cv2 import numpy as np # Load the image and convert to grayscale image = cv2.imread('path_to_image.jpg') gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Apply thresholding ret, thresh1 = cv2.threshold(gray_image, 127, 255, cv2.THRESH_BINARY) # Display the result cv2.imshow('Original Image', gray_image) cv2.imshow('Binary Threshold', thresh1) cv2.waitKey(0) cv2.destroyAllWindows()
Replace 'path_to_image.jpg'
with the path to your image file and run the script. The original grayscale and thresholded images will be displayed side by side.
Simple thresholding in OpenCV using Python:
import cv2 import numpy as np # Read an image in grayscale img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE) # Apply simple thresholding _, thresholded = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) # Display the original and thresholded images cv2.imshow('Original Image', img) cv2.imshow('Thresholded Image', thresholded) cv2.waitKey(0) cv2.destroyAllWindows()
Thresholding algorithms comparison in OpenCV:
import cv2 import numpy as np img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE) # Apply different thresholding methods _, binary = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) _, binary_inv = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV) _, trunc = cv2.threshold(img, 127, 255, cv2.THRESH_TRUNC) _, tozero = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO) _, tozero_inv = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO_INV) # Display the results cv2.imshow('Binary Threshold', binary) cv2.imshow('Binary Inverted Threshold', binary_inv) cv2.imshow('Truncated Threshold', trunc) cv2.imshow('To Zero Threshold', tozero) cv2.imshow('To Zero Inverted Threshold', tozero_inv) cv2.waitKey(0) cv2.destroyAllWindows()
Adaptive simple thresholding in OpenCV:
import cv2 img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE) # Apply adaptive thresholding adaptive_thresh = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2) # Display the original and adaptive thresholded images cv2.imshow('Original Image', img) cv2.imshow('Adaptive Thresholded Image', adaptive_thresh) cv2.waitKey(0) cv2.destroyAllWindows()
Simple thresholding for image segmentation in OpenCV:
import cv2 img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE) # Apply simple thresholding for image segmentation _, segmented_img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) # Perform additional processing on the segmented image as needed # Display the original and segmented images cv2.imshow('Original Image', img) cv2.imshow('Segmented Image', segmented_img) cv2.waitKey(0) cv2.destroyAllWindows()
Otsu's method for thresholding in OpenCV:
import cv2 img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE) # Apply Otsu's thresholding _, otsu_thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) # Display the original and Otsu's thresholded images cv2.imshow('Original Image', img) cv2.imshow('Otsu Thresholded Image', otsu_thresh) cv2.waitKey(0) cv2.destroyAllWindows()
Real-time simple thresholding in OpenCV:
import cv2 cap = cv2.VideoCapture(0) # Use webcam, or replace with video path while True: ret, frame = cap.read() if not ret: break # Convert the frame to grayscale gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Apply simple thresholding _, thresholded = cv2.threshold(gray_frame, 127, 255, cv2.THRESH_BINARY) # Display the original and thresholded frames cv2.imshow('Original Frame', frame) cv2.imshow('Thresholded Frame', thresholded) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
Simple thresholding for noise reduction in OpenCV:
import cv2 img = cv2.imread('noisy_image.jpg', cv2.IMREAD_GRAYSCALE) # Apply simple thresholding for noise reduction _, denoised_img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) # Display the original and denoised images cv2.imshow('Original Image', img) cv2.imshow('Denoised Image', denoised_img) cv2.waitKey(0) cv2.destroyAllWindows()
Thresholding grayscale and color images with OpenCV:
import cv2 # Grayscale image thresholding img_gray = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE) _, thresh_gray = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY) # Color image thresholding img_color = cv2.imread('color_image.jpg') _, thresh_color = cv2.threshold(img_color, 127, 255, cv2.THRESH_BINARY) # Display the results cv2.imshow('Grayscale Thresholded Image', thresh_gray) cv2.imshow('Color Thresholded Image', thresh_color) cv2.waitKey(0) cv2.destroyAllWindows()