Morphological Erosion and Dilation
Morphological Erosion and Dilation
Morphological operations are fundamental tools for processing binary images. Erosion shrinks foreground regions (removing thin protrusions and noise), while dilation expands them (filling small holes and connecting nearby components).
Given a binary image (0s and 1s), a binary structuring element (kernel), and an operation type ("erode" or "dilate"), apply the morphological operation with zero-padding.
Algorithm
Pad the image with zeros using padding = kernel_size // 2 on each side. For each output pixel at position (i, j):
Erosion: the output is 1 only if every position where the kernel is 1 also has a 1 in the corresponding image position. Otherwise the output is 0.
Dilation: the output is 1 if any position where the kernel is 1 has a 1 in the corresponding image position. Otherwise the output is 0.
Examples
Input:
image = [[0,0,0],[0,1,0],[0,0,0]], kernel = [[1,1,1],[1,1,1],[1,1,1]], operation = "dilate"
Output:
[[1,1,1],[1,1,1],[1,1,1]]
The single center pixel expands in all directions covered by the 3x3 kernel.
Input:
image = [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]], kernel = [[1,1,1],[1,1,1],[1,1,1]], operation = "erode"
Output:
[[0,0,0,0],[0,1,1,0],[0,1,1,0],[0,0,0,0]]
Border pixels are eroded because the kernel extends into the zero-padded region.
Hint 1
Padding size is kernel_height // 2 for rows and kernel_width // 2 for columns. This centers the kernel over each output pixel.
Hint 2
For erosion you can break early (set to 0) as soon as one kernel-1 position fails. For dilation, break early (set to 1) as soon as one match is found.
Requirements
- Apply zero-padding based on kernel dimensions
- For erosion, check that ALL kernel-1 positions match image-1 positions
- For dilation, check that ANY kernel-1 position matches an image-1 position
- Output has the same dimensions as the input
Constraints
- Image contains only 0s and 1s
- Kernel contains only 0s and 1s, with odd dimensions
- operation is either "erode" or "dilate"
- Return a 2D list of integers (0 or 1) with the same shape as input
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array
Accepts: array
Accepts: string
Morphological Erosion and Dilation
Morphological Erosion and Dilation
Morphological operations are fundamental tools for processing binary images. Erosion shrinks foreground regions (removing thin protrusions and noise), while dilation expands them (filling small holes and connecting nearby components).
Given a binary image (0s and 1s), a binary structuring element (kernel), and an operation type ("erode" or "dilate"), apply the morphological operation with zero-padding.
Algorithm
Pad the image with zeros using padding = kernel_size // 2 on each side. For each output pixel at position (i, j):
Erosion: the output is 1 only if every position where the kernel is 1 also has a 1 in the corresponding image position. Otherwise the output is 0.
Dilation: the output is 1 if any position where the kernel is 1 has a 1 in the corresponding image position. Otherwise the output is 0.
Examples
Input:
image = [[0,0,0],[0,1,0],[0,0,0]], kernel = [[1,1,1],[1,1,1],[1,1,1]], operation = "dilate"
Output:
[[1,1,1],[1,1,1],[1,1,1]]
The single center pixel expands in all directions covered by the 3x3 kernel.
Input:
image = [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]], kernel = [[1,1,1],[1,1,1],[1,1,1]], operation = "erode"
Output:
[[0,0,0,0],[0,1,1,0],[0,1,1,0],[0,0,0,0]]
Border pixels are eroded because the kernel extends into the zero-padded region.
Hint 1
Padding size is kernel_height // 2 for rows and kernel_width // 2 for columns. This centers the kernel over each output pixel.
Hint 2
For erosion you can break early (set to 0) as soon as one kernel-1 position fails. For dilation, break early (set to 1) as soon as one match is found.
Requirements
- Apply zero-padding based on kernel dimensions
- For erosion, check that ALL kernel-1 positions match image-1 positions
- For dilation, check that ANY kernel-1 position matches an image-1 position
- Output has the same dimensions as the input
Constraints
- Image contains only 0s and 1s
- Kernel contains only 0s and 1s, with odd dimensions
- operation is either "erode" or "dilate"
- Return a 2D list of integers (0 or 1) with the same shape as input
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array
Accepts: array
Accepts: string