[](https://badge.fury.io/py/ncolor)
[](https://pepy.tech/project/ncolor)
# ncolor <img src="https://github.com/kevinjohncutler/ncolor/blob/main/logo.png?raw=true" width="400" title="bacteria" alt="bacteria" align="right" vspace = "0">
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 this algorithm 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:
```python
import ncolor
ncolor_masks = ncolor.label(masks)
```
If you need the number of unique labels returned:
```python
ncolor_masks, num_labels = ncolor.label(masks,return_n=True)
```
If you need to convert back to `0,...,N` object labels:
```python
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](https://github.com/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`.
Raw data
{
"_id": null,
"home_page": "https://github.com/kevinjohncutler/ncolor",
"name": "ncolor",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "Kevin Cutler",
"author_email": "kevinjohncutler@outlook.com",
"download_url": "https://files.pythonhosted.org/packages/a2/98/07dcf5d1459e1b92fae5073100dad2e9626570c2ac4b8342dfb45a3d64f4/ncolor-1.2.1.tar.gz",
"platform": null,
"description": "[](https://badge.fury.io/py/ncolor)\n[](https://pepy.tech/project/ncolor)\n\n# ncolor <img src=\"https://github.com/kevinjohncutler/ncolor/blob/main/logo.png?raw=true\" width=\"400\" title=\"bacteria\" alt=\"bacteria\" align=\"right\" vspace = \"0\">\n\nFast 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 this algorithm 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.\n\n### Usage\nIf you have an integer array called `masks`, you may transform it into an N-color representation as follows:\n\n```python\nimport ncolor \nncolor_masks = ncolor.label(masks)\n```\n\nIf you need the number of unique labels returned:\n```python\nncolor_masks, num_labels = ncolor.label(masks,return_n=True)\n\n```\nIf you need to convert back to `0,...,N` object labels:\n```python\nlabels = ncolor.format_labels(ncolor_masks,clean=True)\n\n```\n\nNote that `format_labels` with ```clean=True``` will also remove small labels (<9px) by default. This behavior can be changed with the `min_area` parameter. \n\n \nThe 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.\n\nThanks to Ryan Peters ([@ryanirl](https://github.com/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`. \n",
"bugtrack_url": null,
"license": "BSD",
"summary": "label matrix coloring algorithm",
"version": "1.2.1",
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "7be7fbbe3ae9ae234e8edddcec9b0613f49ec28a8552bf7186f3dd66dace8bc2",
"md5": "147ffb16d76149047efcabe674723031",
"sha256": "204b94e3508181374d7a4266c735f07c89de6e5a5229eb70fbdce7d55442d849"
},
"downloads": -1,
"filename": "ncolor-1.2.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "147ffb16d76149047efcabe674723031",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 7508,
"upload_time": "2023-02-09T10:29:43",
"upload_time_iso_8601": "2023-02-09T10:29:43.146520Z",
"url": "https://files.pythonhosted.org/packages/7b/e7/fbbe3ae9ae234e8edddcec9b0613f49ec28a8552bf7186f3dd66dace8bc2/ncolor-1.2.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "a29807dcf5d1459e1b92fae5073100dad2e9626570c2ac4b8342dfb45a3d64f4",
"md5": "770a6f78a70b1a06a709607d7f4ed25e",
"sha256": "f05bfc75b8f1ea3ba9ee61c353e13146064bd63503a1655accee00a602ae2797"
},
"downloads": -1,
"filename": "ncolor-1.2.1.tar.gz",
"has_sig": false,
"md5_digest": "770a6f78a70b1a06a709607d7f4ed25e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 43396,
"upload_time": "2023-02-09T10:29:44",
"upload_time_iso_8601": "2023-02-09T10:29:44.805471Z",
"url": "https://files.pythonhosted.org/packages/a2/98/07dcf5d1459e1b92fae5073100dad2e9626570c2ac4b8342dfb45a3d64f4/ncolor-1.2.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-02-09 10:29:44",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "kevinjohncutler",
"github_project": "ncolor",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "ncolor"
}