Bird
Raised Fist0
MATLABdata~20 mins

Morphological operations in MATLAB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Morphological Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of dilation on a binary image
What is the output of the following MATLAB code snippet that performs dilation on a binary image?
BW = [0 0 0 0 0; 0 1 1 0 0; 0 1 0 0 0; 0 0 0 0 0];
se = strel('square', 3);
BW2 = imdilate(BW, se);
disp(BW2)
MATLAB
BW = [0 0 0 0 0; 0 1 1 0 0; 0 1 0 0 0; 0 0 0 0 0];
se = strel('square', 3);
BW2 = imdilate(BW, se);
disp(BW2)
A[0 0 0 0 0; 0 1 1 0 0; 0 1 0 0 0; 0 0 0 0 0]
B[0 1 1 0 0; 1 1 1 0 0; 1 1 1 0 0; 0 1 1 0 0]
C[0 1 1 1 0; 1 1 1 1 0; 1 1 1 1 0; 0 1 1 1 0]
D[1 1 1 0 0; 1 1 1 0 0; 1 1 1 0 0; 0 0 0 0 0]
Attempts:
2 left
💡 Hint
Dilation expands the white regions by the shape of the structuring element.
❓ Predict Output
intermediate
2:00remaining
Result of erosion on a binary image
What is the output of this MATLAB code that performs erosion on a binary image?
BW = [1 1 1 0 0; 1 1 1 0 0; 1 1 1 0 0; 0 0 0 0 0];
se = strel('disk', 1, 0);
BW2 = imerode(BW, se);
disp(BW2)
MATLAB
BW = [1 1 1 0 0; 1 1 1 0 0; 1 1 1 0 0; 0 0 0 0 0];
se = strel('disk', 1, 0);
BW2 = imerode(BW, se);
disp(BW2)
A[1 1 0 0 0; 1 1 0 0 0; 1 1 0 0 0; 0 0 0 0 0]
B[1 0 0 0 0; 1 0 0 0 0; 1 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]
D[1 1 1 0 0; 1 1 1 0 0; 1 1 1 0 0; 0 0 0 0 0]
Attempts:
2 left
💡 Hint
Erosion shrinks white regions by removing pixels near edges based on the structuring element.
🔧 Debug
advanced
2:00remaining
Identify the error in morphological closing code
The following MATLAB code is intended to perform morphological closing on a binary image. What error will it produce when run?
BW = imread('text.png');
se = strel('line', 5, 45)
BW2 = imclose(BW, se);
imshow(BW2);
MATLAB
BW = imread('text.png');
se = strel('line', 5, 45)
BW2 = imclose(BW, se);
imshow(BW2);
ARuntime error: 'text.png' file not found
BSyntaxError due to missing semicolon after strel line
CNo error, code runs and shows the closed image
DTypeError because imclose expects a binary image but BW is uint8
Attempts:
2 left
💡 Hint
Check the input type expected by imclose function.
🧠 Conceptual
advanced
1:30remaining
Effect of structuring element shape on morphological operations
Which statement best describes how the shape of the structuring element affects morphological operations like dilation and erosion?
AThe structuring element shape only affects the color of the output image, not the shape.
BThe structuring element shape determines the direction and extent of pixel expansion or shrinking in the image.
CThe structuring element shape has no effect; only its size matters.
DThe structuring element shape affects only grayscale images, not binary images.
Attempts:
2 left
💡 Hint
Think about how the shape controls which neighbors are considered during the operation.
❓ Predict Output
expert
3:00remaining
Output of morphological gradient on a binary image
What is the output of this MATLAB code that computes the morphological gradient of a binary image?
BW = [0 0 0 0 0; 0 1 1 0 0; 0 1 1 0 0; 0 0 0 0 0];
se = strel('square', 3);
BWd = imdilate(BW, se);
BWe = imerode(BW, se);
BWg = BWd - BWe;
disp(BWg)
MATLAB
BW = [0 0 0 0 0; 0 1 1 0 0; 0 1 1 0 0; 0 0 0 0 0];
se = strel('square', 3);
BWd = imdilate(BW, se);
BWe = imerode(BW, se);
BWg = BWd - BWe;
disp(BWg)
A[0 1 1 1 0; 1 0 0 1 0; 1 0 0 1 0; 0 1 1 1 0]
B[0 1 1 1 0; 1 1 1 1 0; 1 1 1 1 0; 0 1 1 1 0]
C[0 0 0 0 0; 0 1 1 0 0; 0 1 1 0 0; 0 0 0 0 0]
D[1 1 1 1 1; 1 1 1 1 1; 1 1 1 1 1; 1 1 1 1 1]
Attempts:
2 left
💡 Hint
Morphological gradient highlights edges by subtracting eroded image from dilated image.

