Bird
Raised Fist0
Computer Visionml~20 mins

Morphological operations (erosion, dilation, opening, closing) in Computer Vision - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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
Given the following 5x5 binary image and a 3x3 square structuring element, what is the output after applying one iteration of erosion?
Computer Vision
import numpy as np
import cv2

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

kernel = np.ones((3,3), np.uint8)
eroded = cv2.erode(image, kernel, iterations=1)
print(eroded)
A
[[1 1 1 0 0]
 [1 1 1 0 0]
 [1 1 1 0 0]
 [0 0 0 1 1]
 [0 0 0 1 1]]
B
[[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]]
C
[[0 0 0 0 0]
 [0 1 1 0 0]
 [0 1 1 0 0]
 [0 0 0 0 0]
 [0 0 0 0 1]]
D
[[0 0 0 0 0]
 [0 1 1 0 0]
 [0 1 1 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]]
Attempts:
2 left
💡 Hint
Erosion shrinks white regions by removing pixels on object boundaries where the structuring element does not fit entirely.
🧠 Conceptual
intermediate
1:30remaining
Effect of dilation on image noise
Which of the following best describes the effect of applying dilation to a binary image with small isolated noise pixels?
ADilation enlarges white regions and can cause isolated noise pixels to grow.
BDilation removes isolated noise pixels by shrinking white regions.
CDilation converts white pixels to black pixels in isolated noise areas.
DDilation has no effect on isolated noise pixels.
Attempts:
2 left
💡 Hint
Think about how dilation changes the size of white areas.
Hyperparameter
advanced
2:00remaining
Choosing kernel size for opening operation
You want to remove small white noise dots from a binary image without affecting larger objects. Which kernel size for the opening operation is most appropriate?
AA kernel size slightly larger than the noise dots but smaller than the smallest object.
BA kernel size smaller than the noise dots but larger than the smallest object.
CA very large kernel that is bigger than the largest object in the image.
DA kernel size equal to the entire image size.
Attempts:
2 left
💡 Hint
Opening removes small objects smaller than the kernel size.
Metrics
advanced
1:30remaining
Evaluating closing operation effect
After applying a closing operation on a binary image, which metric would best indicate that small black holes inside white objects have been filled?
AIncrease in the number of connected white components.
BIncrease in the total white pixel area inside objects.
CDecrease in the number of white pixels.
DDecrease in the total black pixel area outside objects.
Attempts:
2 left
💡 Hint
Closing fills small black holes inside white regions.
🔧 Debug
expert
2:00remaining
Identifying error in morphological operation code
What error will this code raise when applying dilation with an invalid kernel shape? import numpy as np import cv2 image = np.array([[0,1,0],[1,1,1],[0,1,0]], dtype=np.uint8) kernel = np.array([1,1,1]) # Incorrect kernel shape result = cv2.dilate(image, kernel)
AValueError: invalid kernel shape
BTypeError: kernel must be a 2D array
Ccv2.error: (-215:Assertion failed) !ksize.empty() in function 'dilate'
DNo error, code runs successfully
Attempts:
2 left
💡 Hint
Check the expected shape of the kernel argument in OpenCV dilation.

Practice

(1/5)
1. What does the erosion operation do to the white parts of a binary image?
easy
A. It grows the white parts by adding pixels at the edges.
B. It removes noise by smoothing the edges.
C. It fills small holes inside the white parts.
D. It shrinks the white parts by removing pixels at the edges.

Solution

  1. Step 1: Understand erosion effect on white pixels

    Erosion removes pixels from the boundaries of white regions, making them smaller.
  2. Step 2: Compare with other operations

    Dilation grows white parts, opening removes noise, closing fills holes, so erosion must shrink white parts.
  3. Final Answer:

    It shrinks the white parts by removing pixels at the edges. -> Option D
  4. Quick Check:

    Erosion = Shrinks white parts [OK]
Hint: Erosion shrinks white areas by cutting edges [OK]
Common Mistakes:
  • Confusing erosion with dilation
  • Thinking erosion fills holes
  • Mixing erosion with noise removal
2. Which of the following is the correct syntax to perform dilation using OpenCV in Python on an image img with a 3x3 kernel?
easy
A. cv2.erode(img, np.ones((3,3), np.uint8))
B. cv2.dilate(img, np.ones((3,3), np.uint8))
C. cv2.dilate(img, (3,3))
D. cv2.dilate(img, kernel=3)

