0
0
SciPydata~20 mins

Morphological operations (erosion, dilation) in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Morphological Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of erosion on a binary image
What is the output array after applying erosion with a 3x3 square structuring element on the given binary image?
SciPy
import numpy as np
from scipy.ndimage import binary_erosion

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)

structure = np.ones((3,3), dtype=bool)
eroded = binary_erosion(image, structure=structure)
print(eroded.astype(int))
A
[[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]]
B
[[0 0 0 0 0]
 [0 0 1 0 0]
 [0 1 1 1 0]
 [0 0 1 0 0]
 [0 0 0 0 0]]
C
[[0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 1 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]]
D
[[0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]]
Attempts:
2 left
💡 Hint
Erosion shrinks the white regions by removing pixels on the edges.
Predict Output
intermediate
2:00remaining
Result of dilation on a sparse binary image
What is the output after applying dilation with a 3x3 cross-shaped structuring element on the binary image below?
SciPy
import numpy as np
from scipy.ndimage import binary_dilation

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

structure = np.array([
  [0, 1, 0],
  [1, 1, 1],
  [0, 1, 0]
], dtype=bool)
dilated = binary_dilation(image, structure=structure)
print(dilated.astype(int))
A
[[0 0 1 0 0]
 [0 1 1 1 0]
 [0 0 1 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]]
B
[[0 0 0 0 0]
 [0 1 1 1 0]
 [0 1 1 1 0]
 [0 0 1 0 0]
 [0 0 0 0 0]]
C
[[0 0 0 0 0]
 [0 1 1 1 0]
 [0 1 1 1 0]
 [0 1 0 1 0]
 [0 0 0 0 0]]
D
[[0 0 0 0 0]
 [0 0 1 0 0]
 [0 1 1 1 0]
 [0 0 1 0 0]
 [0 0 0 0 0]]
Attempts:
2 left
💡 Hint
Dilation adds pixels around existing white pixels according to the structuring element shape.
visualization
advanced
3:00remaining
Visualize erosion and dilation effects
Which option shows the correct pair of images after applying erosion and dilation respectively on the given binary image with a 3x3 square structuring element?
SciPy
import numpy as np
from scipy.ndimage import binary_erosion, binary_dilation
import matplotlib.pyplot as plt

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)

structure = np.ones((3,3), dtype=bool)
eroded = binary_erosion(image, structure=structure)
dilated = binary_dilation(image, structure=structure)

fig, axs = plt.subplots(1, 2)
axs[0].imshow(eroded, cmap='gray')
axs[0].set_title('Erosion')
axs[1].imshow(dilated, cmap='gray')
axs[1].set_title('Dilation')
plt.show()
ALeft image: 3x3 white square with holes; Right image: 5x5 white square with holes
BLeft image: 3x3 white square; Right image: 3x3 white square
CLeft image: 5x5 white square; Right image: single pixel in center
DLeft image: single pixel in center; Right image: 5x5 white square
Attempts:
2 left
💡 Hint
Erosion shrinks white areas, dilation expands them.
🧠 Conceptual
advanced
1:30remaining
Effect of structuring element shape on dilation
Which statement correctly describes how the shape of the structuring element affects the dilation result on a binary image?
AThe dilation result shape matches the structuring element shape centered on each white pixel.
BA larger structuring element always produces a smaller dilated area.
CThe structuring element shape does not affect dilation, only erosion.
DDilation removes pixels from the edges of white regions based on the structuring element.
Attempts:
2 left
💡 Hint
Think about how dilation adds pixels around existing ones.
🔧 Debug
expert
2:00remaining
Identify the error in morphological operation code
What error will occur when running the following code snippet for erosion, and why?
SciPy
import numpy as np
from scipy.ndimage import binary_erosion

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

structure = np.array([
  [1, 1],
  [1, 1]
])
eroded = binary_erosion(image, structure=structure)
print(eroded)
AValueError because structuring element is not square
BTypeError because image array is not boolean
CNo error, code runs and outputs eroded image
DIndexError due to structuring element size larger than image
Attempts:
2 left
💡 Hint
Check the data type required by binary_erosion for the input image.