Practice

(1/5)
1. Which MATLAB function is used to create a structuring element for morphological operations?
easy
A. imerode
B. strel
C. imopen
D. imdilate

Solution

  1. Step 1: Understand structuring element creation

    The structuring element defines the shape used in morphological operations.
  2. Step 2: Identify the MATLAB function for structuring elements

    In MATLAB, strel creates structuring elements like disks or lines.
  3. Final Answer:

    strel -> Option B
  4. Quick Check:

    Structuring element = strel [OK]
Hint: Remember: strel makes shapes for morphology [OK]
Common Mistakes:
  • Confusing strel with imopen or imerode
  • Using imdilate to create structuring elements
  • Thinking imopen creates structuring elements
2. Which of the following is the correct syntax to perform erosion on a binary image BW using a disk structuring element of radius 3?
easy
A. BW2 = imopen(BW, strel('disk', 3));
B. BW2 = imdilate(BW, strel('disk', 3));
C. BW2 = imerode(BW, strel('disk', 3));
D. BW2 = imclose(BW, strel('disk', 3));

Solution

  1. Step 1: Identify the erosion function

    In MATLAB, imerode performs erosion on images.
  2. Step 2: Check the syntax for erosion with a disk structuring element

    The correct syntax is imerode(image, strel('disk', radius)).
  3. Final Answer:

    BW2 = imerode(BW, strel('disk', 3)); -> Option C
  4. Quick Check:

    Erosion syntax = imerode(image, strel) [OK]
Hint: imerode + strel('disk', radius) erodes image [OK]
Common Mistakes:
  • Using imdilate instead of imerode
  • Wrong order of arguments
  • Missing quotes around 'disk'
3. What will be the output size of the binary image BW2 after applying BW2 = imdilate(BW, strel('square', 3)); if BW is a 5x5 matrix?
medium
A. Cannot determine without image content
B. 7x7
C. 3x3
D. 5x5

Solution

  1. Step 1: Understand dilation size effect

    Dilation expands shapes but does not change image size in MATLAB.
  2. Step 2: Check MATLAB behavior on image size

    MATLAB morphological functions keep the output image size same as input.
  3. Final Answer:

    5x5 -> Option D
  4. Quick Check:

    Output size = input size for imdilate [OK]
Hint: Morphology keeps image size same in MATLAB [OK]
Common Mistakes:
  • Assuming dilation increases image size
  • Confusing structuring element size with output size
  • Thinking output depends on image content
4. Consider the code snippet:
se = strel('line', 5, 45);
BW2 = imerode(BW, se);

What is the likely error if BW is a grayscale image instead of binary?
medium
A. No error; imerode works on grayscale images
B. Error because strel cannot create line elements
C. Error because imerode expects binary images only
D. Error due to missing image conversion to binary

Solution

  1. Step 1: Check imerode input types

    MATLAB's imerode supports both binary and grayscale images.
  2. Step 2: Verify structuring element creation

    strel('line', 5, 45) correctly creates a line structuring element.
  3. Final Answer:

    No error; imerode works on grayscale images -> Option A
  4. Quick Check:

    imerode supports grayscale and binary [OK]
Hint: imerode works on grayscale too, no conversion needed [OK]
Common Mistakes:
  • Thinking imerode only works on binary
  • Assuming strel('line') is invalid
  • Forgetting imerode supports grayscale images
5. You want to remove small noise dots from a binary image BW without changing the main shapes. Which morphological operation and structuring element combination is best?
hard
A. imopen with strel('disk', 2)
B. imclose with strel('square', 5)
C. imerode with strel('line', 3, 0)
D. imdilate with strel('diamond', 1)

Solution

  1. Step 1: Understand noise removal by morphology

    Opening removes small objects (noise) while preserving shape size.
  2. Step 2: Choose suitable structuring element

    A disk with radius 2 is good for removing small dots without harming main shapes.
  3. Final Answer:

    imopen with strel('disk', 2) -> Option A
  4. Quick Check:

    Opening + disk removes noise best [OK]
Hint: Use imopen with small disk to clean noise [OK]
Common Mistakes:
  • Using imclose which fills holes
  • Using erosion alone which shrinks shapes
  • Using dilation alone which enlarges noise
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard