0
0
SciPydata~10 mins

Morphological operations (erosion, dilation) in SciPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to perform erosion on the binary image using scipy.

SciPy
from scipy import ndimage

binary_image = [[0, 1, 1, 0], [1, 1, 1, 0], [0, 1, 0, 0]]
eroded_image = ndimage.binary_[1](binary_image)
print(eroded_image)
Drag options to blanks, or click blank then click option'
Aerosion
Bdilation
Copening
Dclosing
Attempts:
3 left
💡 Hint
Common Mistakes
Using dilation instead of erosion
Misspelling the function name
Confusing opening and erosion
2fill in blank
medium

Complete the code to perform dilation on the binary image using scipy.

SciPy
from scipy import ndimage

binary_image = [[0, 1, 0], [1, 0, 1], [0, 1, 0]]
dilated_image = ndimage.binary_[1](binary_image)
print(dilated_image)
Drag options to blanks, or click blank then click option'
Aerosion
Bdilation
Cgradient
Dhit_or_miss
Attempts:
3 left
💡 Hint
Common Mistakes
Using erosion instead of dilation
Using unrelated morphological functions
Forgetting to import ndimage
3fill in blank
hard

Fix the error in the code to correctly perform erosion with a 3x3 structuring element.

SciPy
from scipy import ndimage
import numpy as np

binary_image = np.array([[1, 1, 0], [1, 0, 1], [0, 1, 1]])
structure = np.ones((3, 3))
eroded = ndimage.binary_[1](binary_image, structure=structure)
print(eroded)
Drag options to blanks, or click blank then click option'
Aopening
Bclosing
Cerosion
Ddilation
Attempts:
3 left
💡 Hint
Common Mistakes
Using dilation instead of erosion
Passing structuring element to wrong argument
Misspelling function name
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each pixel to its dilation and erosion results.

SciPy
from scipy import ndimage
import numpy as np

image = np.array([[0, 1], [1, 0]])
results = {pixel: (ndimage.binary_[1](image == pixel), ndimage.binary_[2](image == pixel)) for pixel in [0, 1]}
print(results)
Drag options to blanks, or click blank then click option'
Adilation
Berosion
Copening
Dclosing
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping dilation and erosion
Using opening or closing instead
Not using binary_ functions
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each pixel to its closing, erosion, and opening results.

SciPy
from scipy import ndimage
import numpy as np

image = np.array([[1, 0, 1], [0, 1, 0], [1, 0, 1]])
results = {pixel: (ndimage.binary_[1](image == pixel), ndimage.binary_[2](image == pixel), ndimage.binary_[3](image == pixel)) for pixel in [0, 1]}
print(results)
Drag options to blanks, or click blank then click option'
Adilation
Berosion
Copening
Dclosing
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up opening and closing
Using dilation instead of closing
Not using binary_ functions