label matrix coloring algorithm
Project description
ncolor 
Fast remapping of instance labels 1,2,3,...,M to a smaller set of repeating, disjoint labels, 1,2,...,N. The four color theorem guarantees that at most four colors are required for any 2D segmentation/map, but the stochastic algorithms of ncolor will opt for 5 or 6 to give an acceptable result if it fails to find a 4-color mapping quickly. Also works for 3D labels (<8 colors typically required) and perhaps higher dimensions as well.
Usage
If you have an integer array called masks, you may transform it into an N-color representation as follows:
import ncolor
ncolor_masks = ncolor.label(masks)
If you need the number of unique labels returned:
ncolor_masks, num_labels = ncolor.label(masks,return_n=True)
If you need to convert back to 0,...,N object labels:
labels = ncolor.format_labels(ncolor_masks,clean=True)
Note that format_labels with clean=True will also remove small labels (<9px) by default. This behavior can be changed with the min_area parameter.
The integer array ncolor_masks can be visualized using any color map you prefer. The example in this README uses the viridis colormap. See example.ipynb for more details.
Thanks to Ryan Peters (@ryanirl) for suggesting the expand_labels function. This is applied by default to 2D images (optionally for 3D images with expand=True, but this can give bad results since objects in 3D have a lot more wiggle room to make contact when expanded). This preprocessing step eliminates cases where close (but not touching) or dispersed objects previously received the same label. I dug a layer back to use ndimage.distance_transform_edt for a speed boost. If undesired for 2D images, use expand=False.
Project details
Release history Release notifications | RSS feed
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 ncolor-1.5.3.tar.gz.
File metadata
- Download URL: ncolor-1.5.3.tar.gz
- Upload date:
- Size: 63.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
887b3998cd89d589789875217556c27b911e18de5bf814327eb35f8c1c5e3e42
|
|
| MD5 |
e16c17cda48f2f1d7c5ed59c93cc4881
|
|
| BLAKE2b-256 |
cc7a8fabc3344335758f3d352135c55f0cc7e909ff25570d1c9966cc4c1d7c43
|
File details
Details for the file ncolor-1.5.3-py3-none-any.whl.
File metadata
- Download URL: ncolor-1.5.3-py3-none-any.whl
- Upload date:
- Size: 18.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
abf63f68aa5f13994459d7e8745b5428ad7301e7643835684e5094c5a7673337
|
|
| MD5 |
33e167875cf78e0270578c58a50b3461
|
|
| BLAKE2b-256 |
951e66986fe7b2a0fe8441ce37e0a1046dc676d6d326b14c6f9234542da1bcb1
|