← Image Processing for EveryoneToolsInVisionLearning Center
Structure and Segmentation · Lesson 11

Morphology: Clean a Binary Mask Carefully

Your thresholded mask contains tiny isolated dots and small gaps inside a larger region.

Watch the lesson

Morphology: Clean a Binary Mask Carefully explained visually

1:15

Your thresholded mask contains tiny isolated dots and small gaps inside a larger region. English captions are optional and start off. The complete transcript follows the written lesson.

Lesson overview

What you will learn

Your thresholded mask contains tiny isolated dots and small gaps inside a larger region.

  1. Work with shape
  2. Understand the basic operations
  3. Choose the neighborhood
  4. A worked example
01

Work with shape

Our screw mask has tiny dots outside the screw and little holes inside it. Morphology can change these shapes using a small neighborhood called a structuring element. Its size and shape are part of the decision: a large element may remove a real thin feature as readily as a false speck.

02

Understand the basic operations

Erosion tends to shrink foreground. Dilation tends to grow it. Opening applies erosion and then dilation, often removing small foreground dots. Closing applies dilation and then erosion, often filling small holes or narrow gaps. Reversing the order changes the result.

03

Choose the neighborhood

Put the original mask and both processed versions side by side. Did opening remove a tiny screw head or did closing join two separate screws? A cleaner-looking mask may produce a worse object count or biased area measurement. On a real part, check the photograph as well as the mask.

04

A worked example

Record the structuring element and order. Next, we'll label each connected region so we can count objects and measure the mask they actually occupy.

Worked example

Consider two foreground blobs separated by a one-pixel gap. A closing operation with a suitable neighborhood may connect them. That is helpful if the gap came from a segmentation error, but harmful if the blobs represent two separate objects that must be counted independently.

Knowledge check

Quick check: test your understanding

Try it yourself: Choose an operation for isolated white specks, then name a real object that the same operation might accidentally remove.

Check the reasoning

Opening may remove small specks. It could also remove a thin cable, a small grain, or another narrow foreground structure.

Why must the structuring element be recorded in a reproducible workflow?

Its shape and size affect which regions are removed, expanded, or connected.

How would you solve the practice case?

Opening may remove small specks. It could also remove a thin cable, a small grain, or another narrow foreground structure.

What does the worked example show?

Consider two foreground blobs separated by a one-pixel gap. A closing operation with a suitable neighborhood may connect them. That is helpful if the gap came from a segmentation error, but harmful if the blobs represent two separate objects that must be counted independently.

Further reading

Digital Image Processing, 4th edition — Rafael C. Gonzalez and Richard E. Woods.

Original ToolsInVision teaching examples. Editorial approach · Suggest a correction

Accessible referenceLesson 11 transcript165 words · Video transcript · Matches the published narration and captionsOpen transcript

Our screw mask has tiny dots outside the screw and little holes inside it. Morphology can change these shapes using a small neighborhood called a structuring element. Its size and shape are part of the decision: a large element may remove a real thin feature as readily as a false speck.

Erosion tends to shrink foreground. Dilation tends to grow it. Opening applies erosion and then dilation, often removing small foreground dots. Closing applies dilation and then erosion, often filling small holes or narrow gaps. Reversing the order changes the result.

Put the original mask and both processed versions side by side. Did opening remove a tiny screw head or did closing join two separate screws? A cleaner-looking mask may produce a worse object count or biased area measurement. On a real part, check the photograph as well as the mask.

Record the structuring element and order. Next, we'll label each connected region so we can count objects and measure the mask they actually occupy.