📖 Scenario: You are working with a simple black and white image representing shapes. You want to clean up small noise and fill small holes using morphological operations.
🎯 Goal: Learn how to apply basic morphological operations like dilation and erosion on a binary image in MATLAB.
📋 What You'll Learn
Create a binary image matrix with specific pattern
Define a structuring element for morphological operations
Apply dilation and erosion using MATLAB functions
Display the original and processed images
💡 Why This Matters
🌍 Real World
Morphological operations help clean up images by removing noise or filling gaps, useful in medical imaging, document scanning, and object detection.
💼 Career
Understanding these operations is important for roles in image processing, computer vision, and machine learning where image data needs preprocessing.
Progress0 / 4 steps
1
Create the binary image matrix
Create a binary matrix called bw with size 7x7 where the center 3x3 block is 1s and the rest are 0s.
MATLAB
Hint
Use zeros(7,7) to create a matrix of zeros and assign 1s to the center block using indexing.
2
Define the structuring element
Create a structuring element called se using strel('square', 3).
MATLAB
Hint
Use the strel function with 'square' shape and size 3.
3
Apply dilation and erosion
Create two new matrices: dilated_bw by dilating bw with se, and eroded_bw by eroding bw with se.
MATLAB
Hint
Use imdilate and imerode functions with bw and se.
4
Display the images
Use imshow to display bw, dilated_bw, and eroded_bw side by side using subplot. Show bw in the first subplot, dilated_bw in the second, and eroded_bw in the third.
MATLAB
Hint
Use subplot(1,3,i) and imshow for each image, then add title for clarity.
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
Step 1: Understand structuring element creation
The structuring element defines the shape used in morphological operations.
Step 2: Identify the MATLAB function for structuring elements
In MATLAB, strel creates structuring elements like disks or lines.
Final Answer:
strel -> Option B
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
Step 1: Identify the erosion function
In MATLAB, imerode performs erosion on images.
Step 2: Check the syntax for erosion with a disk structuring element
The correct syntax is imerode(image, strel('disk', radius)).
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
Step 1: Understand dilation size effect
Dilation expands shapes but does not change image size in MATLAB.
Step 2: Check MATLAB behavior on image size
MATLAB morphological functions keep the output image size same as input.
Final Answer:
5x5 -> Option D
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
Step 1: Check imerode input types
MATLAB's imerode supports both binary and grayscale images.
Step 2: Verify structuring element creation
strel('line', 5, 45) correctly creates a line structuring element.
Final Answer:
No error; imerode works on grayscale images -> Option A
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
Step 1: Understand noise removal by morphology
Opening removes small objects (noise) while preserving shape size.
Step 2: Choose suitable structuring element
A disk with radius 2 is good for removing small dots without harming main shapes.
Final Answer:
imopen with strel('disk', 2) -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.