Filter Image Using Morphological Open
SUMMARY
Filter Image Using Morphological Open applies morphological opening (erosion followed by dilation).
Opening removes small bright spots and thin connections, useful for noise removal while preserving object size. This operation first shrinks objects to remove noise, then expands them back to approximately their original size. It's particularly effective for removing isolated noise pixels and thin protrusions without significantly affecting the main object structure.
Use this Skill when you want to remove small noise while maintaining object size.
The Skill
from telekinesis import pupil
filtered_image = pupil.filter_image_using_morphological_open(
image=image,
kernel_size=5,
kernel_shape="ellipse",
iterations=1,
border_type="default",
)Example
Input Image

Original image with noise and thin connections
Output Image

Opened image - noise removed, main objects preserved
The Code
from telekinesis import pupil
from datatypes import io, datatypes
import pathlib
from loguru import logger
DATA_DIR = pathlib.Path("path/to/telekinesis-data")
# Load the binary image
filepath = str(DATA_DIR / "images" / "broken_cables.png")
image = io.load_image(filepath=filepath, as_binary=True, binary_method='fixed')
# Apply opening
filtered_image = pupil.filter_image_using_morphological_open(
image=image,
kernel_size=3,
kernel_shape="ellipse",
iterations=2,
border_type="default",
)
logger.success(f"Applied opening with opened image shape as {filtered_image.to_numpy().shape}")The Explanation of the Code
The morphological opening implementation follows a standard image processing workflow with proper parameter configuration.
First, import the required modules:
from telekinesis import pupil
from datatypes import io, datatypes
import pathlib
from loguru import loggerThese imports provide access to the Pupil SDK, type-safe datatypes for both I/O and parameters, path handling utilities, and logging functionality.
Next, prepare your image data:
DATA_DIR = pathlib.Path("path/to/telekinesis-data")
filepath = str(DATA_DIR / "images" / "broken_cables.png")
image = io.load_image(filepath=filepath, as_binary=True, binary_method='fixed')The image is loaded from the data directory using the io module. For morphological operations, the image is loaded as a binary image using the as_binary=True parameter, which creates a binary mask where opening can effectively remove small noise while preserving larger structures.
Next, the parameters controlling the opening operation are configured. Opening is a compound operation that first erodes the image to remove small noise features, then dilates back to restore the approximate size of larger objects.
kernel_size determines the size of the structuring element used for opening. Larger kernels remove larger noise features and small objects, while smaller kernels preserve more detail and only remove very small noise.
kernel_shape defines the geometric shape of the structuring element. Common shapes include "ellipse" for smooth, isotropic opening, "rectangle" for axis-aligned operations, and "cross" for directional effects. The shape affects how noise is removed and how object boundaries are smoothed.
iterations controls how many times the opening operation (erosion followed by dilation) is applied sequentially. Each iteration removes progressively larger noise features, but may also remove legitimate small objects if iterations are too high.
border_type specifies how pixels near image boundaries are handled during opening, where the structuring element extends beyond the image edges. This affects how objects near borders are processed.
Finally, the opening operation is applied using the filter_image_using_morphological_open Skill.
filtered_image = pupil.filter_image_using_morphological_open(
image=image,
kernel_size=3,
kernel_shape="ellipse",
iterations=2,
border_type="default",
)
logger.success(f"Applied opening with opened image shape as {filtered_image.to_numpy().shape}")Opening first erodes the image to remove small noise features, then dilates back to restore the approximate size of larger objects. This removes features smaller than the structuring element while maintaining the size of larger objects, making it the go-to operation for binary image cleanup and removing salt noise from thresholded images.
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:
cd telekinesis-examples
python examples/pupil_examples.py --example filter_image_using_morphological_openHow to Tune the Parameters
The filter_image_using_morphological_open Skill has 4 parameters that mirror other morphological operations. The key parameter is kernel_size which determines what counts as "noise" versus "signal" - features smaller than the kernel are removed.
TIP
Best practice: Opening is the go-to operation for binary image cleanup. Use kernel_size slightly larger than the noise you want to remove but smaller than the features you want to keep.
Where to Use the Skill in a Pipeline
Morphological Opening is commonly used in the following pipelines:
- Binary image cleanup - Remove salt noise from thresholded images
- Object separation - Break thin connections between objects
- Preprocessing for counting - Clean objects before counting
- Shape analysis - Simplify shapes by removing protrusions
- Segmentation refinement - Clean up segmentation results
A typical pipeline for object counting looks as follows:
# Example pipeline using opening (parameters omitted).
from telekinesis import pupil
from datatypes import io
# 1. Load surface image
image = io.load_image(filepath=...)
binary = ...
# 2. Apply opening to remove noise
cleaned = pupil.filter_image_using_morphological_open(image=binary, ...)
# 3. Count objects
count = ...Related skills to build such a pipeline:
load_image: Load imagesfilter_image_usingmorphological_close: Fill holesfilter_image_using_median_blur: Alternative noise removal for grayscale
Alternative Skills
| Skill | vs. Morphological Opening |
|---|---|
| filter_image_using_morphological_close | Closing fills holes, opening removes noise. Use opening for noise removal, closing for hole filling. |
| filter_image_using_median_blur | Median blur works on grayscale. Use opening for binary images, median for grayscale noise. |
When Not to Use the Skill
Do not use Morphological Opening when:
- You want to fill holes (Use closing instead)
- Small features are important (Opening will remove them)
- You're working with grayscale images (Use filters instead)
- You need to preserve thin structures (Opening will remove them)

