Thresholding
In the previous lesson, we learned how histograms describe the distribution of pixel intensities in an image.
Now we take the next logical step: using those intensity values to separate objects from background.
This process is called thresholding.
Thresholding is one of the most important and fundamental ideas in Computer Vision. It is simple, fast, and extremely powerful.
What Is Thresholding?
Thresholding is a technique used to convert a grayscale image into a binary image.
Instead of many gray levels, the image is reduced to only two values:
- Black (0)
- White (255)
The decision is made using a single value called the threshold.
The Core Idea (Very Important)
Thresholding follows a simple rule:
- If pixel value ≥ threshold → white
- If pixel value < threshold → black
That’s it.
This single rule allows a computer to separate foreground objects from background.
Even though the idea is simple, it forms the base of many advanced CV pipelines.
Why Thresholding Works
In many images, objects and background have different intensity ranges.
For example:
- Text is darker than paper
- Road markings are brighter than asphalt
- Medical regions of interest differ from surrounding tissue
When this difference exists, a threshold can cleanly separate them.
Thresholding and Histograms (Connection)
Thresholding decisions are often guided by histograms.
A histogram may show two clear peaks:
- One peak for background pixels
- One peak for object pixels
The threshold is chosen between these peaks.
This is why histogram analysis usually comes before thresholding.
Types of Thresholding
There are several types of thresholding, each suited for different scenarios.
1. Binary Thresholding
The most common and basic type.
- Pixels above threshold → white
- Pixels below threshold → black
Used when object and background are clearly separable.
2. Inverse Binary Thresholding
This is the opposite of binary thresholding.
- Pixels above threshold → black
- Pixels below threshold → white
Useful when objects are darker than background and you want them highlighted.
3. Truncation Thresholding
In this case:
- Pixels above threshold are set to the threshold value
- Pixels below threshold remain unchanged
This is used to limit extreme brightness values.
4. To-Zero Thresholding
Pixels below the threshold are removed (set to zero), while others stay unchanged.
Useful when you want to suppress noise but keep strong features.
Global Thresholding
In global thresholding, a single threshold value is applied to the entire image.
This works well when:
- Lighting is uniform
- Background is consistent
However, global thresholding struggles with uneven lighting.
Limitations of Simple Thresholding
Thresholding is powerful, but it is not perfect.
Problems occur when:
- Lighting varies across the image
- Shadows are present
- Object and background intensities overlap
These limitations lead us to more advanced techniques, which we will study later.
Real-World Applications
- Document scanning and OCR
- License plate detection
- Medical image segmentation
- Industrial defect detection
- Preprocessing for contour detection
In many systems, thresholding is the very first step.
Thresholding vs Edge Detection
Thresholding separates regions.
Edge detection finds boundaries.
Both are complementary techniques and are often used together in pipelines.
Common Beginner Mistakes
- Using a fixed threshold for all images
- Ignoring histogram analysis
- Applying thresholding directly on noisy images
Good preprocessing improves thresholding results.
Practice Questions
Q1. What is the output of thresholding?
Q2. Why are histograms useful before thresholding?
Q3. When does global thresholding fail?
Quick Quiz
Q1. What happens to pixels below the threshold in binary thresholding?
Q2. Is thresholding usually applied to grayscale or color images?
Key Takeaways
- Thresholding converts grayscale images to binary
- A threshold decides foreground vs background
- Histograms guide threshold selection
- Global thresholding is simple but limited
- Thresholding is a foundation for segmentation
In the next lesson, we will study smoothing and blurring, which help reduce noise before thresholding and edge detection.