Bird
0
0

What will be the output of the following code snippet?

medium📝 Predict Output Q4 of 15
SciPy - Image Processing (scipy.ndimage)
What will be the output of the following code snippet?
import numpy as np
from scipy.ndimage import binary_dilation
img = np.array([[0,0,0],[0,1,0],[0,0,0]])
dilated = binary_dilation(img, structure=np.ones((3,3)))
print(dilated.astype(int))
A[[1 1 1] [1 1 1] [1 1 1]]
B[[0 0 0] [0 1 0] [0 0 0]]
C[[0 1 0] [1 1 1] [0 1 0]]
D[[1 0 1] [0 1 0] [1 0 1]]
Step-by-Step Solution
Solution:
  1. Step 1: Understand binary_dilation with 3x3 structuring element

    Dilation adds pixels to neighbors where the structuring element overlaps with any 1 pixel.
  2. Step 2: Apply dilation to the input image

    The single center 1 pixel causes all its 8 neighbors to become 1, but since the image border is zero, dilation results in a cross shape with ones at center and its 4 neighbors.
  3. Final Answer:

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

    Dilation expands pixels to neighbors [OK]
Quick Trick: Dilation with full 3x3 structure fills neighbors [OK]
Common Mistakes:
  • Expecting full 3x3 block of ones
  • Confusing dilation with erosion
  • Using wrong structuring element size

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes