Bird
Raised Fist0
SciPydata~10 mins

Morphological operations (erosion, dilation) in SciPy - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Morphological operations (erosion, dilation)
Start with binary image
Choose structuring element
Apply Erosion
Pixels shrink
Apply Dilation
Pixels grow
Result: Modified image
Start with a binary image, pick a shape to probe it, then shrink or grow pixels by erosion or dilation.
Execution Sample
SciPy
import numpy as np
from scipy.ndimage import binary_erosion, binary_dilation

image = np.array([[0,1,1,0],[1,1,1,1],[0,1,1,0]])
eroded = binary_erosion(image)
dilated = binary_dilation(image)
This code shrinks and grows the white pixels in a small binary image using erosion and dilation.
Execution Table
StepOperationInput ImageStructuring ElementOutput Image
1Input Image[[0 1 1 0] [1 1 1 1] [0 1 1 0]]N/A[[0 1 1 0] [1 1 1 1] [0 1 1 0]]
2Erosion[[0 1 1 0] [1 1 1 1] [0 1 1 0]]3x3 cross (default)[[0 0 0 0] [0 1 1 0] [0 0 0 0]]
3Dilation[[0 1 1 0] [1 1 1 1] [0 1 1 0]]3x3 cross (default)[[1 1 1 1] [1 1 1 1] [1 1 1 1]]
💡 All operations applied; output images show pixel shrinkage and growth.
Variable Tracker
VariableStartAfter ErosionAfter Dilation
image[[0 1 1 0] [1 1 1 1] [0 1 1 0]][[0 0 0 0] [0 1 1 0] [0 0 0 0]][[0 1 1 0] [1 1 1 1] [0 1 1 0]]
erodedN/A[[0 0 0 0] [0 1 1 0] [0 0 0 0]][[0 0 0 0] [0 1 1 0] [0 0 0 0]]
dilatedN/AN/A[[1 1 1 1] [1 1 1 1] [1 1 1 1]]
Key Moments - 3 Insights
Why does erosion make the white areas smaller?
Erosion removes pixels where the structuring element doesn't fit fully inside white pixels, as shown in step 2 of the execution_table where some 1s become 0s.
Why does dilation make the white areas bigger?
Dilation adds pixels around white areas where the structuring element touches, shown in step 3 where zeros become ones around the original white pixels.
What is the role of the structuring element?
It acts like a small shape that probes the image; erosion shrinks pixels where it doesn't fit, dilation grows pixels where it touches, as noted in the 'Structuring Element' column.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table step 2. What is the value of the pixel at position (1,1) after erosion?
AUndefined
B0
C1
D2
💡 Hint
Check the 'Output Image' in step 2 of execution_table at row 1, column 1 (0-based indexing).
At which step does the white area grow larger than the original image?
AStep 1
BStep 3
CStep 2
DNo growth occurs
💡 Hint
Look at the 'Output Image' column in step 3 of execution_table.
If the structuring element was a smaller shape, how would erosion output change?
AFewer pixels would be eroded
BMore pixels would be eroded
CNo change in erosion
DImage would become all zeros
💡 Hint
Smaller structuring elements fit more easily, so erosion removes fewer pixels (see structuring element role in key_moments).
Concept Snapshot
Morphological operations modify binary images.
Erosion shrinks white areas by removing edge pixels.
Dilation grows white areas by adding pixels around edges.
Use a structuring element to probe the image.
Commonly used for noise removal and shape manipulation.
Full Transcript
Morphological operations change shapes in binary images. We start with a binary image and pick a small shape called a structuring element. Erosion shrinks white pixels by removing those that don't fully fit the structuring element. Dilation grows white pixels by adding pixels where the structuring element touches. In the example, erosion made the white area smaller, and dilation made it bigger. The structuring element controls how much shrinking or growing happens. These operations help clean images or highlight shapes.

Practice

(1/5)
1. What does the erosion operation do to a binary image in morphological processing?
easy
A. It inverts the colors of the image.
B. It enlarges the white regions by adding pixels to edges.
C. It shrinks the white regions by removing edge pixels.
D. It blurs the image to reduce noise.

Solution

  1. Step 1: Understand erosion effect

    Erosion removes pixels on object boundaries, making white regions smaller.
  2. Step 2: Compare with other operations

    Dilation adds pixels, inversion changes colors, blurring smooths image; none match erosion.
  3. Final Answer:

    It shrinks the white regions by removing edge pixels. -> Option C
  4. Quick Check:

    Erosion = Shrink white regions [OK]
Hint: Erosion shrinks shapes by cutting edges [OK]
Common Mistakes:
  • Confusing erosion with dilation
  • Thinking erosion adds pixels
  • Mixing erosion with color inversion
