Binary Image Erosion
Implement a morphological operation to apply erosion to a given binary image using a structuring element. The goal is to produce a new binary image where each pixel is 1 only if all corresponding pixels in the image covered by the structuring element are also 1.
The concept of erosion is fundamental in morphological operations, which are used to manipulate and analyze the shape and structure of objects in images. Erosion is a process that shrinks or reduces the size of objects in an image by removing pixels from the object boundaries. This is achieved by applying a structuring element, a small binary image, to the original image. The structuring element is used to probe the image, and for each pixel, it checks if all the pixels covered by the structuring element are 1.
To perform erosion, the following steps are taken:
- Center the structuring element at each pixel in the image.
- Check if all the image pixels under the 1-entries of the structuring element are 1. Entries where the structuring element is 0 are ignored, and any position that falls outside the image counts as 0.
- If all the pixels are 1, the corresponding pixel in the output image is set to 1; otherwise, it is set to 0.
This technique is widely used in image processing and computer vision applications to remove noise and extract features from images.
Example
image = [[0,0,0,0,0],[0,1,1,1,0],[0,1,1,1,0],[0,1,1,1,0],[0,0,0,0,0]] se = [[0,1,0],[1,1,1],[0,1,0]]
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
imageis a image with a square of 1s (rows 1–3, columns 1–3) on 0s.seis a cross: centre plus the four direct neighbours are 1; its 0 corners play no part.- Centre the cross on a pixel: the output there is 1 only if the pixel and its four direct neighbours are all 1, i.e. of those five values.
- Centre pixel : itself, , , and are all 1, so the minimum is 1.
- Edge pixel : its upper neighbour is 0, so the minimum is 0. Corner fails twice, via and .
- Result:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]. Notice the 9-pixel square shrank to a single pixel: erosion peels away every pixel touching the background. Positions beyond the image border count as 0, so an all-1s image still loses its outer ring.
Constraints
- image is a 2D binary list (0 or 1)
- se (structuring element) is a 2D binary list with odd dimensions
- Return eroded binary image (same size as input)