Skip to content
Tactiverge

School/○ Innovator/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

1.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.

2.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.

Loading the interactive…

Can't use the interactive? Read a description instead

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.

3.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1

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

4.Predict

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

Guess first. Being wrong here is how the surprise works.

5.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

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.

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.

6.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 2

A 4-pixel row has values 30, 140, 200, 90. With T = 100, how many pixels are object?

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?

7.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.

8.Reflect

Concepts: Thresholding and simple classification · Images as arrays of numbers