Opening and Closing
Compose erosion and dilation to remove bright specks or fill small holes without manually editing the mask.
Morphology is nested under Segmentation & edges , so the broader pipeline usually still applies here.
Interactive playground
Tweak the operator or scene live so the article connects to an immediate visual result.
Opening / closing
Chain morphology passes to remove specks or fill holes without manually touching every pixel.
Source image
Procedural inputWebGPU result
Composite outputControls
Adjust the neighborhood size, repeat count, or decision threshold to see how the shader compounds the result.
Checking WebGPU support...
Family
Foundations -> Segmentation & edges -> Morphology
Binary-mask operators such as dilation, erosion, opening, and closing.
Builds on
4 topics
Read these first if you want the surrounding pipeline context.
Unlocks
0 next topics
Use these follow-ups when you want to keep turning the image-processing pipeline forward.
Learning paths
1
This topic appears in curated graphics progressions so the next step is obvious.
Choose this over that
Opening and closing are deliberate two-step repairs
Both operators combine erosion and dilation, but the order decides whether you remove bright specks or fill small dark holes.
Opening = erosion then dilation
Choose this when: Small bright specks should disappear while larger shapes stay mostly intact.
Choose something else when: The bigger problem is small dark gaps or holes inside the foreground.
Closing = dilation then erosion
Choose this when: Small holes and gaps should be sealed without permanently thickening the whole shape.
Choose something else when: You are trying to remove isolated bright noise instead of fill gaps.
Gaussian Blur
Choose this when: The source is still a continuous image and should be smoothed, not treated as a binary mask.
Choose something else when: The mask already exists and needs structural cleanup.
Problem
Single-step dilation and erosion are useful, but often too blunt.
Typical mask defects come in two common forms:
- isolated bright specks that should disappear
- small dark gaps or holes that should be filled
You need a controlled two-step repair, not just unconditional growth or shrinkage.
Intuition
Opening and closing use the same two primitive operators:
- erosion
- dilation
The entire behavior is decided by the order.
Opening
opening = erosion -> dilation
This removes small bright foreground noise first, then regrows the surviving larger structures.
Closing
closing = dilation -> erosion
This fills small dark holes or narrow breaks first, then shrinks the result back toward its original boundary.
That order difference is the whole story.
Worked example
Opening
Suppose a mask contains one large object plus tiny bright specks.
- Erosion removes the specks entirely and shrinks the large object.
- Dilation regrows the large object.
- The specks do not come back because they were erased completely in the first step.
Closing
Suppose a mask contains a mostly solid region with tiny holes.
- Dilation grows the foreground so holes and narrow gaps seal.
- Erosion trims the outside boundary back down.
- The previously sealed holes stay filled if they were smaller than the structuring element.
Why opening and closing are not opposites
They use the same two primitives, but they are not interchangeable.
- Opening is biased toward removing small bright features
- Closing is biased toward filling small dark features
That makes them perfect “choose this over that” topics.
Complexity
If one morphology pass costs , then opening or closing costs:
O(2WHk^2)
which is still just a constant-factor multiple of the primitive operation.
GPU implementation note
On WebGPU, opening and closing are naturally expressed as two passes:
- render the first morphology result into an intermediate texture
- run the second morphology pass from that intermediate texture into the final target
That makes them a good example of a small multi-pass graphics pipeline rather than a single operator.
When to choose each
Choose opening when
- the mask contains isolated bright specks
- thin false positives should disappear
- the foreground should become cleaner, not more connected
Choose closing when
- the mask contains tiny holes
- boundaries have narrow cracks
- nearly connected regions should fuse
Key takeaways
- Opening and closing are two-pass morphology techniques
- The order determines the effect
- Opening removes small bright noise
- Closing fills small dark gaps and holes
- They are more selective than applying dilation or erosion alone
Practice ideas
- Build one noisy mask with bright specks and another with small holes, then compare opening and closing on both
- Vary the morphology radius and note the largest defects each operator can still fix
- Implement opening and closing as two WebGPU passes with an intermediate texture
Relation to other topics
- Dilation and erosion are the primitive operators opening and closing are built from
- Thresholding often produces the mask that later needs opening or closing
- Gaussian blur is a continuous-image cleanup step from earlier in the pipeline, not a binary-mask repair tool
Build on these first
These topics supply the mental model or preceding stage that this page assumes.
Image Processing Fundamentals
Build the mental model behind blur, thresholding, edges, and morphology: pixels, neighborhoods, kernels, masks, and why local operators compose into pipelines.
Thresholding
Convert a grayscale image into a binary mask by splitting values into foreground and background.
Dilation
Grow foreground regions in a binary mask so gaps close and thin structures get thicker.
Erosion
Shrink foreground regions in a binary mask so thin noise and small protrusions disappear.
Related directions
These topics live nearby conceptually, even if they are not strict prerequisites.
Gaussian Blur
Smooth an image with a weighted local average so noise shrinks before thresholding, edge detection, or later analysis.
Connected-Components Labeling
Turn a binary mask into object IDs so you can count, measure, filter, and track separate regions instead of raw foreground pixels.
Skeletonization
Reduce a thick binary shape to a thin centerline while trying to preserve its topology.
More from Morphology
Stay in the same family when you want parallel operators built from the same mental model.
Dilation
Grow foreground regions in a binary mask so gaps close and thin structures get thicker.
Erosion
Shrink foreground regions in a binary mask so thin noise and small protrusions disappear.
Skeletonization
Reduce a thick binary shape to a thin centerline while trying to preserve its topology.
Paths that include this topic
Follow one of these sequences if you want a guided next step instead of open-ended browsing.
Mask analysis and cleanup
Build a binary mask, explore connectivity, then repair or simplify it with morphology-aware tools.
From the blog
Pair the graphics atlas with recent writing from the broader site whenever you want a wider engineering lens.