Skip to main content

Python library for morphological cleaning of multiclass 2D numpy arrays (edge smoothing and island removal)

Project description

MultiClean

image Python 3.9+ License: MIT Tutorials

MultiClean is a Python library for morphological cleaning of multiclass 2D numpy arrays (segmentation masks and classification rasters). It provides efficient tools for edge smoothing and small-island removal across multiple classes, then fills gaps using the nearest valid class.

Visual Example

Below: Land Use before/after cleaning (smoothed edges, small-island removal, nearest-class gap fill).

Land Use before/after

Key Features

  • Multi-class processing: Clean all classes in one pass
  • Edge smoothing: Morphological opening to reduce jagged boundaries
  • Island removal: Remove small connected components per class
  • Gap filling: Fill invalids via nearest valid class (distance transform)
  • Fast: NumPy + OpenCV + SciPy with parallelism

Installation

pip install multiclean

or

uv add multiclean

Quick Start

import numpy as np
from multiclean import clean_array

# Create a sample classification array with classes 0, 1, 2, 3
array = np.random.randint(0, 4, (1000, 1000), dtype=np.int32)

# Clean with default parameters
cleaned = clean_array(array)

# Custom parameters
cleaned = clean_array(
    array,
    class_values=[0, 1, 2, 3],
    smooth_edge_size=2,     # kernel width, larger value increases smoothness
    min_island_size=100,    # remove components with area < 100
    connectivity=8,         # 4 or 8
    max_workers=4,
)

Examples

See the notebooks folder for end-to-end examples:

Try in Colab

Colab_Button

Use Cases

MultiClean is designed for cleaning segmentation outputs from:

  • Remote sensing: Land cover classification, crop mapping
  • Computer vision: Semantic segmentation post-processing
  • Geospatial analysis: Raster classification cleaning
  • Machine learning: Neural network output refinement

How It Works

MultiClean uses morphological operations to clean classification arrays:

  1. Edge smoothing (per class): Morphological opening with an elliptical kernel.
  2. Island removal (per class): Find connected components (OpenCV) and mark components with area < min_island_size as invalid.
  3. Gap filling: Compute a distance transform to copy the nearest valid class into invalid pixels.

Classes are processed together and the result maintains a valid label at every pixel.

API

clean_array

from multiclean import clean_array

out = clean_array(
    array: np.ndarray,
    class_values: int | list[int] | None = None,
    smooth_edge_size: int = 2,
    min_island_size: int = 100,
    connectivity: int = 4,
    max_workers: int | None = None,
)
  • array: 2D numpy array of class labels (int or float). For float arrays, NaN is treated as nodata and will remain NaN.
  • class_values: Classes to consider. If None, inferred from array (ignores NaN for floats). An int restricts cleaning to a single class.
  • smooth_edge_size: Kernel size (pixels) for morphological opening. Use 0 to disable.
  • min_island_size: Remove components with area strictly < min_island_size. Use 1 to keep single pixels.
  • connectivity: Pixel connectivity for components, 4 or 8.
  • max_workers: Parallelism for per-class operations (None lets the executor choose).

Returns a numpy array matching the input shape. Integer inputs return integer outputs. Float arrays with NaN are supported (treated as nodata and retained as NaN).

Performance

MultiClean is optimised for large arrays:

  • Vectorised operations using NumPy, OpenCV, and SciPy
  • Parallel processing for island detection across classes
  • Fast distance transforms for gap filling

Examples

Cleaning Satellite Land Cover Data

from multiclean import clean_array
import rasterio

# Read land cover classification
with rasterio.open('landcover.tif') as src:
    landcover = src.read(1)

# Clean with appropriate parameters for satellite data
cleaned = clean_array(
    landcover,
    class_values=[0, 1, 2, 3, 4],  # forest, water, urban, crop, other
    smooth_edge_size=1,
    min_island_size=25,
    connectivity=8,
)

Cleaning Neural Network Segmentation Output

from multiclean import clean_array

# Model produces logits; convert to class predictions
np_pred = np_model_logits.argmax(axis=0)  # shape: (H, W)

# Clean the segmentation
cleaned = clean_array(
    np_pred,
    smooth_edge_size=2,
    min_island_size=100,
    connectivity=4,
)

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

multiclean-0.1.1.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

multiclean-0.1.1-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file multiclean-0.1.1.tar.gz.

File metadata

  • Download URL: multiclean-0.1.1.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.0

File hashes

Hashes for multiclean-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7e4b388d866bf2eb2b5d24794cb8758575527850056188f8e1e5e62d2fa295f2
MD5 917839d315d6292ed247d779740adcf2
BLAKE2b-256 981aec07276cb3b144d3258e1feb902b6e38479bfea95416745a2385f96c8043

See more details on using hashes here.

File details

Details for the file multiclean-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for multiclean-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8dc6faa3971667c1a2efc79117fdb51d2dec77209e34e56d0cd89b818d575bdf
MD5 6055e981bd9dbceeb5dc56fea3a587d9
BLAKE2b-256 e027a7906ba79b48d9d852aa58dc8489ff3400771de5a17870d055d2711a16b5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page