Morphological operations help us change shapes in images or data. They make objects smaller or bigger to find patterns or clean noise.
Morphological operations (erosion, dilation) in SciPy
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
SciPy
from scipy.ndimage import binary_erosion, binary_dilation # Erosion result = binary_erosion(input_array, structure=structuring_element) # Dilation result = binary_dilation(input_array, structure=structuring_element)
input_array is the image or data as a 2D array of True/False or 1/0.
structuring_element defines the shape used to erode or dilate, like a small square or circle.
Examples
SciPy
from scipy.ndimage import binary_erosion import numpy as np image = np.array([[1,1,1],[1,1,1],[1,1,1]], dtype=bool) eroded = binary_erosion(image)
SciPy
from scipy.ndimage import binary_dilation import numpy as np image = np.array([[0,0,0],[0,1,0],[0,0,0]], dtype=bool) dilated = binary_dilation(image)
Sample Program
This code shows how erosion shrinks the square by removing edge pixels, and dilation grows it by adding pixels around the edges.
SciPy
from scipy.ndimage import binary_erosion, binary_dilation import numpy as np # Create a simple 5x5 image with a square in the center image = np.array([ [0,0,0,0,0], [0,1,1,1,0], [0,1,1,1,0], [0,1,1,1,0], [0,0,0,0,0] ], dtype=bool) # Define a 3x3 square structuring element structure = np.ones((3,3), dtype=bool) # Apply erosion eroded_image = binary_erosion(image, structure=structure) # Apply dilation dilated_image = binary_dilation(image, structure=structure) print("Original image:\n", image.astype(int)) print("\nEroded image:\n", eroded_image.astype(int)) print("\nDilated image:\n", dilated_image.astype(int))
Important Notes
Erosion removes pixels on object edges, making shapes smaller.
Dilation adds pixels to object edges, making shapes bigger.
Choosing the right structuring element shape and size affects results a lot.
Summary
Morphological operations change shapes in images by shrinking or growing them.
Erosion removes edge pixels; dilation adds edge pixels.
These operations help clean images and find patterns.
Practice
1. What does the
erosion operation do to a binary image in morphological processing?easy
Solution
Step 1: Understand erosion effect
Erosion removes pixels on object boundaries, making white regions smaller.Step 2: Compare with other operations
Dilation adds pixels, inversion changes colors, blurring smooths image; none match erosion.Final Answer:
It shrinks the white regions by removing edge pixels. -> Option CQuick 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
Solution
Step 1: Identify correct function name
The function for erosion on binary images is namedbinary_erosionin scipy.ndimage.Step 2: Check import syntax
Correct import isfrom scipy.ndimage import binary_erosion. Other options are invalid or incorrect names.Final Answer:
from scipy.ndimage import binary_erosion -> Option BQuick 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
Solution
Step 1: Understand binary_dilation effect
Dilation adds pixels to the edges of white regions (1s), expanding them by one pixel in all directions.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]].Final Answer:
[[0 1 1 0 0] [1 1 1 1 0] [1 1 1 0 0] [0 1 0 0 0]] -> Option DQuick 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
Solution
Step 1: Check function availability
Scipy.ndimage does not have a function named 'erosion'; the correct function is 'binary_erosion'.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)'.Final Answer:
The function 'erosion' does not exist in scipy.ndimage; use 'binary_erosion' instead. -> Option AQuick 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
Solution
Step 1: Understand noise removal goal
Small white dots are noise; we want to remove them without changing main shape much.Step 2: Choose correct morphological sequence
Opening (erosion then dilation) removes small objects but keeps main shape. Closing fills holes, not remove dots.Final Answer:
Apply erosion followed by dilation (opening) to remove small objects. -> Option AQuick 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
