AI Hints
Home›Computer Vision Tutorial›Classic Image Processing
Computer Vision · Lesson 6 of 111

Thresholding

Separating objects from background.

⏱ 2 min read▶ Video at 10:40🎓 Free
Watch this lesson in the Computer Vision courseWatch on YouTube ↗Full Computer Vision course

The simplest way to find objects is to pick a brightness. Anything above it is an object, and anything below is background. It's so simple that it sounds like a toy, and yet it's used in factories all over the world, because with good lighting, it just works.

Thresholding
From the video: Thresholding
  • Pick one brightness
  • Above is object, below is background
  • Used in factories everywhere

Reading the histogram

A histogram counts how many pixels have each brightness. If your objects are bright and the background's dark, you'll see two hills. The best threshold is the valley between them. In nineteen seventy nine, Nobuyuki Otsu found a way for a computer to pick that valley automatically.

Automatic thresholds

Here we have a photo of coins. Otsu's method finds the threshold for us. But look at the lighting in this photo. It's uneven, so a single threshold fails on one side. Adaptive thresholding picks a different value for each small region. Run it. Compare the two. Otsu loses part of the image, because of the shadow. Adaptive handles the changing light, though it does add some noise. There's always a trade-off.

threshold.pypython
import cv2
import matplotlib.pyplot as plt
from skimage import data

gray = data.coins()

value, otsu = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
adaptive = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                 cv2.THRESH_BINARY, 51, 2)

fig, ax = plt.subplots(1, 3, figsize=(10, 3))
for a, im, n in zip(ax, [gray, otsu, adaptive], ["Original", "Otsu", "Adaptive"]):
    a.imshow(im, cmap="gray"); a.set_title(n); a.axis("off")
plt.tight_layout(); plt.savefig("threshold.png", dpi=110)
print("Otsu chose the value:", value)
Output
Otsu chose the value: 107.0

Summary

Otsu finds the valley, and adaptive thresholds handle uneven light. But a threshold leaves specks and holes. Next, a simple set of tools for cleaning up shapes.

Key takeaways
  • Histograms show the brightness
  • Otsu picks the valley
  • Adaptive handles uneven light
Next up: Morphology — cleaning up shapes