Solution

  1. Step 1: Recall dilation syntax in OpenCV

    Dilation requires the image and a structuring element (kernel), usually created with np.ones of desired size and type.
  2. Step 2: Check options for correct usage

    cv2.dilate(img, np.ones((3,3), np.uint8)) uses cv2.dilate with a 3x3 kernel created by np.ones and correct dtype, which is valid syntax.
  3. Final Answer:

    cv2.dilate(img, np.ones((3,3), np.uint8)) -> Option B
  4. Quick Check:

    Dilation syntax = cv2.dilate(image, kernel) [OK]
Hint: Use np.ones((3,3), np.uint8) as kernel for dilation [OK]
Common Mistakes:
  • Using erode instead of dilate
  • Passing kernel size tuple directly
  • Using wrong kernel datatype
3. Given the following Python code using OpenCV:
import cv2
import numpy as np
img = np.array([[0,0,0,0,0],
                [0,255,255,255,0],
                [0,255,0,255,0],
                [0,255,255,255,0],
                [0,0,0,0,0]], dtype=np.uint8)
kernel = np.ones((3,3), np.uint8)
eroded = cv2.erode(img, kernel)
print(eroded)

What will be the printed output?
medium
A. [[ 0 0 0 0 0] [ 0 255 0 255 0] [ 0 0 0 0 0] [ 0 255 0 255 0] [ 0 0 0 0 0]]
B. [[ 0 0 0 0 0] [ 0 255 255 255 0] [ 0 255 255 255 0] [ 0 255 255 255 0] [ 0 0 0 0 0]]
C. [[ 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]]
D. [[255 255 255 255 255] [255 255 255 255 255] [255 255 255 255 255] [255 255 255 255 255] [255 255 255 255 255]]

Solution

  1. Step 1: Understand erosion on the given image

    Erosion removes pixels at edges of white regions. The center pixel (0) surrounded by 255s will cause erosion to shrink the white area.
  2. Step 2: Apply 3x3 kernel erosion

    Since the kernel covers neighbors, any pixel with a zero neighbor becomes zero. The white cross shape will erode to a smaller cross with zeros at the center and edges.
  3. Final Answer:

    White cross with zeros at center and edges as shown in option A -> Option A
  4. Quick Check:

    Erosion shrinks white, so edge pixels vanish but some inner pixels remain [OK]
Hint: Erosion removes edge pixels, shrinking white areas [OK]
Common Mistakes:
  • Assuming erosion keeps center pixels
  • Confusing erosion with dilation output
  • Ignoring zero pixels in kernel neighborhood
4. You wrote this code to perform opening on an image img but it does not remove noise as expected:
kernel = np.ones((3,3), np.uint8)
opened = cv2.dilate(cv2.erode(img, kernel), kernel)

What is the error and how to fix it?
medium
A. Kernel size is too small; increase kernel size to remove noise.
B. The order is reversed; opening is dilation followed by erosion, so swap the calls.
C. Use cv2.morphologyEx with cv2.MORPH_OPEN instead for correct opening.
D. The order is reversed; opening is erosion followed by dilation, so code is correct.

Solution

  1. Step 1: Check the definition of opening

    Opening is erosion followed by dilation. The code applies erosion then dilation, which is correct in order.
  2. Step 2: Identify practical issue

    Manual calls may work but can be error-prone; using cv2.morphologyEx with MORPH_OPEN is recommended for correct and optimized opening.
  3. Final Answer:

    Use cv2.morphologyEx with cv2.MORPH_OPEN instead for correct opening. -> Option C
  4. Quick Check:

    Use built-in morphologyEx for opening [OK]
Hint: Use cv2.morphologyEx with MORPH_OPEN for opening [OK]
Common Mistakes:
  • Swapping erosion and dilation order
  • Not using built-in morphology functions
  • Assuming kernel size fixes logic errors
5. You have a noisy binary image with small black holes inside white objects. Which morphological operation should you apply to fill these holes without changing the object shapes much?
hard
A. Closing
B. Dilation
C. Opening
D. Erosion

Solution

  1. Step 1: Understand the problem of black holes inside white objects

    Black holes are small dark spots inside white regions that need to be filled.
  2. Step 2: Choose operation that fills holes without shrinking objects

    Closing is dilation followed by erosion; it fills small holes and gaps while preserving object shape.
  3. Final Answer:

    Closing -> Option A
  4. Quick Check:

    Closing fills holes inside white objects [OK]
Hint: Closing fills holes inside white objects [OK]
Common Mistakes:
  • Using erosion which shrinks objects
  • Using opening which removes noise but not holes
  • Confusing dilation alone with closing