Python library for morphological cleaning of multiclass 2D numpy arrays (edge smoothing and island removal)
Project description
MultiClean
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).
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,
fill_nan=False # enable/disable the filling of nan values in input array
)
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
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 with parallelism
How It Works
MultiClean uses morphological operations to clean classification arrays:
- Edge smoothing (per class): Morphological opening with a circular kernel.
- Island removal (per class): Find connected components (OpenCV) and mark components with area
< min_island_sizeas invalid. - 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 Reference
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,
fill_nan: bool = False
)
array: 2D numpy array of class labels (int or float). For float arrays,NaNis treated as nodata and will remainNaNunlessfill_nanis set toTrue.class_values: Classes to consider. IfNone, inferred fromarray(ignoresNaNfor floats). An int restricts cleaning to a single class.smooth_edge_size: Kernel size (pixels) for morphological opening. Use0to disable.min_island_size: Remove components with area strictly< min_island_size. Use1to keep single pixels.connectivity: Pixel connectivity for components,4or8.max_workers: Parallelism for per-class operations (None lets the executor choose).fill_nan: If True will fill NAN values from input array with nearest valid value.
Returns a numpy array matching the input shape and dtype. Float arrays with NaN are supported and can be filled or remain as NaN.
Examples
Cleaning 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,
fill_nan=False
)
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,
)
Notebooks
See the notebooks folder for end-to-end examples:
- Land Use Example Notebook: land use classification cleaning
- Cloud Example Notebook: cloud/shadow classification cleaning
Try in Colab
Changelog
Release notes and the full version history are kept in CHANGELOG.md.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Maintainers: see RELEASING.md for how to cut a release.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file multiclean-0.3.0.tar.gz.
File metadata
- Download URL: multiclean-0.3.0.tar.gz
- Upload date:
- Size: 7.4 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1265a70845539e8cdf00dafa651ab33ed525a09ff9a4fefde3859816c09bcb04
|
|
| MD5 |
f66f304d5afed372f72b8e8e7bf0e82b
|
|
| BLAKE2b-256 |
a192767ca6d043c5cae1de109be4220c69808bd4c9c2b2299208b5a65a6183e7
|
Provenance
The following attestation bundles were made for multiclean-0.3.0.tar.gz:
Publisher:
publish.yml on DPIRD-DMA/MultiClean
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
multiclean-0.3.0.tar.gz -
Subject digest:
1265a70845539e8cdf00dafa651ab33ed525a09ff9a4fefde3859816c09bcb04 - Sigstore transparency entry: 1436517723
- Sigstore integration time:
-
Permalink:
DPIRD-DMA/MultiClean@77f014dc3346b0586d0747c1010bc3947821bc02 -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/DPIRD-DMA
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@77f014dc3346b0586d0747c1010bc3947821bc02 -
Trigger Event:
push
-
Statement type:
File details
Details for the file multiclean-0.3.0-py3-none-any.whl.
File metadata
- Download URL: multiclean-0.3.0-py3-none-any.whl
- Upload date:
- Size: 10.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fc2bc3f9a38b6c3336e071de458766c7fb3e870462059bf064f4c77eb25353d0
|
|
| MD5 |
a0bbbd5cd8c035e5b618dedb72fcf6a2
|
|
| BLAKE2b-256 |
8f86c203ee38dc23acba9b49512237aa4d55d87ae3d2e28cd97ae82b00bae460
|
Provenance
The following attestation bundles were made for multiclean-0.3.0-py3-none-any.whl:
Publisher:
publish.yml on DPIRD-DMA/MultiClean
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
multiclean-0.3.0-py3-none-any.whl -
Subject digest:
fc2bc3f9a38b6c3336e071de458766c7fb3e870462059bf064f4c77eb25353d0 - Sigstore transparency entry: 1436517736
- Sigstore integration time:
-
Permalink:
DPIRD-DMA/MultiClean@77f014dc3346b0586d0747c1010bc3947821bc02 -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/DPIRD-DMA
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@77f014dc3346b0586d0747c1010bc3947821bc02 -
Trigger Event:
push
-
Statement type: