Skip to main content

A small package with Numba-accelerated morphological operations.

Project description

nbmorph

Tests PyPI version

A small, Numba-accelerated Python package for morphological operations on 3D labeled images.

nbmorph provides a set of common morphological operations optimized for performance using Numba. It is designed to work with 3D NumPy arrays representing labeled image data, where different integer labels correspond to different objects.

Features

  • Numba-accelerated and multithreaded: Operations are just-in-time compiled with Numba for high performance on CPUs.

  • 3D Label Image Support: All operations are designed for 3D labeled images (integer NumPy arrays).

  • Quasi-Spherical Structuring Elements: Approximates spherical structuring elements by alternating between box and diamond kernels for dilation and erosion.

  • Core Morphological Operations:

    • dilate_labels_spherical: Expands the boundaries of labeled regions by assigning the mode of the neighborhood to background voxels.

    • erode_labels_spherical: Shrinks the boundaries of labeled regions.

    • open_labels_spherical: Removes small noise and thin protrusions (erosion followed by dilation).

    • close_labels_spherical: Fills small holes within objects (dilation followed by erosion).

    • smooth_labels_spherical: Smoothes object boundaries by performing an opening followed by a closing.

Effect of Morphological Smoothing Demonstration of the smoothing effect with varying radii and iterations on a sample image. The smoothing is followed by a dilation operation to fill up the empty space.

Installation

You can install nbmorph directly from pypi using pip:

pip install nbmorph

Usage

Here is a basic example of how to use nbmorph to apply operations to a 3D labeled image.

import numpy as np
import nbmorph
import numba

numba.set_num_threads(4)

# Create a sample 3D labeled image
# For example, a 5x5x5 cube of two different labels in a 10x10x10 volume
labels = np.zeros((10, 10, 10), dtype=np.uint16)
labels[2:7, 2:7, 2:5] = 1
labels[2:7, 2:7, 5:7] = 2

# First execution may take a while due to numba compilation
# Apply morphological erosion with a radius of 1
eroded_labels = nbmorph.erode_labels_spherical(labels, radius=1)

# Apply morphological dilation with a radius of 1
dilated_labels = nbmorph.dilate_labels_spherical(labels, radius=1)

# Apply morphological opening with a radius of 1
opened_labels = nbmorph.open_labels_spherical(labels, radius=1)

# Apply morphological closing with a radius of 1
closed_labels = nbmorph.close_labels_spherical(labels, radius=1)

# Apply morphological smoothing with a radius of 1
smoothed_labels = nbmorph.smooth_labels_spherical(labels, radius=1)

Testing

Tests are written using pytest. To run the tests, first install the test dependencies and then run pytest:

pip install .[test]
pytest

Benchmarking

A benchmark script is included in the scripts directory:

python scripts/benchmark.py

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

nbmorph-0.2.0.tar.gz (16.7 kB view details)

Uploaded Source

Built Distribution

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

nbmorph-0.2.0-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

Details for the file nbmorph-0.2.0.tar.gz.

File metadata

  • Download URL: nbmorph-0.2.0.tar.gz
  • Upload date:
  • Size: 16.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for nbmorph-0.2.0.tar.gz
Algorithm Hash digest
SHA256 61893ae683e95026c830c902d1c08a59b7b540c379fd3bcb3115dc21bdf308cd
MD5 63ea9cabe984a5e7ea0c2c08ef0dfbf5
BLAKE2b-256 bf0ac127fba95d00ef0a9505e7709d0d7c93c7c41fb9e68f000f27da7162ca44

See more details on using hashes here.

Provenance

The following attestation bundles were made for nbmorph-0.2.0.tar.gz:

Publisher: release.yaml on MariusCausemann/nbmorph

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nbmorph-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: nbmorph-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 15.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for nbmorph-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e537adef0200050cea049797e9e2efcf0cbf3abb29797859251f638d6e394c0b
MD5 4acc677ed455c7812f2eac813c7bd972
BLAKE2b-256 dbf67a547229f3cbe02a83b05fdd69383a61f845a9555a1b494835179827ec1a

See more details on using hashes here.

Provenance

The following attestation bundles were made for nbmorph-0.2.0-py3-none-any.whl:

Publisher: release.yaml on MariusCausemann/nbmorph

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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