Separate foreground from background
Turn an image into a foreground mask with a threshold, and pick the threshold from a histogram.
- Apply a threshold to make a binary mask
- Read a brightness histogram and choose a threshold from it
- Explain why uneven lighting breaks a single threshold
Scanning a document, counting cells under a microscope, finding a part on a conveyor belt: often the first job is to separate the foreground (the things you care about) from the background. The simplest tool is a threshold: every pixel at least as bright as a cutoff becomes 1 (foreground), the rest 0. The result is a binary mask - a simple form of segmentation.
pixels = [20, 90, 140, 230]
mask = [1 if value >= 128 else 0 for value in pixels]
print(mask)[0, 0, 1, 1]
Choosing the threshold
A histogram counts how many pixels have each brightness. When objects and background differ clearly, it shows two humps, and a good threshold sits in the valley between them. Otsu’s method finds that valley automatically, by picking the cutoff that makes the two groups as different as possible.
Try it
Up to 15 XPThreshold slider
This scene has a bright square (top left) and a bright disc (bottom right) - but it is lit unevenly: darker on the left, brighter on the right.
- Slide the threshold to capture both objects and nothing else. Can you?
- Try Otsu’s automatic threshold. Does an automatic method fix uneven lighting?
- Watch the gray and blue bars: the threshold splits the histogram in two.
Histogram: how many pixels have each brightness
No single cutoff works everywhere in that scene: the bright right side of the background is as bright as the square on the dark left side. Adaptive thresholding fixes this by computing a separate threshold for each neighborhood (for example, “brighter than the local average”). OpenCV’s thresholding guide shows both methods on real photos.
Key takeaways
A threshold turns brightness into a yes/no mask - the simplest segmentation.
A histogram shows the brightness distribution; a good cutoff sits between the humps (Otsu finds it automatically).
Uneven lighting needs adaptive thresholds that vary across the image.
Lesson quiz
6 questions · pass with 5 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: apply computer vision with Python
Use small pixel arrays to explore vision concepts, run your code against sample images, and connect each result to the larger computer vision idea.
Build a binary mask
Read a JSON list of grayscale values and an integer threshold on the next line. Print a list with 1 where a pixel is at least the threshold and 0 otherwise.
- Split dark and light
- Include the cutoff
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Build a brightness histogram
Read a JSON grayscale image. Count its pixels in four brightness bins - 0–63, 64–127, 128–191 and 192–255 - and print the four counts separated by spaces.
- Dark and bright
- All mid-gray
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…