avatarAmit Chauhan

Summary

The web content provides an overview of image thresholding techniques, particularly focusing on the Otsu method and its implementation in Python using OpenCV, along with adaptive thresholding methods.

Abstract

The article discusses the concept of image thresholding in the context of computer vision, an essential step in image segmentation for separating objects from the background. It introduces OpenCV, a comprehensive library for image processing, and explains how to install it using Anaconda. The author delves into different thresholding methods, including simple thresholding, adaptive thresholding (mean and Gaussian), and the Otsu method, which automatically calculates an optimal threshold for bimodal images. The article also includes Python code examples to demonstrate the application of these techniques, accompanied by visual outputs and histograms to illustrate the effects of each method. Additionally, the author encourages readers to connect on LinkedIn and Twitter for further engagement.

Opinions

  • The author emphasizes the importance of thresholding in image segmentation and its role in machine learning and deep learning applications.
  • The Otsu method is presented as a superior approach for bimodal images, as it automatically determines the optimal threshold without manual input.
  • Adaptive thresholding methods are recommended for images where a global threshold is not suitable, suggesting that these methods can better handle varying illumination and contrast conditions.
  • The use of Gaussian filtering in conjunction with the Otsu method is suggested to improve thresholding results on images with noise or gradual intensity changes.
  • The article is structured to be accessible to readers with an interest in artificial intelligence and image processing, providing both theoretical explanations and practical code examples.
  • By providing links to the author's LinkedIn and Twitter profiles, the author invites further discussion and professional networking within the field of data science and AI.

OpenCV: Adaptive and Otsu Threshold in Image Processing with Python

Image pre-processing techniques in artificial intelligence

Otsu Image Threshold. An image by the Author

What is OpenCV?

This library is a nutshell and robust collection of image processing methods that drive the vision application in machine learning and deep learning.

To install the OpenCV library in anaconda

pip install opencv-python

What is the image threshold?

In computer vision applications i.e. image segmentation is an important aspect in which the object in the image is separated into foreground and background.

To make an effective threshold need a grayscale image of one channel in which each image pixels value lies between 0 to 255. The value going toward zero considers white and the value going toward 255 considers black.

The Otsu method is an automatic threshold value optimizer from the bimodal images. The bimodal images are the pixels value that differentiates the image in the foreground(object) and background.

The example of a bimodal image that contains two distributions in the histogram defines the separation of two objects in the grayscale image.

Before Otsu threshold methods

Two threshold methods before Otsu do the threshold based on the threshold value as input in the method that makes the image pixels value into two parts i.e. making the binary image.

  1. Simple threshold: In this type of threshold we have to specify the threshold value and it applies to each pixel of the image. If the pixel value is smaller than the threshold then the pixel value becomes 0 i.e. white and if the pixel value is greater than the threshold then the pixel value is set to 255 i.e. black.

Example of a simple threshold:

Simple threshold methods. An image by the author

2. Adaptive Threshold: In this type of threshold, the pixel of the image is replaced with the mean and weighted sum of the neighborhood pixels. The type adaptive thresholds are Adaptive Mean Threshold and Adaptive Gaussian Threshold.

Example of adaptive threshold:

Adaptive threshold methods. An image by the author

Otsu threshold

In the previous threshold methods, we used to choose the global threshold value or give the size of the block and constant in case of adaptive.

In the case of Otsu, we need a bimodal image and its histogram shows two peaks so that the otsu method can find the global threshold value automatically.

Python Example:

# openCV library 
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

# reading the image from the same directory
img = cv.imread('13.png', cv.IMREAD_GRAYSCALE)

# threshold with the simple binary method
ret1,simp_thresh = cv.threshold(img,127,255,cv.THRESH_BINARY)

# Otsu's threshold without any filter
ret2,simp_thresh_otsu = cv.threshold(img,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)

# Otsu's threshold using Gaussian filtering
blur = cv.GaussianBlur(img,(5,5),0)
ret3,otsu_filter = cv.threshold(blur,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)

# histogram of images
images = [img, 0, simp_thresh,
          img, 0, simp_thresh_otsu,
          blur, 0, otsu_filter]

titles = ['Original Image','Histogram','Global Thresholding (v=127)',
          'Original Image','Histogram',"Otsu's Thresholding",
          'Gaussian filtered Image','Histogram',"Otsu's Thresholding"]

# using a loop to visualize the different types of methods
for i in range(3):
    plt.figure(figsize=(10,6))
    plt.subplot(3,3,i*3+1),plt.imshow(images[i*3],'gray')
    plt.title(titles[i*3]), plt.xticks([]), plt.yticks([])
    plt.subplot(3,3,i*3+2),plt.hist(images[i*3].ravel(),256)
    plt.title(titles[i*3+1]), plt.xticks([]), plt.yticks([])
    plt.subplot(3,3,i*3+3),plt.imshow(images[i*3+2],'gray')
    plt.title(titles[i*3+2]), plt.xticks([]), plt.yticks([])
    
plt.show()

Output:

Output Image of Otsu method. An image by the author

I hope you like the article. Reach me on my LinkedIn and twitter.

Recommended Articles

  1. Most Usable NumPy Methods with Python 2. NumPy: Linear Algebra on Images 3. Exception Handling Concepts in Python 4. Pandas: Dealing with Categorical Data 5. Hyper-parameters: RandomSeachCV and GridSearchCV in Machine Learning 6. Fully Explained Linear Regression with Python 7. Fully Explained Logistic Regression with Python 8. Data Distribution using Numpy with Python 9. 40 Most Insanely Usable Methods in Python 10. 20 Most Usable Pandas Shortcut Methods in Python
Python
Machine Learning
Deep Learning
Artificial Intelligence
Programming
Recommended from ReadMedium