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
Filtered Image

Opened image - noise removed, main objects preserved
The Code
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" / "broken_cables.webp")
image = io.load_image(filepath=filepath, as_binary=True, binary_method='fixed')
logger.success(f"Loaded image from {filepath}")
# Apply open morphological operation
filtered_image = pupil.filter_image_using_morphological_open(
image=image,
kernel_size=3,
kernel_shape="ellipse",
iterations=2,
border_type="constant",
border_value=0,
)
# Access results
filtered_image_np = filtered_image.to_numpy()
logger.success("Applied open 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.
from telekinesis import pupil
from datatypes import io
import pathlib
from loguru import loggerNext, a binary mask is loaded from a .png file using the io.load_image function. The image is loaded as binary using as_binary=True, which creates a binary mask where opening can effectively remove small noise while preserving larger structures.
DATA_DIR = pathlib.Path("path/to/telekinesis-data")
# Load the binary image
filepath = str(DATA_DIR / "images" / "broken_cables.webp")
image = io.load_image(filepath=filepath, as_binary=True, binary_method='fixed')The main operation uses the filter_image_using_morphological_open Skill from the pupil module. This Skill applies morphological opening (erosion followed by dilation) to remove small bright spots and thin connections while preserving object size. The parameters can be tuned to control noise removal strength, kernel shape, iteration count, and border handling depending on the characteristics of the input image.
filtered_image = pupil.filter_image_using_morphological_open(
image=image,
kernel_size=3,
kernel_shape="ellipse",
iterations=2,
border_type="constant",
border_value=0,
)Finally, the filtered image is converted to a NumPy array using to_numpy() for further processing, visualization, or downstream tasks.
filtered_image_np = filtered_image.to_numpy()
logger.success(f"Output image shape: {filtered_image_np.shape}")This operation is particularly useful in robotics and vision pipelines for noise removal, binary image cleanup, object separation, and segmentation refinement, where removing small noise while maintaining object size 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:
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.
kernel_size (default: 3): Controls the size of the structuring element used for opening (erosion then dilation).
- Units: Pixels
- Increase to remove larger noise and small objects; Decrease to keep more detail and remove only very small noise
- Typical range: 3–15 (min: 3, max: 15)
- Use 3–5 for fine salt noise, 5–9 for moderate noise, 9–15 for larger speckles or thin connections
kernel_shape (default: ellipse): Defines the geometric shape of the structuring element used for erosion and dilation.
- Options: "ellipse", "rectangle", "cross", "diamond"
- "ellipse" gives smooth, isotropic opening; "rectangle" and "cross" for directional effects
- Use "ellipse" for most cases
iterations (default: 1): Specifies how many times the morphological opening operation is applied sequentially.
- Units: Count
- Increase to remove progressively larger noise; Decrease for less aggressive removal (too many can remove small valid features)
- 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 pixelreflect- Mirrors the border pixelsreflect 101- Mirror reflection without repeating the border pixel
- Use
defaultorreflectfor most cases
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
Related skills to build such a pipeline:
filter_image_using_morphological_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)

