OpenCV Tutorial

Image Processing

Feature Detection and Description

Drawing Functions

Video Processing

Applications and Projects

Otsu Thresholding in OpenCV

Otsu's thresholding is an automatic thresholding method that calculates an "optimal" threshold by maximizing the variance between two classes of pixels. It's particularly effective when the histogram of the image has a bimodal distribution (i.e., two major peaks). OpenCV offers built-in support for Otsu's thresholding.

Otsu's Thresholding in OpenCV:

1. Setup:

Firstly, make sure you have OpenCV installed:

pip install opencv-python

Then, import the necessary libraries:

import cv2

2. Read the Image & Convert to Grayscale:

Otsu's thresholding works on grayscale images:

# Read the image
image = cv2.imread('path_to_image.jpg')

# Convert to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

3. Apply Otsu's Thresholding:

Use the cv2.threshold() function with the cv2.THRESH_OTSU flag:

ret, thresh = cv2.threshold(gray_image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

Here:

  • gray_image is the input grayscale image.
  • 0 is the initial threshold value, but it's ignored because of the cv2.THRESH_OTSU flag.
  • 255 is the max value, typically representing white.
  • The combination of cv2.THRESH_BINARY and cv2.THRESH_OTSU flags ensures binary thresholding with Otsu's method.

The function will return the optimal threshold value (in ret) found by Otsu's method and the thresholded image (thresh).

4. Display the Result:

cv2.imshow('Original Image', gray_image)
cv2.imshow('Otsu Thresholding', thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()

Complete Code:

Here's a complete script for Otsu's thresholding:

import cv2

# Load the image and convert to grayscale
image = cv2.imread('path_to_image.jpg')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Apply Otsu's thresholding
ret, thresh = cv2.threshold(gray_image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

# Display the result
cv2.imshow('Original Image', gray_image)
cv2.imshow('Otsu Thresholding', thresh)
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 Otsu's thresholded images will be displayed side by side.

Otsu's method assumes that the image contains two classes of pixels following a bi-modal histogram, so it might not give desirable results if this assumption doesn't hold.

  1. Otsu's method for thresholding in OpenCV:

    import cv2
    
    # Read an image in grayscale
    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 thresholded images
    cv2.imshow('Original Image', img)
    cv2.imshow('Otsu Thresholded Image', otsu_thresh)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
  2. Otsu thresholding vs. other thresholding techniques in OpenCV:

    import cv2
    
    # Read an image in grayscale
    img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
    
    # Global thresholding
    _, global_thresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
    
    # Adaptive thresholding
    adaptive_thresh = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
    
    # Otsu's thresholding
    _, otsu_thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    
    # Display the original, global thresholded, adaptive thresholded, and Otsu thresholded images
    cv2.imshow('Original Image', img)
    cv2.imshow('Global Thresholded Image', global_thresh)
    cv2.imshow('Adaptive Thresholded Image', adaptive_thresh)
    cv2.imshow('Otsu Thresholded Image', otsu_thresh)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
  3. Real-time Otsu 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 Otsu's thresholding
        _, otsu_thresh = cv2.threshold(gray_frame, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    
        # Display the original and Otsu thresholded frames
        cv2.imshow('Original Frame', frame)
        cv2.imshow('Otsu Thresholded Frame', otsu_thresh)
    
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    
    cap.release()
    cv2.destroyAllWindows()
    
  4. Otsu's thresholding for image segmentation in OpenCV:

    import cv2
    
    img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
    
    # Apply Otsu's thresholding for image segmentation
    _, otsu_thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    
    # Perform additional processing on the segmented image as needed
    
    # Display the original and segmented images
    cv2.imshow('Original Image', img)
    cv2.imshow('Segmented Image', otsu_thresh)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
  5. Optimal threshold selection using Otsu's method:

    import cv2
    
    img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
    
    # Compute optimal threshold using Otsu's method
    _, optimal_thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    
    # Display the original and optimal thresholded images
    cv2.imshow('Original Image', img)
    cv2.imshow('Optimal Thresholded Image', optimal_thresh)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
  6. Otsu thresholding for noise reduction in OpenCV:

    import cv2
    
    img = cv2.imread('noisy_image.jpg', cv2.IMREAD_GRAYSCALE)
    
    # Apply Otsu's thresholding for noise reduction
    _, otsu_thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    
    # Display the original and thresholded images
    cv2.imshow('Original Image', img)
    cv2.imshow('Otsu Thresholded Image', otsu_thresh)
    cv2.waitKey(0)
    cv2.destroyAllWindows()