2. Which of the following is the correct way to import the erosion function from scipy's ndimage module?
easy
A. from scipy.ndimage import erosion
B. from scipy.ndimage import binary_erosion
C. import scipy.ndimage.erosion
D. from scipy import erosion

Solution

  1. Step 1: Identify correct function name

    The function for erosion on binary images is named binary_erosion in scipy.ndimage.
  2. Step 2: Check import syntax

    Correct import is from scipy.ndimage import binary_erosion. Other options are invalid or incorrect names.
  3. Final Answer:

    from scipy.ndimage import binary_erosion -> Option B
  4. Quick Check:

    Correct import = binary_erosion [OK]
Hint: Use binary_erosion for erosion in scipy.ndimage [OK]
Common Mistakes:
  • Using wrong function name 'erosion'
  • Incorrect import syntax
  • Trying to import from scipy root
3. Given the following code, what will be the output array after applying dilation?
import numpy as np
from scipy.ndimage import binary_dilation

image = np.array([[0, 0, 0, 0, 0],
                  [0, 1, 1, 0, 0],
                  [0, 1, 0, 0, 0],
                  [0, 0, 0, 0, 0]])

result = binary_dilation(image).astype(int)
print(result)
medium
A. [[0 0 0 0 0] [0 0 1 0 0] [0 1 0 0 0] [0 0 0 0 0]]
B. [[0 0 0 0 0] [0 1 1 0 0] [0 1 0 0 0] [0 0 0 0 0]]
C. [[1 1 1 0 0] [1 1 1 1 0] [1 1 1 1 0] [0 1 1 0 0]]
D. [[0 1 1 0 0] [1 1 1 1 0] [1 1 1 0 0] [0 1 0 0 0]]

Solution

  1. Step 1: Understand binary_dilation effect

    Dilation adds pixels to the edges of white regions (1s), expanding them by one pixel in all directions.
  2. Step 2: Apply dilation to given image

    Original white pixels at (1,1),(1,2),(2,1). After dilation, neighbors become 1, resulting in the array in [[0 1 1 0 0] [1 1 1 1 0] [1 1 1 0 0] [0 1 0 0 0]].
  3. Final Answer:

    [[0 1 1 0 0] [1 1 1 1 0] [1 1 1 0 0] [0 1 0 0 0]] -> Option D
  4. Quick Check:

    Dilation = expand edges [OK]
Hint: Dilation grows white pixels by one layer [OK]
Common Mistakes:
  • Confusing dilation with erosion
  • Not converting boolean to int for print
  • Misreading array indices
4. The following code is intended to perform erosion on a binary image, but it raises an error. What is the problem?
import numpy as np
from scipy.ndimage import erosion

image = np.array([[1, 1, 0],
                  [1, 0, 0],
                  [0, 0, 1]])

result = erosion(image)
print(result)
medium
A. The function 'erosion' does not exist in scipy.ndimage; use 'binary_erosion' instead.
B. The input image must be float type, not int.
C. The image array shape is invalid for erosion.
D. The print statement syntax is incorrect.

Solution

  1. Step 1: Check function availability

    Scipy.ndimage does not have a function named 'erosion'; the correct function is 'binary_erosion'.
  2. Step 2: Correct the import and usage

    Replace 'from scipy.ndimage import erosion' with 'from scipy.ndimage import binary_erosion' and call 'binary_erosion(image)'.
  3. Final Answer:

    The function 'erosion' does not exist in scipy.ndimage; use 'binary_erosion' instead. -> Option A
  4. Quick Check:

    Use binary_erosion, not erosion [OK]
Hint: Use binary_erosion, not erosion function [OK]
Common Mistakes:
  • Trying to import non-existent 'erosion'
  • Ignoring error messages
  • Assuming all morphological functions have simple names
5. You have a noisy binary image with small white dots scattered outside the main object. Which sequence of morphological operations using scipy.ndimage would best remove these small dots but keep the main shape mostly intact?
hard
A. Apply erosion followed by dilation (opening) to remove small objects.
B. Apply dilation followed by erosion (closing) to fill small holes.
C. Apply only dilation to enlarge all white areas.
D. Apply only erosion to shrink all white areas drastically.

Solution

  1. Step 1: Understand noise removal goal

    Small white dots are noise; we want to remove them without changing main shape much.
  2. Step 2: Choose correct morphological sequence

    Opening (erosion then dilation) removes small objects but keeps main shape. Closing fills holes, not remove dots.
  3. Final Answer:

    Apply erosion followed by dilation (opening) to remove small objects. -> Option A
  4. Quick Check:

    Opening = erosion + dilation removes noise [OK]
Hint: Use opening (erosion then dilation) to remove small noise [OK]
Common Mistakes:
  • Using closing instead of opening for noise removal
  • Applying only dilation or erosion alone
  • Confusing noise removal with hole filling