Lite / Intelligent Robotic Systems / Week 24
Thresholding: from picture to mask
Draw one line through the brightness scale and every pixel becomes 'object' or 'not object'. Where should the line go?
Why should I care?
Thresholding is the simplest decision a vision system makes, and it is everywhere: line-following robots, barcode readers, bottle-fill checks. It is also where you first meet the core trade-off of every classifier: wherever you draw the line, some pixels land on the wrong side.
- Segment an image into a binary mask with a brightness threshold.
- Choose a threshold from a histogram of pixel values.
- Explain why noise and uneven lighting break a single global threshold.
Predict
An object is about 200 bright on a background of about 40, both with noise ±45. Which threshold separates them best?
- T = 60
- T = 120
- T = 230
Guess first, then reveal
Halfway between the two levels, 120, leaves the most room for noise on both sides. 60 catches noisy background pixels; 230 misses most of the object.
Discover
Choose Threshold. On the Bright square, sweep T from 0 to 255 and note the range that gives a perfect mask. Switch to the Noisy bar: is there still a perfect threshold? Count the mistakes at T = 100, 120 and 140.
What the interactive shows (text version)
In threshold mode the right-hand grid shows the mask: 255 for pixels at or above T, 0 below. For the Bright square any T from 45 to 200 gives a perfect 4 × 4 block. For the Noisy bar, background pixels (around 60 ± 45) and bar pixels (around 170 ± 45) overlap, so every threshold misclassifies a few pixels; values near 115 make the fewest mistakes.
Understand
A threshold is a rule
mask[r][c] = 1 if img[r][c] ≥ T, else 0. The result is a binary mask: object pixels and background pixels. Everything later, from counting objects to measuring them, works on this mask, so mistakes here spread through the whole pipeline.
Let the histogram choose
A histogram of pixel values for a well-lit scene often has two humps: background and object. A good threshold sits in the valley between them. Automatic methods such as Otsu's choose T to make the two groups as internally similar as possible, which usually lands in that valley.
When one threshold fails
Noise makes the humps overlap, so some pixels are always misclassified. Uneven lighting shifts brightness across the image, so a threshold right on the left is wrong on the right. Fixes: control the lighting, smooth before thresholding, or use an adaptive threshold computed from each region's neighbourhood.
Explain it another way
Intuitive
Sorting a pile of grey pebbles into 'light' and 'dark': most are easy, but the in-between ones depend exactly where you draw the line, and in dim light everything looks darker.
Technical
Global thresholding is pixel-wise classification on intensity. Otsu's method maximises the between-class variance of the histogram split; adaptive thresholding compares each pixel with a local mean minus a constant.
Applied
Line-following robots threshold an infrared reflectance reading; they still need recalibrating when moved from a bright classroom to a dim hall, because ambient light shifts both humps.
Worked example 1
Background pixels range 30–90; object pixels range 150–220. Suggest T and justify.
Anywhere from 91 to 150 separates them perfectly. The midpoint of the gap, about 120, leaves the most margin for unseen noise on either side.
Common mix-ups
There is always a perfect threshold if you search hard enough.
✓ When the value ranges of object and background overlap, every threshold makes some mistakes; you can only choose which ones.Thresholds tuned on one image work everywhere.
✓ Lighting, camera settings and surfaces change; test thresholds on images you didn't tune on.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
Apply it
In Python, threshold a greyscale image of coins or pieces of paper on a dark table at three values. For each, count the object pixels and describe the mistakes. Then write a function that picks T as the midpoint between the two most common brightness levels and compare it with your best manual choice.
Reflect
Think of a situation where it would be better to wrongly mark background as object than the reverse.
Write your answer on paper or in your own notes. Lite never stores what you write.