0
0
SciPydata~10 mins

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

Choose your learning style9 modes available
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.