Skip to content

Lesson 3 of 9 · 25m

Filtering, Thresholds, and Image Quality Control

Use blur, morphology, thresholding, illumination normalization, and quality gates to prepare images without destroying evidence.

A junior engineer sees preprocessing as a sequence of clever filters. A senior engineer sees it as controlled destruction. Every blur removes detail, every threshold makes a binary decision under uncertainty, and every normalization step can erase the exact signal the downstream algorithm needed. Preprocess only when you know what noise you are removing and what information you can afford to lose.

Blur is noise modeling, not magic

Different blur operations encode different assumptions:

blur = cv2.GaussianBlur(img, (5, 5), 0)
median = cv2.medianBlur(img, 5)
bilateral = cv2.bilateralFilter(img, 9, 75, 75)

Gaussian blur suppresses high-frequency noise while spreading edges. Median filtering is strong against salt-and-pepper noise. Bilateral filtering preserves edges better but costs more and can create strange texture flattening. If a microscope image, document scan, or license plate has fine detail, aggressive smoothing may remove the signal entirely.

Thresholding is a decision under uneven illumination

A global threshold assumes one illumination distribution:

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, bw = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

Real scenes violate that assumption constantly. Adaptive thresholding estimates a local neighborhood:

bw = cv2.adaptiveThreshold(
    gray, 255,
    cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
    cv2.THRESH_BINARY,
    35, 11,
)

Block size and the subtracted constant are scene parameters, not universal constants. Tune them on representative data, including glare, shadows, blur, compression artifacts, and nighttime input.

Morphology cleans shape, not meaning

Morphological operations are powerful for binary masks:

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
opened = cv2.morphologyEx(bw, cv2.MORPH_OPEN, kernel)
closed = cv2.morphologyEx(bw, cv2.MORPH_CLOSE, kernel)

Opening removes small bright specks; closing fills small dark gaps. Kernel size is measured in pixels, which means it changes meaning when image resolution changes. A 5-pixel kernel on a 640-pixel-wide image is not the same intervention on a 4K image.

Measure quality before fixing quality

Do not blindly enhance input that should be rejected. Compute cheap quality signals:

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
lap_var = cv2.Laplacian(gray, cv2.CV_64F).var()
mean_brightness = float(gray.mean())
contrast = float(gray.std())

Laplacian variance is a common blur proxy. It is not universal truth: a featureless wall is sharp but still has low texture, and compression can inflate edge energy. Use quality gates as routing signals — accept, enhance, retry capture, or send to a slower model.

Normalize illumination carefully

CLAHE can rescue low-contrast imagery:

clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
norm = clahe.apply(gray)

It can also amplify sensor noise and compression artifacts until edges become false positives. When illumination is the problem, fixing lighting or exposure is often better than heroic digital correction.

Field notes

  • Keep the original image. Preprocessing is an interpretation, and investigations need the source evidence.
  • Tune morphology in physical units when possible: object size times pixels-per-unit, not a random kernel.
  • Threshold parameters are resolution- and illumination-dependent; never copy them blindly across cameras.
  • Log quality metrics even when accepted. Drift in blur or brightness is often the first sign of a dirty lens or broken camera.
  • Visualize intermediate masks during development. A pipeline that looks correct numerically can be obviously wrong in one displayed frame.

Was this lesson helpful?

Sign in to rate