Total Pageviews

Friday, September 25, 2026

๐ŸŒ“ Thresholding in Digital Image Processing

๐ŸŒ“ Thresholding in Digital Image Processing

B.Sc. Computer Science Honours | Image Segmentation

๐Ÿ“˜ 1. Introduction

Thresholding is one of the simplest and most important techniques used in Digital Image Processing for separating an object from its background.

The basic idea is to compare the intensity value of each pixel with a selected threshold value T.

Basic idea: Pixels are divided into different classes according to whether their intensity is below or above the threshold.
Grayscale
Image
→
Threshold
T
→
Pixel
Comparison
→
Binary
Image

⚙️ 2. Basic Principle

Let the grayscale intensity of a pixel be represented by f(x,y) and let T be the threshold.

g(x,y) = { 1, if f(x,y) ≥ T
    0, if f(x,y) < T }

Here:

  • f(x,y) = original grayscale pixel value
  • T = threshold value
  • g(x,y) = thresholded output
  • 1 = foreground/object
  • 0 = background
For an 8-bit grayscale image, pixel values normally range from 0 to 255.

๐Ÿ”ข 3. Numerical Example

Consider the following grayscale pixel values:

20, 60, 100, 140, 180, 220

Suppose the threshold is:

T = 128
Pixel Value Comparison Output
20 20 < 128 0
60 60 < 128 0
100 100 < 128 0
140 140 ≥ 128 1
180 180 ≥ 128 1
220 220 ≥ 128 1
Therefore, the output becomes: 0 0 0 1 1 1

๐Ÿ“š 4. Types of Thresholding

1️⃣ Global Thresholding

Uses one threshold value for the entire image.

2️⃣ Local Thresholding

Uses threshold values that can vary across different regions of an image.

3️⃣ Adaptive Thresholding

Automatically calculates a threshold for local neighborhoods.

4️⃣ Otsu's Thresholding

Automatically selects a global threshold by maximizing the separation between two intensity classes.

๐ŸŒ 5. Global Thresholding

In global thresholding, one threshold value is applied throughout the complete image.

T = Constant

Example:

T = 128
If the object and background have clearly different intensity values, global thresholding can work effectively.

๐Ÿ” 6. Local Thresholding

In local thresholding, different regions of an image can use different threshold values.

Image
→
Divide into
Regions
→
Calculate
Local T
→
Binary
Image

Local thresholding is useful when illumination is not uniform across the image.

๐Ÿง  7. Adaptive Thresholding

Adaptive thresholding calculates the threshold based on the local neighborhood of each pixel.

Two commonly used approaches are:

Mean Adaptive Thresholding

Threshold is calculated using the mean of neighboring pixels.

Gaussian Adaptive Thresholding

Uses a weighted average where nearby pixels receive greater importance.

๐Ÿ“Š 8. Otsu's Thresholding

Otsu's method is an automatic threshold-selection technique commonly used for separating an image into two classes.

It searches for a threshold that gives strong separation between the foreground and background classes.

Otsu's method is particularly useful when the image histogram has two relatively distinct intensity groups.

Basic Idea

Step 1: Calculate the grayscale histogram.
Step 2: Consider possible threshold values.
Step 3: Divide pixels into two classes.
Step 4: Calculate within-class or between-class variance.
Step 5: Select the threshold providing the desired maximum separation criterion.

๐Ÿ“ˆ 9. Histogram and Thresholding

A grayscale histogram represents the frequency of different intensity levels in an image.

Image
→
Histogram
→
Select T
→
Segmented
Image
When foreground and background have different intensity distributions, the histogram can help identify a suitable threshold.

⚫⚪ 10. Binary Image Formation

Thresholding commonly converts a grayscale image into a binary image.

Grayscale Image → Threshold → Binary Image

Black Pixel

Usually represented by intensity 0.

White Pixel

Usually represented by intensity 255.

๐Ÿงช 11. Interactive Thresholding Demonstration


Set a pixel value and threshold, then click Apply Threshold.

๐Ÿงฉ 12. Thresholding on an Image Matrix

Consider an 8×8 grayscale image represented by pixel intensities. Thresholding converts every value into either foreground or background.

Click New Matrix to generate a grayscale matrix.

๐Ÿ 13. Beginner Python Code

The following example demonstrates simple global thresholding using OpenCV.

