Opening, closing and morphology
Compose erosions and dilations to remove small structures or measure their boundaries.
Two objects and a single-pixel speck make growth, shrinkage and cleanup easy to compare.
The orange window straddles the object boundary. Its covered pixels determine the new centre value.
Opening first erodes the region, then dilates the survivors. The isolated speck disappears.
100 foreground pixels become 99. Opening first erodes the region, then dilates the survivors. The isolated speck disappears.
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
Experiment at pixel level
Apply morphology to a thresholded mask with an elliptical kernel.
The engine loads on your first run. Your images stay in this browser.
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
Select a pixel
Select a pixel
When to use it
Clean segmentation masks, fill small holes, or isolate bright and dark details at a selected scale.
How it works
- 01Choose a structuring element that represents the feature scale.
- 02Opening erodes then dilates; closing dilates then erodes.
- 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.
Related methods
Erosion
Shrink a bright mask by taking the minimum under a structuring element.
Dilation
Expand a bright mask by taking the maximum under a structuring element.
Distance transforms
For every nonzero pixel, measure the distance to the nearest zero pixel.
Skeleton thinning
Iteratively remove boundary pixels while trying to preserve the connectivity of a binary shape.