TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Morphological Erosion and Dilation

Computer Vision
Medium

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.

Loading visualization...

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]]

Explanation: The active center pixel reaches every location covered by the 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]]

Hint 1

For erosion, begin with one and clear it when any active-kernel position sees zero.

Hint 2

For dilation, begin with zero and set it when any active-kernel position sees one.

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
Try Similar Problems
Conv2d Image FilteringGaussian Blur KernelSobel Edge DetectionColor To GrayscaleHistogram Equalization

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

Accepts: string

You must run your code first.
PrevNext

Morphological Erosion and Dilation

Computer Vision
Medium

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.

Loading visualization...

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]]

Explanation: The active center pixel reaches every location covered by the 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]]

Hint 1

For erosion, begin with one and clear it when any active-kernel position sees zero.

Hint 2

For dilation, begin with zero and set it when any active-kernel position sees one.

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
Try Similar Problems
Conv2d Image FilteringGaussian Blur KernelSobel Edge DetectionColor To GrayscaleHistogram Equalization

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

Accepts: string

You must run your code first.