Skip to content
AI360Xpert
Beta

Top Hat and Black Hat Transforms

Top-hat keeps what opening deletes, exposing small bright details. Black-hat keeps what closing fills, exposing small dark gaps.

Top-hat subtracts the opened image from the original, leaving only the bright speck that opening removed.
Top-hat subtracts the opened image from the original, leaving only the bright speck that opening removed.

Why Does This Exist?

Uneven lighting buries small details: text on a shaded page, defects on a gradient surface. Global thresholding fails because no single cutoff separates detail from background everywhere. The hat transforms instead estimate the background with morphology and subtract it, leaving details floating on a flat zero field regardless of the lighting gradient.

White top-hat builds on opening: original minus opening keeps only bright structures smaller than the probe. Black-hat builds on closing: closing minus original keeps only dark gaps smaller than the probe. Both live in morphological operations as the extraction tools.

Think of It Like This

Lifting coins off a rubbing

Lay paper over coins and rub with charcoal: the rubbing shows coins plus table texture. Now rub a second sheet with the coins removed to capture pure table texture, and subtract one rubbing from the other. Only the coins remain. Opening makes that coin-free background estimate, and the subtraction is the top-hat. It works whenever the background is broader than the probe and the detail is not.

How It Actually Works

White top-hat is Twhite=A−(A∘B)T_{white} = A - (A \circ B): the opened image estimates the background, and subtracting it from the original AA leaves bright details the probe could not contain. Black-hat is Tblack=(A∙B)−AT_{black} = (A \bullet B) - A: the closed image estimates detail-free foreground, and subtracting the original leaves dark gaps the probe bridged over. The probe size is the size selector: details smaller than the probe appear, larger structures cancel out.

Worked example

Background sits at 50 with one bright speck at 200. Opening with a probe wider than the speck returns 50 everywhere, since the speck cannot survive erosion. Top-hat gives 200−50=150200 - 50 = 150 at the speck and 50−50=050 - 50 = 0 elsewhere. The uneven background, whatever its shape, cancels to zero as long as it is broader than the probe.

Watch Out For

Probe smaller than the background texture

If the probe fits inside background bumps as well as details, opening preserves those bumps and the subtraction cancels real detail along with background. The symptom is an empty hat image despite visible features. Grow the probe past the background texture scale while keeping it near the detail scale.

The Quick Version

  • White top-hat equals original minus opening and exposes small bright details.
  • Black-hat equals closing minus original and exposes small dark gaps.
  • Both flatten uneven lighting because the background estimate subtracts out.
  • Probe size selects which feature sizes appear in the output.
  • Threshold the hat output for a lighting-proof detail mask.