TactivergeLite

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?

About 35 min · 3 goals · Checked by automated tests · Full interactive version

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.

Predict

An object is about 200 bright on a background of about 40, both with noise ±45. Which threshold separates them best?

  1. T = 60
  2. T = 120
  3. 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.

You can also open the interactive on the full site when you have a faster connection.

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

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

Practice 1. With T = 128, what does a pixel of value 128 become?

Practice 3. Parts on the left of a conveyor are lit twice as brightly as on the right. A single threshold…
Practice 4. A pixel histogram has humps centred at 50 and 180, with very few pixels between 100 and 130. Where should T go?

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.

← Images are arrays of numbersColour spaces, HSV and the module check →