Skip to content

Opening, closing and morphology

Algorithm atlasMorphology

Compose erosions and dilations to remove small structures or measure their boundaries.

Try it on your image ↓

VISUAL WALKTHROUGHMorphology
REFERENCE INPUTBinary mask

Two objects and a single-pixel speck make growth, shrinkage and cleanup easy to compare.

RESULTChanged foreground

100 foreground pixels become 99. Opening first erodes the region, then dilates the survivors. The isolated speck disappears.

STEP 03 / 03

Gradient, top-hat and black-hat subtract the appropriate filtered images.

Computed teaching example on a 24 × 16 image. Small kernels and simplified settings keep each change visible; use the image laboratory for native OpenCV.

Try it on an image

YOUR IMAGE · REAL OPENCV

Experiment at pixel level

Open full lab ↗

Apply morphology to a thresholded mask with an elliptical kernel.

The engine loads on your first run. Your images stay in this browser.

Input448 × 320
OutputWaiting for a result

Scroll over either image to zoom at the pointer. Use the scrollbars to pan both views over the same relative area. Zoom is relative to the input; pixel coordinates belong to each image. Warps can change scene correspondence.

Pixel inspector RGBA · native values · matched scale · 9 × 9 output pixels
Hover to inspect. Click to pin a pixel.
Input
Select a pixel

Output
Select a pixel

Sample models and licenses

When to use it

Clean segmentation masks, fill small holes, or isolate bright and dark details at a selected scale.

How it works

  1. 01Choose a structuring element that represents the feature scale.
  2. 02Opening erodes then dilates; closing dilates then erodes.
  3. 03Gradient, top-hat and black-hat subtract the appropriate filtered images.

open = dilate(erode(A)); close = erode(dilate(A))

What to tune

op selects opening, closing, gradient, top-hat or black-hat. Kernel shape matters as much as its size.

Where it breaks down

Opening removes protrusions; closing fills gaps. Neither operation reconstructs the true hidden boundary.

TypeScript API

Open an entry for its exact overloads, parameter descriptions, result ownership and pinned upstream source.

All of these calls execute on the CPU. Native objects need explicit disposal. See matrices and ownership and build compatibility.