import cv2 # Read image image = cv2.imread("input.jpg") # Convert to grayscale gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Apply threshold T = 128 ret, binary = cv2.threshold( gray, T, 255, cv2.THRESH_BINARY ) # Display images cv2.imshow("Original", image) cv2.imshow("Binary Image", binary) cv2.waitKey(0) cv2.destroyAllWindows()
Explanation:
cv2.threshold() compares the grayscale pixel values with the threshold value. Pixels satisfying the threshold condition are assigned the maximum value, here 255, while the others become 0.

๐Ÿ 14. Beginner Python Code – Otsu Thresholding

import cv2 # Read image image = cv2.imread("input.jpg") # Convert to grayscale gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Otsu thresholding ret, binary = cv2.threshold( gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU ) print("Selected Threshold:", ret) cv2.imshow("Original", image) cv2.imshow("Otsu Binary Image", binary) cv2.waitKey(0) cv2.destroyAllWindows()
Important: With Otsu's method, the threshold is selected automatically rather than manually specifying a fixed threshold such as 128.

๐Ÿ 15. Beginner Python Code – Adaptive Thresholding

import cv2 # Read image image = cv2.imread("input.jpg") # Convert to grayscale gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Adaptive threshold binary = cv2.adaptiveThreshold( gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2 ) cv2.imshow("Original", image) cv2.imshow("Adaptive Threshold", binary) cv2.waitKey(0) cv2.destroyAllWindows()

๐Ÿ“Š 16. Comparison of Thresholding Methods

Method Threshold Suitable Situation
Global One fixed T Relatively uniform illumination
Local Region dependent Different image regions
Adaptive Calculated locally Uneven illumination
Otsu Automatically selected Images with two dominant intensity classes

๐Ÿง  17. Thresholding Algorithm

Step 1: Read the image.
Step 2: Convert the image into grayscale if necessary.
Step 3: Select or calculate a threshold value T.
Step 4: Compare every pixel with T.
Step 5: Assign the required output intensity.
Step 6: Obtain the binary or segmented image.

๐ŸŒ 18. Applications

๐Ÿ“„ Document Processing

Separating text from paper backgrounds.

๐Ÿ”ข OCR

Preparing scanned documents for Optical Character Recognition.

๐Ÿงฌ Medical Images

Isolating regions of interest in some medical images.

๐Ÿญ Industrial Inspection

Detecting objects, defects or regions in manufactured products.

๐Ÿ›ฐ️ Satellite Images

Separating selected regions based on intensity information.

๐ŸŽฅ Object Segmentation

Separating foreground objects from suitable backgrounds.

✅ 19. Advantages

1. Simple and easy to implement.
2. Computationally inexpensive compared with many advanced segmentation techniques.
3. Produces a simple binary representation.
4. Useful as a preprocessing step for OCR and object analysis.
5. Automatic methods such as Otsu reduce the need to manually choose T.

⚠️ 20. Limitations

1. Global thresholding may fail when illumination is uneven.
2. Noise can affect the thresholded result.
3. A poor threshold can remove useful object information.
4. Some images contain overlapping foreground and background intensity distributions.
5. Adaptive methods can require additional computation.

๐Ÿ” 21. Thresholding vs Edge Detection

Feature Thresholding Edge Detection
Main Purpose Separate regions/classes Find intensity boundaries
Basic Principle Compare pixel intensity with T Measure intensity changes
Output Usually binary regions Usually edge map
Common Methods Global, Otsu, Adaptive Sobel, Prewitt, Canny

๐ŸŽ“ 22. Important Examination Points

1. Thresholding is an image segmentation technique.
2. The threshold value is generally represented by T.
3. Global thresholding uses one threshold for the whole image.
4. Adaptive thresholding calculates thresholds locally.
5. Otsu's method automatically selects a threshold using a class-separation criterion.
6. Thresholding is widely used in OCR, document processing, object segmentation and image analysis.

๐Ÿ“Œ 23. Quick Revision Table

Concept Key Point
Threshold Value used to separate intensity classes.
Binary Image Image containing two main intensity levels.
Global Threshold One threshold for the complete image.
Local Threshold Threshold depends on an image region.
Adaptive Threshold Threshold calculated from local neighborhoods.
Otsu Automatic threshold selection based on class separation.
Application Segmentation, OCR, inspection and image analysis.
``` This follows the same **Digital Image Processing educational layout** as your previous topics and is ready to paste directly into the **Blogger HTML editor**.

No comments:

Post a Comment