Um momento
0x20Lesson 3 of 6

Separate foreground from background

Use a threshold to turn grayscale intensities into a simple binary mask.

14 min 4-question quiz 1 code exercise
By the end of this lesson you can
  • Apply a pixel threshold and explain the resulting mask.

A threshold compares each pixel intensity with a chosen value. Pixels above the threshold can be marked as foreground and the rest as background, producing a binary mask. Thresholding is a simple segmentation method. It works best when foreground and background intensities are distinct; shadows and uneven lighting can make it fail.

cv_example.py
pixels = [20, 90, 140, 230]
mask = [1 if value >= 128 else 0 for value in pixels]
print(mask)
Output
[0, 0, 1, 1]

A mask does not identify objects by itself; it only marks pixels that meet a rule. Real segmentation methods use richer image features or learned models. The threshold choice affects false positives and false negatives. OpenCV’s thresholding guide shows simple and adaptive thresholding on actual images.

Key takeaways

  • Apply a pixel threshold and explain the resulting mask.

  • Small arrays make vision ideas concrete.

  • Check model performance across varied real-world examples.

Lesson quiz

4 questions · pass with 3 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.

Exercise 1

Build a binary mask

+25 XP

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
main.py
Loading editor…

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…

Gostou da aula? 😆👍
Apoie nosso trabalho com uma doação: