Skip to content

Filter Image Using Morphological Erosion

SUMMARY

Filter Image Using Morphological Erosion applies erosion to shrink bright regions and remove small noise.

Erosion removes pixels from object boundaries, useful for removing small bright spots and shrinking objects. This morphological operation is fundamental in image processing, particularly for cleaning up binary images, separating touching objects, and removing noise. It works by replacing each pixel with the minimum value in its neighborhood defined by the structuring element.

Use this Skill when you want to remove small bright features and shrink objects.

The Skill

python
from telekinesis import pupil

filtered_image = pupil.filter_image_using_morphological_erode(
    image=image,
    kernel_size=5,
    kernel_shape="ellipse",
    iterations=1,
    border_type="default",
)

API Reference

Example

Input Image

Input image

Original grayscale or binary image

Filtered Image

Output image

Eroded image showing small features removed and objects shrunk

The Code

python
from telekinesis import pupil
from datatypes import io
import pathlib
from loguru import logger

DATA_DIR = pathlib.Path("path/to/telekinesis-data")

# Load image
filepath = str(DATA_DIR / "images" / "gear_with_texture.jpg")
image = io.load_image(filepath=filepath, as_binary=True, binary_method='otsu')
logger.success(f"Loaded image from {filepath}")

# Apply erosion morphological operation
filtered_image = pupil.filter_image_using_morphological_erode(
    image=image,
    kernel_size=5,
    kernel_shape="ellipse",
    iterations=10,
    border_type="default",
)

# Access results
filtered_image_np = filtered_image.to_numpy()
logger.success("Applied erosion morphological operation. Output image shape: {}", filtered_image_np.shape)

The Explanation of the Code

The code begins by importing the necessary modules: pupil for image processing operations, io for data handling, pathlib for path management, and loguru for logging.

python
from telekinesis import pupil
from datatypes import io
import pathlib
from loguru import logger

Next, an image is loaded from disk using the io.load_image function. The image is loaded as binary using as_binary=True, which is appropriate for morphological erosion operations.

python
DATA_DIR = pathlib.Path("path/to/telekinesis-data")

# Load image
filepath = str(DATA_DIR / "images" / "gear_with_texture.jpg")
image = io.load_image(filepath=filepath, as_binary=True, binary_method='otsu')

The main operation uses the filter_image_using_morphological_erode Skill from the pupil module. This Skill shrinks bright regions by replacing each pixel with the minimum value in its neighborhood defined by the structuring element. The parameters can be tuned to control the degree of erosion, feature removal, and border handling depending on the characteristics of the input image.

python
filtered_image = pupil.filter_image_using_morphological_erode(
    image=image,
    kernel_size=5,
    kernel_shape="ellipse",
    iterations=10,
    border_type="default",
)
logger.success("Applied erosion morphological operation. Output image shape: {}", filtered_image.to_numpy().shape)

Finally, the filtered image is converted to a NumPy array using to_numpy() for further processing, visualization, or downstream tasks.

python
filtered_image_np = filtered_image.to_numpy()
logger.success("Output image shape: {}", filtered_image_np.shape)

This operation is particularly useful in robotics and vision pipelines for binary image cleanup, object separation, noise removal, and preprocessing for segmentation, where shrinking bright regions is required.

Running the Example

Runnable examples are available in the Telekinesis examples repository. Follow the README in that repository to set up the environment. Once set up, you can run this specific example with:

bash
cd telekinesis-examples
python examples/pupil_examples.py --example filter_image_using_morphological_erode

How to Tune the Parameters

The filter_image_using_morphological_erode Skill has 4 parameters that control erosion strength and feature removal:

kernel_size (default: 3): Controls the size of the structuring element used for erosion.

  • Units: Pixels
  • Increase to remove larger features and shrink objects more; Decrease for less aggressive erosion
  • Typical range: 3–15 (min: 3, max: 15)
  • Use 3–5 for small noise, 5–9 for moderate features, 9–15 for large features

kernel_shape (default: ellipse): Defines the geometric shape of the structuring element used for erosion.

  • Options:
    • ellipse - Produces smooth, isotropic erosion (recommended for most cases)
    • rectangle - Erosion aligned with horizontal and vertical directions
    • cross - Emphasizes orthogonal structures
    • diamond - Approximates circular morphology with sharper diagonal influence
  • Use ellipse for most cases

iterations (default: 1): Specifies how many times the erosion operation is applied sequentially.

  • Units: Count
  • Increase to apply more erosion; Decrease for less erosion
  • Typical range: 1–10 (min: 1, max: 10)
  • Each iteration compounds the effect

border_type (default: default): Determines how image borders are handled when the structuring element extends beyond the image boundary.

  • Options:
    • default - Uses reflect 101 padding (recommended)
    • constant - Pads borders with a constant value (typically zero)
    • replicate - Repeats the nearest edge pixel
    • reflect - Mirrors the border pixels
    • reflect 101 - Mirror reflection without repeating the border pixel
  • Use default or reflect for most cases

TIP

Best practice: Start with kernel_size=3 and iterations=1. If small noise remains, increase kernel_size slightly. If objects become too small, reduce kernel_size or use fewer iterations.

Where to Use the Skill in a Pipeline

Morphological Erosion is commonly used in the following pipelines:

  • Binary image cleanup - Remove small noise spots from thresholded images
  • Object separation - Break thin connections between touching objects
  • Feature removal - Remove features smaller than the structuring element
  • Preprocessing for segmentation - Clean images before analysis
  • Shape analysis - Simplify object boundaries

Related skills to build such a pipeline:

  • filter_image_using_morphological_dilate: Expand objects after erosion
  • filter_image_using_morphological_open: Combined erosion-dilation operation
  • filter_image_using_median_blur: Alternative noise removal method

Alternative Skills

Skillvs. Morphological Erosion
filter_image_using_morphological_openOpening is erosion followed by dilation. Use opening to remove noise while preserving object size, erosion when you want to shrink objects.
filter_image_using_median_blurMedian blur removes noise on grayscale images. Use erosion for binary images, median blur for grayscale.

When Not to Use the Skill

Do not use Morphological Erosion when:

  • You want to preserve object size (Use opening instead, which dilates after eroding)
  • You're working with grayscale noise (Use median or Gaussian blur instead)
  • You want to fill holes (Use dilation or closing instead)
  • You need to preserve fine details (Erosion will remove small features)