Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- envs/kitoverlay/bin/lsm2bin +8 -0
- envs/kitoverlay/bin/tiff2fsspec +8 -0
- envs/kitoverlay/bin/tiffcomment +8 -0
- envs/kitoverlay/bin/tifffile +8 -0
- envs/kitoverlay/lazy_loader-0.5.dist-info/INSTALLER +1 -0
- envs/kitoverlay/lazy_loader-0.5.dist-info/METADATA +179 -0
- envs/kitoverlay/lazy_loader-0.5.dist-info/RECORD +9 -0
- envs/kitoverlay/lazy_loader-0.5.dist-info/REQUESTED +0 -0
- envs/kitoverlay/lazy_loader-0.5.dist-info/WHEEL +5 -0
- envs/kitoverlay/lazy_loader-0.5.dist-info/licenses/LICENSE.md +29 -0
- envs/kitoverlay/lazy_loader-0.5.dist-info/top_level.txt +1 -0
- envs/kitoverlay/skimage/color/__init__.py +5 -0
- envs/kitoverlay/skimage/color/__init__.pyi +135 -0
- envs/kitoverlay/skimage/color/__pycache__/__init__.cpython-311.pyc +0 -0
- envs/kitoverlay/skimage/color/__pycache__/adapt_rgb.cpython-311.pyc +0 -0
- envs/kitoverlay/skimage/color/__pycache__/colorconv.cpython-311.pyc +0 -0
- envs/kitoverlay/skimage/color/__pycache__/colorlabel.cpython-311.pyc +0 -0
- envs/kitoverlay/skimage/color/__pycache__/delta_e.cpython-311.pyc +0 -0
- envs/kitoverlay/skimage/color/__pycache__/rgb_colors.cpython-311.pyc +0 -0
- envs/kitoverlay/skimage/color/adapt_rgb.py +81 -0
- envs/kitoverlay/skimage/color/colorconv.py +2314 -0
- envs/kitoverlay/skimage/color/colorlabel.py +299 -0
- envs/kitoverlay/skimage/color/delta_e.py +393 -0
- envs/kitoverlay/skimage/color/rgb_colors.py +146 -0
- envs/kitoverlay/skimage/exposure/__init__.py +5 -0
- envs/kitoverlay/skimage/exposure/__init__.pyi +29 -0
- envs/kitoverlay/skimage/exposure/__pycache__/__init__.cpython-311.pyc +0 -0
- envs/kitoverlay/skimage/exposure/__pycache__/_adapthist.cpython-311.pyc +0 -0
- envs/kitoverlay/skimage/exposure/__pycache__/exposure.cpython-311.pyc +0 -0
- envs/kitoverlay/skimage/exposure/__pycache__/histogram_matching.cpython-311.pyc +0 -0
- envs/kitoverlay/skimage/exposure/_adapthist.py +317 -0
- envs/kitoverlay/skimage/exposure/exposure.py +851 -0
- envs/kitoverlay/skimage/exposure/histogram_matching.py +93 -0
- envs/kitoverlay/skimage/filters/__init__.py +5 -0
- envs/kitoverlay/skimage/filters/__init__.pyi +109 -0
- envs/kitoverlay/skimage/filters/__pycache__/__init__.cpython-311.pyc +0 -0
- envs/kitoverlay/skimage/filters/__pycache__/_fft_based.cpython-311.pyc +0 -0
- envs/kitoverlay/skimage/filters/__pycache__/_gabor.cpython-311.pyc +0 -0
- envs/kitoverlay/skimage/filters/__pycache__/_gaussian.cpython-311.pyc +0 -0
- envs/kitoverlay/skimage/filters/__pycache__/_median.cpython-311.pyc +0 -0
- envs/kitoverlay/skimage/filters/__pycache__/_rank_order.cpython-311.pyc +0 -0
- envs/kitoverlay/skimage/filters/__pycache__/_sparse.cpython-311.pyc +0 -0
- envs/kitoverlay/skimage/filters/__pycache__/_unsharp_mask.cpython-311.pyc +0 -0
- envs/kitoverlay/skimage/filters/__pycache__/_window.cpython-311.pyc +0 -0
- envs/kitoverlay/skimage/filters/__pycache__/edges.cpython-311.pyc +0 -0
- envs/kitoverlay/skimage/filters/__pycache__/lpi_filter.cpython-311.pyc +0 -0
- envs/kitoverlay/skimage/filters/__pycache__/ridges.cpython-311.pyc +0 -0
- envs/kitoverlay/skimage/filters/__pycache__/thresholding.cpython-311.pyc +0 -0
- envs/kitoverlay/skimage/filters/_fft_based.py +189 -0
- envs/kitoverlay/skimage/filters/_gabor.py +220 -0
envs/kitoverlay/bin/lsm2bin
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/isaac-sim/kit/python/bin/python3
|
| 2 |
+
# -*- coding: utf-8 -*-
|
| 3 |
+
import re
|
| 4 |
+
import sys
|
| 5 |
+
from tifffile.lsm2bin import main
|
| 6 |
+
if __name__ == '__main__':
|
| 7 |
+
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
| 8 |
+
sys.exit(main())
|
envs/kitoverlay/bin/tiff2fsspec
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/isaac-sim/kit/python/bin/python3
|
| 2 |
+
# -*- coding: utf-8 -*-
|
| 3 |
+
import re
|
| 4 |
+
import sys
|
| 5 |
+
from tifffile.tiff2fsspec import main
|
| 6 |
+
if __name__ == '__main__':
|
| 7 |
+
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
| 8 |
+
sys.exit(main())
|
envs/kitoverlay/bin/tiffcomment
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/isaac-sim/kit/python/bin/python3
|
| 2 |
+
# -*- coding: utf-8 -*-
|
| 3 |
+
import re
|
| 4 |
+
import sys
|
| 5 |
+
from tifffile.tiffcomment import main
|
| 6 |
+
if __name__ == '__main__':
|
| 7 |
+
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
| 8 |
+
sys.exit(main())
|
envs/kitoverlay/bin/tifffile
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/isaac-sim/kit/python/bin/python3
|
| 2 |
+
# -*- coding: utf-8 -*-
|
| 3 |
+
import re
|
| 4 |
+
import sys
|
| 5 |
+
from tifffile import main
|
| 6 |
+
if __name__ == '__main__':
|
| 7 |
+
sys.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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Metadata-Version: 2.4
|
| 2 |
+
Name: lazy-loader
|
| 3 |
+
Version: 0.5
|
| 4 |
+
Summary: Makes it easy to load subpackages and functions on demand.
|
| 5 |
+
Author: Scientific Python Developers
|
| 6 |
+
License-Expression: BSD-3-Clause
|
| 7 |
+
Project-URL: Home, https://scientific-python.org/specs/spec-0001/
|
| 8 |
+
Project-URL: Source, https://github.com/scientific-python/lazy-loader
|
| 9 |
+
Classifier: Development Status :: 5 - Production/Stable
|
| 10 |
+
Classifier: Programming Language :: Python :: 3
|
| 11 |
+
Classifier: Programming Language :: Python :: 3.9
|
| 12 |
+
Classifier: Programming Language :: Python :: 3.10
|
| 13 |
+
Classifier: Programming Language :: Python :: 3.11
|
| 14 |
+
Classifier: Programming Language :: Python :: 3.12
|
| 15 |
+
Classifier: Programming Language :: Python :: 3.13
|
| 16 |
+
Classifier: Programming Language :: Python :: 3.14
|
| 17 |
+
Requires-Python: >=3.9
|
| 18 |
+
Description-Content-Type: text/markdown
|
| 19 |
+
License-File: LICENSE.md
|
| 20 |
+
Requires-Dist: packaging
|
| 21 |
+
Provides-Extra: test
|
| 22 |
+
Requires-Dist: pytest>=8.0; extra == "test"
|
| 23 |
+
Requires-Dist: pytest-cov>=5.0; extra == "test"
|
| 24 |
+
Requires-Dist: coverage[toml]>=7.2; extra == "test"
|
| 25 |
+
Provides-Extra: lint
|
| 26 |
+
Requires-Dist: pre-commit==4.3.0; extra == "lint"
|
| 27 |
+
Provides-Extra: dev
|
| 28 |
+
Requires-Dist: changelist==0.5; extra == "dev"
|
| 29 |
+
Requires-Dist: spin==0.15; extra == "dev"
|
| 30 |
+
Dynamic: license-file
|
| 31 |
+
|
| 32 |
+
[](https://pypi.org/project/lazy-loader/)
|
| 33 |
+
[](https://github.com/scientific-python/lazy-loader/actions?query=workflow%3A%22test%22)
|
| 34 |
+
[](https://app.codecov.io/gh/scientific-python/lazy-loader/branch/main)
|
| 35 |
+
|
| 36 |
+
`lazy-loader` makes it easy to load subpackages and functions on demand.
|
| 37 |
+
|
| 38 |
+
## Motivation
|
| 39 |
+
|
| 40 |
+
1. Allow subpackages to be made visible to users without incurring import costs.
|
| 41 |
+
2. Allow external libraries to be imported only when used, improving import times.
|
| 42 |
+
|
| 43 |
+
For a more detailed discussion, see [the SPEC](https://scientific-python.org/specs/spec-0001/).
|
| 44 |
+
|
| 45 |
+
## Installation
|
| 46 |
+
|
| 47 |
+
```
|
| 48 |
+
pip install -U lazy-loader
|
| 49 |
+
```
|
| 50 |
+
|
| 51 |
+
We recommend using `lazy-loader` with Python >= 3.11.
|
| 52 |
+
If using Python 3.11, please upgrade to 3.11.9 or later.
|
| 53 |
+
If using Python 3.12, please upgrade to 3.12.3 or later.
|
| 54 |
+
These versions [avoid](https://github.com/python/cpython/pull/114781) a [known race condition](https://github.com/python/cpython/issues/114763).
|
| 55 |
+
|
| 56 |
+
## Usage
|
| 57 |
+
|
| 58 |
+
### Lazily load subpackages
|
| 59 |
+
|
| 60 |
+
Consider the `__init__.py` from [scikit-image](https://scikit-image.org):
|
| 61 |
+
|
| 62 |
+
```python
|
| 63 |
+
subpackages = [
|
| 64 |
+
...,
|
| 65 |
+
'filters',
|
| 66 |
+
...
|
| 67 |
+
]
|
| 68 |
+
|
| 69 |
+
import lazy_loader as lazy
|
| 70 |
+
__getattr__, __dir__, _ = lazy.attach(__name__, subpackages)
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
+
You can now do:
|
| 74 |
+
|
| 75 |
+
```python
|
| 76 |
+
import skimage as ski
|
| 77 |
+
ski.filters.gaussian(...)
|
| 78 |
+
```
|
| 79 |
+
|
| 80 |
+
The `filters` subpackages will only be loaded once accessed.
|
| 81 |
+
|
| 82 |
+
### Lazily load subpackages and functions
|
| 83 |
+
|
| 84 |
+
Consider `skimage/filters/__init__.py`:
|
| 85 |
+
|
| 86 |
+
```python
|
| 87 |
+
from ..util import lazy
|
| 88 |
+
|
| 89 |
+
__getattr__, __dir__, __all__ = lazy.attach(
|
| 90 |
+
__name__,
|
| 91 |
+
submodules=['rank'],
|
| 92 |
+
submod_attrs={
|
| 93 |
+
'_gaussian': ['gaussian', 'difference_of_gaussians'],
|
| 94 |
+
'edges': ['sobel', 'scharr', 'prewitt', 'roberts',
|
| 95 |
+
'laplace', 'farid']
|
| 96 |
+
}
|
| 97 |
+
)
|
| 98 |
+
```
|
| 99 |
+
|
| 100 |
+
The above is equivalent to:
|
| 101 |
+
|
| 102 |
+
```python
|
| 103 |
+
from . import rank
|
| 104 |
+
from ._gaussian import gaussian, difference_of_gaussians
|
| 105 |
+
from .edges import (sobel, scharr, prewitt, roberts,
|
| 106 |
+
laplace, farid)
|
| 107 |
+
```
|
| 108 |
+
|
| 109 |
+
Except that all subpackages (such as `rank`) and functions (such as `sobel`) are loaded upon access.
|
| 110 |
+
|
| 111 |
+
### Type checkers
|
| 112 |
+
|
| 113 |
+
Static type checkers and IDEs cannot infer type information from
|
| 114 |
+
lazily loaded imports. As a workaround you can load [type
|
| 115 |
+
stubs](https://mypy.readthedocs.io/en/stable/stubs.html) (`.pyi`
|
| 116 |
+
files) with `lazy.attach_stub`:
|
| 117 |
+
|
| 118 |
+
```python
|
| 119 |
+
import lazy_loader as lazy
|
| 120 |
+
__getattr__, __dir__, _ = lazy.attach_stub(__name__, "subpackages.pyi")
|
| 121 |
+
```
|
| 122 |
+
|
| 123 |
+
Note that, since imports are now defined in `.pyi` files, those
|
| 124 |
+
are not only necessary for type checking but also at runtime.
|
| 125 |
+
|
| 126 |
+
The SPEC [describes this workaround in more
|
| 127 |
+
detail](https://scientific-python.org/specs/spec-0001/#type-checkers).
|
| 128 |
+
|
| 129 |
+
### Early failure
|
| 130 |
+
|
| 131 |
+
With lazy loading, missing imports no longer fail upon loading the
|
| 132 |
+
library. During development and testing, you can set the `EAGER_IMPORT`
|
| 133 |
+
environment variable to "1" or "true" to disable lazy loading ("0" or "" re-enables lazy loading).
|
| 134 |
+
|
| 135 |
+
### External libraries
|
| 136 |
+
|
| 137 |
+
The `lazy.attach` function discussed above is used to set up package
|
| 138 |
+
internal imports.
|
| 139 |
+
|
| 140 |
+
Use `lazy.load` to lazily import external libraries:
|
| 141 |
+
|
| 142 |
+
```python
|
| 143 |
+
sp = lazy.load('scipy') # `sp` will only be loaded when accessed
|
| 144 |
+
sp.linalg.norm(...)
|
| 145 |
+
```
|
| 146 |
+
|
| 147 |
+
_Note that lazily importing *sub*packages,
|
| 148 |
+
i.e. `load('scipy.linalg')` will cause the package containing the
|
| 149 |
+
subpackage to be imported immediately; thus, this usage is
|
| 150 |
+
discouraged._
|
| 151 |
+
|
| 152 |
+
You can ask `lazy.load` to raise import errors as soon as it is called:
|
| 153 |
+
|
| 154 |
+
```python
|
| 155 |
+
linalg = lazy.load('scipy.linalg', error_on_import=True)
|
| 156 |
+
```
|
| 157 |
+
|
| 158 |
+
#### Optional requirements
|
| 159 |
+
|
| 160 |
+
One use for lazy loading is for loading optional dependencies, with
|
| 161 |
+
`ImportErrors` only arising when optional functionality is accessed. If optional
|
| 162 |
+
functionality depends on a specific version, a version requirement can
|
| 163 |
+
be set:
|
| 164 |
+
|
| 165 |
+
```python
|
| 166 |
+
np = lazy.load("numpy", require="numpy >=1.24")
|
| 167 |
+
```
|
| 168 |
+
|
| 169 |
+
In this case, if `numpy` is installed, but the version is less than 1.24,
|
| 170 |
+
the `np` module returned will raise an error on attribute access. Using
|
| 171 |
+
this feature is not all-or-nothing: One module may rely on one version of
|
| 172 |
+
numpy, while another module may not set any requirement.
|
| 173 |
+
|
| 174 |
+
_Note that the requirement must use the package [distribution name][] instead
|
| 175 |
+
of the module [import name][]. For example, the `pyyaml` distribution provides
|
| 176 |
+
the `yaml` module for import._
|
| 177 |
+
|
| 178 |
+
[distribution name]: https://packaging.python.org/en/latest/glossary/#term-Distribution-Package
|
| 179 |
+
[import name]: https://packaging.python.org/en/latest/glossary/#term-Import-Package
|
envs/kitoverlay/lazy_loader-0.5.dist-info/RECORD
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
lazy_loader-0.5.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
|
| 2 |
+
lazy_loader-0.5.dist-info/METADATA,sha256=q2XIYmSTfYrAgNX8iV25J_8vlkoHy5L5F2jbFV_Aw3w,5919
|
| 3 |
+
lazy_loader-0.5.dist-info/RECORD,,
|
| 4 |
+
lazy_loader-0.5.dist-info/REQUESTED,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
| 5 |
+
lazy_loader-0.5.dist-info/WHEEL,sha256=YCfwYGOYMi5Jhw2fU4yNgwErybb2IX5PEwBKV4ZbdBo,91
|
| 6 |
+
lazy_loader-0.5.dist-info/licenses/LICENSE.md,sha256=eXtpN6T5doNu-7uzrjM9eGbdw-s8sVKXKyvjSNNSk3I,1539
|
| 7 |
+
lazy_loader-0.5.dist-info/top_level.txt,sha256=NYVH9nn-v-w-FAbrgorNRM8g_GoewNbzC_1tptomQTQ,12
|
| 8 |
+
lazy_loader/__init__.py,sha256=kBfn54D_53QrjNJYgsIsk6y5rDU_M3K43Mdqz4Mqzyk,10801
|
| 9 |
+
lazy_loader/__pycache__/__init__.cpython-311.pyc,,
|
envs/kitoverlay/lazy_loader-0.5.dist-info/REQUESTED
ADDED
|
File without changes
|
envs/kitoverlay/lazy_loader-0.5.dist-info/WHEEL
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Wheel-Version: 1.0
|
| 2 |
+
Generator: setuptools (82.0.0)
|
| 3 |
+
Root-Is-Purelib: true
|
| 4 |
+
Tag: py3-none-any
|
| 5 |
+
|
envs/kitoverlay/lazy_loader-0.5.dist-info/licenses/LICENSE.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
BSD 3-Clause License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2022--2023, Scientific Python project
|
| 4 |
+
All rights reserved.
|
| 5 |
+
|
| 6 |
+
Redistribution and use in source and binary forms, with or without
|
| 7 |
+
modification, are permitted provided that the following conditions are met:
|
| 8 |
+
|
| 9 |
+
1. Redistributions of source code must retain the above copyright notice, this
|
| 10 |
+
list of conditions and the following disclaimer.
|
| 11 |
+
|
| 12 |
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
| 13 |
+
this list of conditions and the following disclaimer in the documentation
|
| 14 |
+
and/or other materials provided with the distribution.
|
| 15 |
+
|
| 16 |
+
3. Neither the name of the copyright holder nor the names of its
|
| 17 |
+
contributors may be used to endorse or promote products derived from
|
| 18 |
+
this software without specific prior written permission.
|
| 19 |
+
|
| 20 |
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
| 21 |
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
| 22 |
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
| 23 |
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
| 24 |
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
| 25 |
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
| 26 |
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
| 27 |
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
| 28 |
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
| 29 |
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
envs/kitoverlay/lazy_loader-0.5.dist-info/top_level.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
lazy_loader
|
envs/kitoverlay/skimage/color/__init__.py
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Color space conversion."""
|
| 2 |
+
|
| 3 |
+
import lazy_loader as _lazy
|
| 4 |
+
|
| 5 |
+
__getattr__, __dir__, __all__ = _lazy.attach_stub(__name__, __file__)
|
envs/kitoverlay/skimage/color/__init__.pyi
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Explicitly setting `__all__` is necessary for type inference engines
|
| 2 |
+
# to know which symbols are exported. See
|
| 3 |
+
# https://peps.python.org/pep-0484/#stub-files
|
| 4 |
+
|
| 5 |
+
__all__ = [
|
| 6 |
+
'convert_colorspace',
|
| 7 |
+
'xyz_tristimulus_values',
|
| 8 |
+
'rgba2rgb',
|
| 9 |
+
'rgb2hsv',
|
| 10 |
+
'hsv2rgb',
|
| 11 |
+
'rgb2xyz',
|
| 12 |
+
'xyz2rgb',
|
| 13 |
+
'rgb2rgbcie',
|
| 14 |
+
'rgbcie2rgb',
|
| 15 |
+
'rgb2gray',
|
| 16 |
+
'gray2rgb',
|
| 17 |
+
'gray2rgba',
|
| 18 |
+
'xyz2lab',
|
| 19 |
+
'lab2xyz',
|
| 20 |
+
'lab2rgb',
|
| 21 |
+
'rgb2lab',
|
| 22 |
+
'rgb2hed',
|
| 23 |
+
'hed2rgb',
|
| 24 |
+
'lab2lch',
|
| 25 |
+
'lch2lab',
|
| 26 |
+
'rgb2yuv',
|
| 27 |
+
'yuv2rgb',
|
| 28 |
+
'rgb2yiq',
|
| 29 |
+
'yiq2rgb',
|
| 30 |
+
'rgb2ypbpr',
|
| 31 |
+
'ypbpr2rgb',
|
| 32 |
+
'rgb2ycbcr',
|
| 33 |
+
'ycbcr2rgb',
|
| 34 |
+
'rgb2ydbdr',
|
| 35 |
+
'ydbdr2rgb',
|
| 36 |
+
'separate_stains',
|
| 37 |
+
'combine_stains',
|
| 38 |
+
'rgb_from_hed',
|
| 39 |
+
'hed_from_rgb',
|
| 40 |
+
'rgb_from_hdx',
|
| 41 |
+
'hdx_from_rgb',
|
| 42 |
+
'rgb_from_fgx',
|
| 43 |
+
'fgx_from_rgb',
|
| 44 |
+
'rgb_from_bex',
|
| 45 |
+
'bex_from_rgb',
|
| 46 |
+
'rgb_from_rbd',
|
| 47 |
+
'rbd_from_rgb',
|
| 48 |
+
'rgb_from_gdx',
|
| 49 |
+
'gdx_from_rgb',
|
| 50 |
+
'rgb_from_hax',
|
| 51 |
+
'hax_from_rgb',
|
| 52 |
+
'rgb_from_bro',
|
| 53 |
+
'bro_from_rgb',
|
| 54 |
+
'rgb_from_bpx',
|
| 55 |
+
'bpx_from_rgb',
|
| 56 |
+
'rgb_from_ahx',
|
| 57 |
+
'ahx_from_rgb',
|
| 58 |
+
'rgb_from_hpx',
|
| 59 |
+
'hpx_from_rgb',
|
| 60 |
+
'color_dict',
|
| 61 |
+
'label2rgb',
|
| 62 |
+
'deltaE_cie76',
|
| 63 |
+
'deltaE_ciede94',
|
| 64 |
+
'deltaE_ciede2000',
|
| 65 |
+
'deltaE_cmc',
|
| 66 |
+
]
|
| 67 |
+
|
| 68 |
+
from .colorconv import (
|
| 69 |
+
convert_colorspace,
|
| 70 |
+
xyz_tristimulus_values,
|
| 71 |
+
rgba2rgb,
|
| 72 |
+
rgb2hsv,
|
| 73 |
+
hsv2rgb,
|
| 74 |
+
rgb2xyz,
|
| 75 |
+
xyz2rgb,
|
| 76 |
+
rgb2rgbcie,
|
| 77 |
+
rgbcie2rgb,
|
| 78 |
+
rgb2gray,
|
| 79 |
+
gray2rgb,
|
| 80 |
+
gray2rgba,
|
| 81 |
+
xyz2lab,
|
| 82 |
+
lab2xyz,
|
| 83 |
+
lab2rgb,
|
| 84 |
+
rgb2lab,
|
| 85 |
+
xyz2luv,
|
| 86 |
+
luv2xyz,
|
| 87 |
+
luv2rgb,
|
| 88 |
+
rgb2luv,
|
| 89 |
+
rgb2hed,
|
| 90 |
+
hed2rgb,
|
| 91 |
+
lab2lch,
|
| 92 |
+
lch2lab,
|
| 93 |
+
rgb2yuv,
|
| 94 |
+
yuv2rgb,
|
| 95 |
+
rgb2yiq,
|
| 96 |
+
yiq2rgb,
|
| 97 |
+
rgb2ypbpr,
|
| 98 |
+
ypbpr2rgb,
|
| 99 |
+
rgb2ycbcr,
|
| 100 |
+
ycbcr2rgb,
|
| 101 |
+
rgb2ydbdr,
|
| 102 |
+
ydbdr2rgb,
|
| 103 |
+
separate_stains,
|
| 104 |
+
combine_stains,
|
| 105 |
+
rgb_from_hed,
|
| 106 |
+
hed_from_rgb,
|
| 107 |
+
rgb_from_hdx,
|
| 108 |
+
hdx_from_rgb,
|
| 109 |
+
rgb_from_fgx,
|
| 110 |
+
fgx_from_rgb,
|
| 111 |
+
rgb_from_bex,
|
| 112 |
+
bex_from_rgb,
|
| 113 |
+
rgb_from_rbd,
|
| 114 |
+
rbd_from_rgb,
|
| 115 |
+
rgb_from_gdx,
|
| 116 |
+
gdx_from_rgb,
|
| 117 |
+
rgb_from_hax,
|
| 118 |
+
hax_from_rgb,
|
| 119 |
+
rgb_from_bro,
|
| 120 |
+
bro_from_rgb,
|
| 121 |
+
rgb_from_bpx,
|
| 122 |
+
bpx_from_rgb,
|
| 123 |
+
rgb_from_ahx,
|
| 124 |
+
ahx_from_rgb,
|
| 125 |
+
rgb_from_hpx,
|
| 126 |
+
hpx_from_rgb,
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
from .colorlabel import color_dict, label2rgb
|
| 130 |
+
from .delta_e import (
|
| 131 |
+
deltaE_cie76,
|
| 132 |
+
deltaE_ciede94,
|
| 133 |
+
deltaE_ciede2000,
|
| 134 |
+
deltaE_cmc,
|
| 135 |
+
)
|
envs/kitoverlay/skimage/color/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (383 Bytes). View file
|
|
|
envs/kitoverlay/skimage/color/__pycache__/adapt_rgb.cpython-311.pyc
ADDED
|
Binary file (3.95 kB). View file
|
|
|
envs/kitoverlay/skimage/color/__pycache__/colorconv.cpython-311.pyc
ADDED
|
Binary file (78 kB). View file
|
|
|
envs/kitoverlay/skimage/color/__pycache__/colorlabel.cpython-311.pyc
ADDED
|
Binary file (12.4 kB). View file
|
|
|
envs/kitoverlay/skimage/color/__pycache__/delta_e.cpython-311.pyc
ADDED
|
Binary file (17.2 kB). View file
|
|
|
envs/kitoverlay/skimage/color/__pycache__/rgb_colors.cpython-311.pyc
ADDED
|
Binary file (5.91 kB). View file
|
|
|
envs/kitoverlay/skimage/color/adapt_rgb.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import functools
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
from .. import color
|
| 6 |
+
from ..util.dtype import _convert
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
__all__ = ['adapt_rgb', 'hsv_value', 'each_channel']
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def is_rgb_like(image, channel_axis=-1):
|
| 13 |
+
"""Return True if the image *looks* like it's RGB.
|
| 14 |
+
|
| 15 |
+
This function should not be public because it is only intended to be used
|
| 16 |
+
for functions that don't accept volumes as input, since checking an image's
|
| 17 |
+
shape is fragile.
|
| 18 |
+
"""
|
| 19 |
+
return (image.ndim == 3) and (image.shape[channel_axis] in (3, 4))
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def adapt_rgb(apply_to_rgb):
|
| 23 |
+
"""Return decorator that adapts to RGB images to a gray-scale filter.
|
| 24 |
+
|
| 25 |
+
This function is only intended to be used for functions that don't accept
|
| 26 |
+
volumes as input, since checking an image's shape is fragile.
|
| 27 |
+
|
| 28 |
+
Parameters
|
| 29 |
+
----------
|
| 30 |
+
apply_to_rgb : function
|
| 31 |
+
Function that returns a filtered image from an image-filter and RGB
|
| 32 |
+
image. This will only be called if the image is RGB-like.
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
def decorator(image_filter):
|
| 36 |
+
@functools.wraps(image_filter)
|
| 37 |
+
def image_filter_adapted(image, *args, **kwargs):
|
| 38 |
+
if is_rgb_like(image):
|
| 39 |
+
return apply_to_rgb(image_filter, image, *args, **kwargs)
|
| 40 |
+
else:
|
| 41 |
+
return image_filter(image, *args, **kwargs)
|
| 42 |
+
|
| 43 |
+
return image_filter_adapted
|
| 44 |
+
|
| 45 |
+
return decorator
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def hsv_value(image_filter, image, *args, **kwargs):
|
| 49 |
+
"""Return color image by applying `image_filter` on HSV-value of `image`.
|
| 50 |
+
|
| 51 |
+
Note that this function is intended for use with `adapt_rgb`.
|
| 52 |
+
|
| 53 |
+
Parameters
|
| 54 |
+
----------
|
| 55 |
+
image_filter : function
|
| 56 |
+
Function that filters a gray-scale image.
|
| 57 |
+
image : array
|
| 58 |
+
Input image. Note that RGBA images are treated as RGB.
|
| 59 |
+
"""
|
| 60 |
+
# Slice the first three channels so that we remove any alpha channels.
|
| 61 |
+
hsv = color.rgb2hsv(image[:, :, :3])
|
| 62 |
+
value = hsv[:, :, 2].copy()
|
| 63 |
+
value = image_filter(value, *args, **kwargs)
|
| 64 |
+
hsv[:, :, 2] = _convert(value, hsv.dtype)
|
| 65 |
+
return color.hsv2rgb(hsv)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def each_channel(image_filter, image, *args, **kwargs):
|
| 69 |
+
"""Return color image by applying `image_filter` on channels of `image`.
|
| 70 |
+
|
| 71 |
+
Note that this function is intended for use with `adapt_rgb`.
|
| 72 |
+
|
| 73 |
+
Parameters
|
| 74 |
+
----------
|
| 75 |
+
image_filter : function
|
| 76 |
+
Function that filters a gray-scale image.
|
| 77 |
+
image : array
|
| 78 |
+
Input image.
|
| 79 |
+
"""
|
| 80 |
+
c_new = [image_filter(c, *args, **kwargs) for c in np.moveaxis(image, -1, 0)]
|
| 81 |
+
return np.stack(c_new, axis=-1)
|
envs/kitoverlay/skimage/color/colorconv.py
ADDED
|
@@ -0,0 +1,2314 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Functions for converting between color spaces.
|
| 2 |
+
|
| 3 |
+
The "central" color space in this module is RGB, more specifically the linear
|
| 4 |
+
sRGB color space using D65 as a white-point [1]_. This represents a
|
| 5 |
+
standard monitor (w/o gamma correction). For a good FAQ on color spaces see
|
| 6 |
+
[2]_.
|
| 7 |
+
|
| 8 |
+
The API consists of functions to convert to and from RGB as defined above, as
|
| 9 |
+
well as a generic function to convert to and from any supported color space
|
| 10 |
+
(which is done through RGB in most cases).
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
Supported color spaces
|
| 14 |
+
----------------------
|
| 15 |
+
* RGB : Red Green Blue.
|
| 16 |
+
Here the sRGB standard [1]_.
|
| 17 |
+
* HSV : Hue, Saturation, Value.
|
| 18 |
+
Uniquely defined when related to sRGB [3]_.
|
| 19 |
+
* RGB CIE : Red Green Blue.
|
| 20 |
+
The original RGB CIE standard from 1931 [4]_. Primary colors are 700 nm
|
| 21 |
+
(red), 546.1 nm (blue) and 435.8 nm (green).
|
| 22 |
+
* XYZ CIE : XYZ
|
| 23 |
+
Derived from the RGB CIE color space. Chosen such that
|
| 24 |
+
``x == y == z == 1/3`` at the whitepoint, and all color matching
|
| 25 |
+
functions are greater than zero everywhere.
|
| 26 |
+
* LAB CIE : Lightness, a, b
|
| 27 |
+
Colorspace derived from XYZ CIE that is intended to be more
|
| 28 |
+
perceptually uniform
|
| 29 |
+
* LUV CIE : Lightness, u, v
|
| 30 |
+
Colorspace derived from XYZ CIE that is intended to be more
|
| 31 |
+
perceptually uniform
|
| 32 |
+
* LCH CIE : Lightness, Chroma, Hue
|
| 33 |
+
Defined in terms of LAB CIE. C and H are the polar representation of
|
| 34 |
+
a and b. The polar angle C is defined to be on ``(0, 2*pi)``
|
| 35 |
+
|
| 36 |
+
:author: Nicolas Pinto (rgb2hsv)
|
| 37 |
+
:author: Ralf Gommers (hsv2rgb)
|
| 38 |
+
:author: Travis Oliphant (XYZ and RGB CIE functions)
|
| 39 |
+
:author: Matt Terry (lab2lch)
|
| 40 |
+
:author: Alex Izvorski (yuv2rgb, rgb2yuv and related)
|
| 41 |
+
|
| 42 |
+
:license: modified BSD
|
| 43 |
+
|
| 44 |
+
References
|
| 45 |
+
----------
|
| 46 |
+
.. [1] Official specification of sRGB, IEC 61966-2-1:1999.
|
| 47 |
+
.. [2] http://www.poynton.com/ColorFAQ.html
|
| 48 |
+
.. [3] https://en.wikipedia.org/wiki/HSL_and_HSV
|
| 49 |
+
.. [4] https://en.wikipedia.org/wiki/CIE_1931_color_space
|
| 50 |
+
"""
|
| 51 |
+
|
| 52 |
+
from warnings import warn
|
| 53 |
+
|
| 54 |
+
import numpy as np
|
| 55 |
+
from scipy import linalg
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
from .._shared.utils import (
|
| 59 |
+
_supported_float_type,
|
| 60 |
+
channel_as_last_axis,
|
| 61 |
+
identity,
|
| 62 |
+
reshape_nd,
|
| 63 |
+
slice_at_axis,
|
| 64 |
+
)
|
| 65 |
+
from ..util import dtype, dtype_limits
|
| 66 |
+
|
| 67 |
+
# TODO: when minimum numpy dependency is 1.25 use:
|
| 68 |
+
# np..exceptions.AxisError instead of AxisError
|
| 69 |
+
# and remove this try-except
|
| 70 |
+
try:
|
| 71 |
+
from numpy import AxisError
|
| 72 |
+
except ImportError:
|
| 73 |
+
from numpy.exceptions import AxisError
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def convert_colorspace(arr, fromspace, tospace, *, channel_axis=-1):
|
| 77 |
+
"""Convert an image array to a new color space.
|
| 78 |
+
|
| 79 |
+
Valid color spaces are:
|
| 80 |
+
'RGB', 'HSV', 'RGB CIE', 'XYZ', 'YUV', 'YIQ', 'YPbPr', 'YCbCr', 'YDbDr'
|
| 81 |
+
|
| 82 |
+
Parameters
|
| 83 |
+
----------
|
| 84 |
+
arr : (..., C=3, ...) array_like
|
| 85 |
+
The image to convert. By default, the final dimension denotes
|
| 86 |
+
channels.
|
| 87 |
+
fromspace : str
|
| 88 |
+
The color space to convert from. Can be specified in lower case.
|
| 89 |
+
tospace : str
|
| 90 |
+
The color space to convert to. Can be specified in lower case.
|
| 91 |
+
channel_axis : int, optional
|
| 92 |
+
This parameter indicates which axis of the array corresponds to
|
| 93 |
+
channels.
|
| 94 |
+
|
| 95 |
+
.. versionadded:: 0.19
|
| 96 |
+
``channel_axis`` was added in 0.19.
|
| 97 |
+
|
| 98 |
+
Returns
|
| 99 |
+
-------
|
| 100 |
+
out : (..., C=3, ...) ndarray
|
| 101 |
+
The converted image. Same dimensions as input.
|
| 102 |
+
|
| 103 |
+
Raises
|
| 104 |
+
------
|
| 105 |
+
ValueError
|
| 106 |
+
If fromspace is not a valid color space
|
| 107 |
+
ValueError
|
| 108 |
+
If tospace is not a valid color space
|
| 109 |
+
|
| 110 |
+
Notes
|
| 111 |
+
-----
|
| 112 |
+
Conversion is performed through the "central" RGB color space,
|
| 113 |
+
i.e. conversion from XYZ to HSV is implemented as ``XYZ -> RGB -> HSV``
|
| 114 |
+
instead of directly.
|
| 115 |
+
|
| 116 |
+
Examples
|
| 117 |
+
--------
|
| 118 |
+
>>> from skimage import data
|
| 119 |
+
>>> img = data.astronaut()
|
| 120 |
+
>>> img_hsv = convert_colorspace(img, 'RGB', 'HSV')
|
| 121 |
+
"""
|
| 122 |
+
fromdict = {
|
| 123 |
+
'rgb': identity,
|
| 124 |
+
'hsv': hsv2rgb,
|
| 125 |
+
'rgb cie': rgbcie2rgb,
|
| 126 |
+
'xyz': xyz2rgb,
|
| 127 |
+
'yuv': yuv2rgb,
|
| 128 |
+
'yiq': yiq2rgb,
|
| 129 |
+
'ypbpr': ypbpr2rgb,
|
| 130 |
+
'ycbcr': ycbcr2rgb,
|
| 131 |
+
'ydbdr': ydbdr2rgb,
|
| 132 |
+
}
|
| 133 |
+
todict = {
|
| 134 |
+
'rgb': identity,
|
| 135 |
+
'hsv': rgb2hsv,
|
| 136 |
+
'rgb cie': rgb2rgbcie,
|
| 137 |
+
'xyz': rgb2xyz,
|
| 138 |
+
'yuv': rgb2yuv,
|
| 139 |
+
'yiq': rgb2yiq,
|
| 140 |
+
'ypbpr': rgb2ypbpr,
|
| 141 |
+
'ycbcr': rgb2ycbcr,
|
| 142 |
+
'ydbdr': rgb2ydbdr,
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
fromspace = fromspace.lower()
|
| 146 |
+
tospace = tospace.lower()
|
| 147 |
+
if fromspace not in fromdict:
|
| 148 |
+
msg = f'`fromspace` has to be one of {fromdict.keys()}'
|
| 149 |
+
raise ValueError(msg)
|
| 150 |
+
if tospace not in todict:
|
| 151 |
+
msg = f'`tospace` has to be one of {todict.keys()}'
|
| 152 |
+
raise ValueError(msg)
|
| 153 |
+
|
| 154 |
+
return todict[tospace](
|
| 155 |
+
fromdict[fromspace](arr, channel_axis=channel_axis), channel_axis=channel_axis
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
def _prepare_colorarray(arr, force_copy=False, *, channel_axis=-1):
|
| 160 |
+
"""Check the shape of the array and convert it to
|
| 161 |
+
floating point representation.
|
| 162 |
+
"""
|
| 163 |
+
arr = np.asanyarray(arr)
|
| 164 |
+
|
| 165 |
+
if arr.shape[channel_axis] != 3:
|
| 166 |
+
msg = (
|
| 167 |
+
f'the input array must have size 3 along `channel_axis`, '
|
| 168 |
+
f'got {arr.shape}'
|
| 169 |
+
)
|
| 170 |
+
raise ValueError(msg)
|
| 171 |
+
|
| 172 |
+
float_dtype = _supported_float_type(arr.dtype)
|
| 173 |
+
if float_dtype == np.float32:
|
| 174 |
+
_func = dtype.img_as_float32
|
| 175 |
+
else:
|
| 176 |
+
_func = dtype.img_as_float64
|
| 177 |
+
return _func(arr, force_copy=force_copy)
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
def _validate_channel_axis(channel_axis, ndim):
|
| 181 |
+
if not isinstance(channel_axis, int):
|
| 182 |
+
raise TypeError("channel_axis must be an integer")
|
| 183 |
+
if channel_axis < -ndim or channel_axis >= ndim:
|
| 184 |
+
raise AxisError("channel_axis exceeds array dimensions")
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
def rgba2rgb(rgba, background=(1, 1, 1), *, channel_axis=-1):
|
| 188 |
+
"""RGBA to RGB conversion using alpha blending [1]_.
|
| 189 |
+
|
| 190 |
+
Parameters
|
| 191 |
+
----------
|
| 192 |
+
rgba : (..., C=4, ...) array_like
|
| 193 |
+
The image in RGBA format. By default, the final dimension denotes
|
| 194 |
+
channels.
|
| 195 |
+
background : array_like
|
| 196 |
+
The color of the background to blend the image with (3 floats
|
| 197 |
+
between 0 to 1 - the RGB value of the background).
|
| 198 |
+
channel_axis : int, optional
|
| 199 |
+
This parameter indicates which axis of the array corresponds to
|
| 200 |
+
channels.
|
| 201 |
+
|
| 202 |
+
.. versionadded:: 0.19
|
| 203 |
+
``channel_axis`` was added in 0.19.
|
| 204 |
+
|
| 205 |
+
Returns
|
| 206 |
+
-------
|
| 207 |
+
out : (..., C=3, ...) ndarray
|
| 208 |
+
The image in RGB format. Same dimensions as input.
|
| 209 |
+
|
| 210 |
+
Raises
|
| 211 |
+
------
|
| 212 |
+
ValueError
|
| 213 |
+
If `rgba` is not at least 2D with shape (..., 4, ...).
|
| 214 |
+
|
| 215 |
+
References
|
| 216 |
+
----------
|
| 217 |
+
.. [1] https://en.wikipedia.org/wiki/Alpha_compositing#Alpha_blending
|
| 218 |
+
|
| 219 |
+
Examples
|
| 220 |
+
--------
|
| 221 |
+
>>> from skimage import color
|
| 222 |
+
>>> from skimage import data
|
| 223 |
+
>>> img_rgba = data.logo()
|
| 224 |
+
>>> img_rgb = color.rgba2rgb(img_rgba)
|
| 225 |
+
"""
|
| 226 |
+
arr = np.asanyarray(rgba)
|
| 227 |
+
_validate_channel_axis(channel_axis, arr.ndim)
|
| 228 |
+
channel_axis = channel_axis % arr.ndim
|
| 229 |
+
|
| 230 |
+
if arr.shape[channel_axis] != 4:
|
| 231 |
+
msg = (
|
| 232 |
+
f'the input array must have size 4 along `channel_axis`, '
|
| 233 |
+
f'got {arr.shape}'
|
| 234 |
+
)
|
| 235 |
+
raise ValueError(msg)
|
| 236 |
+
|
| 237 |
+
float_dtype = _supported_float_type(arr.dtype)
|
| 238 |
+
if float_dtype == np.float32:
|
| 239 |
+
arr = dtype.img_as_float32(arr)
|
| 240 |
+
else:
|
| 241 |
+
arr = dtype.img_as_float64(arr)
|
| 242 |
+
|
| 243 |
+
background = np.ravel(background).astype(arr.dtype)
|
| 244 |
+
if len(background) != 3:
|
| 245 |
+
raise ValueError(
|
| 246 |
+
'background must be an array-like containing 3 RGB '
|
| 247 |
+
f'values. Got {len(background)} items'
|
| 248 |
+
)
|
| 249 |
+
if np.any(background < 0) or np.any(background > 1):
|
| 250 |
+
raise ValueError('background RGB values must be floats between ' '0 and 1.')
|
| 251 |
+
# reshape background for broadcasting along non-channel axes
|
| 252 |
+
background = reshape_nd(background, arr.ndim, channel_axis)
|
| 253 |
+
|
| 254 |
+
alpha = arr[slice_at_axis(slice(3, 4), axis=channel_axis)]
|
| 255 |
+
channels = arr[slice_at_axis(slice(3), axis=channel_axis)]
|
| 256 |
+
out = np.clip((1 - alpha) * background + alpha * channels, a_min=0, a_max=1)
|
| 257 |
+
return out
|
| 258 |
+
|
| 259 |
+
|
| 260 |
+
@channel_as_last_axis()
|
| 261 |
+
def rgb2hsv(rgb, *, channel_axis=-1):
|
| 262 |
+
"""RGB to HSV color space conversion.
|
| 263 |
+
|
| 264 |
+
Parameters
|
| 265 |
+
----------
|
| 266 |
+
rgb : (..., C=3, ...) array_like
|
| 267 |
+
The image in RGB format. By default, the final dimension denotes
|
| 268 |
+
channels.
|
| 269 |
+
channel_axis : int, optional
|
| 270 |
+
This parameter indicates which axis of the array corresponds to
|
| 271 |
+
channels.
|
| 272 |
+
|
| 273 |
+
.. versionadded:: 0.19
|
| 274 |
+
``channel_axis`` was added in 0.19.
|
| 275 |
+
|
| 276 |
+
Returns
|
| 277 |
+
-------
|
| 278 |
+
out : (..., C=3, ...) ndarray
|
| 279 |
+
The image in HSV format. Same dimensions as input.
|
| 280 |
+
|
| 281 |
+
Raises
|
| 282 |
+
------
|
| 283 |
+
ValueError
|
| 284 |
+
If `rgb` is not at least 2-D with shape (..., C=3, ...).
|
| 285 |
+
|
| 286 |
+
Notes
|
| 287 |
+
-----
|
| 288 |
+
Conversion between RGB and HSV color spaces results in some loss of
|
| 289 |
+
precision, due to integer arithmetic and rounding [1]_.
|
| 290 |
+
|
| 291 |
+
References
|
| 292 |
+
----------
|
| 293 |
+
.. [1] https://en.wikipedia.org/wiki/HSL_and_HSV
|
| 294 |
+
|
| 295 |
+
Examples
|
| 296 |
+
--------
|
| 297 |
+
>>> from skimage import color
|
| 298 |
+
>>> from skimage import data
|
| 299 |
+
>>> img = data.astronaut()
|
| 300 |
+
>>> img_hsv = color.rgb2hsv(img)
|
| 301 |
+
"""
|
| 302 |
+
input_is_one_pixel = rgb.ndim == 1
|
| 303 |
+
if input_is_one_pixel:
|
| 304 |
+
rgb = rgb[np.newaxis, ...]
|
| 305 |
+
|
| 306 |
+
arr = _prepare_colorarray(rgb, channel_axis=-1)
|
| 307 |
+
out = np.empty_like(arr)
|
| 308 |
+
|
| 309 |
+
# -- V channel
|
| 310 |
+
out_v = arr.max(-1)
|
| 311 |
+
|
| 312 |
+
# -- S channel
|
| 313 |
+
delta = np.ptp(arr, axis=-1)
|
| 314 |
+
# Ignore warning for zero divided by zero
|
| 315 |
+
old_settings = np.seterr(invalid='ignore')
|
| 316 |
+
out_s = delta / out_v
|
| 317 |
+
out_s[delta == 0.0] = 0.0
|
| 318 |
+
|
| 319 |
+
# -- H channel
|
| 320 |
+
# red is max
|
| 321 |
+
idx = arr[..., 0] == out_v
|
| 322 |
+
out[idx, 0] = (arr[idx, 1] - arr[idx, 2]) / delta[idx]
|
| 323 |
+
|
| 324 |
+
# green is max
|
| 325 |
+
idx = arr[..., 1] == out_v
|
| 326 |
+
out[idx, 0] = 2.0 + (arr[idx, 2] - arr[idx, 0]) / delta[idx]
|
| 327 |
+
|
| 328 |
+
# blue is max
|
| 329 |
+
idx = arr[..., 2] == out_v
|
| 330 |
+
out[idx, 0] = 4.0 + (arr[idx, 0] - arr[idx, 1]) / delta[idx]
|
| 331 |
+
out_h = (out[..., 0] / 6.0) % 1.0
|
| 332 |
+
out_h[delta == 0.0] = 0.0
|
| 333 |
+
|
| 334 |
+
np.seterr(**old_settings)
|
| 335 |
+
|
| 336 |
+
# -- output
|
| 337 |
+
out[..., 0] = out_h
|
| 338 |
+
out[..., 1] = out_s
|
| 339 |
+
out[..., 2] = out_v
|
| 340 |
+
|
| 341 |
+
# # remove NaN
|
| 342 |
+
out[np.isnan(out)] = 0
|
| 343 |
+
|
| 344 |
+
if input_is_one_pixel:
|
| 345 |
+
out = np.squeeze(out, axis=0)
|
| 346 |
+
|
| 347 |
+
return out
|
| 348 |
+
|
| 349 |
+
|
| 350 |
+
@channel_as_last_axis()
|
| 351 |
+
def hsv2rgb(hsv, *, channel_axis=-1):
|
| 352 |
+
"""HSV to RGB color space conversion.
|
| 353 |
+
|
| 354 |
+
Parameters
|
| 355 |
+
----------
|
| 356 |
+
hsv : (..., C=3, ...) array_like
|
| 357 |
+
The image in HSV format. By default, the final dimension denotes
|
| 358 |
+
channels.
|
| 359 |
+
channel_axis : int, optional
|
| 360 |
+
This parameter indicates which axis of the array corresponds to
|
| 361 |
+
channels.
|
| 362 |
+
|
| 363 |
+
.. versionadded:: 0.19
|
| 364 |
+
``channel_axis`` was added in 0.19.
|
| 365 |
+
|
| 366 |
+
Returns
|
| 367 |
+
-------
|
| 368 |
+
out : (..., C=3, ...) ndarray
|
| 369 |
+
The image in RGB format. Same dimensions as input.
|
| 370 |
+
|
| 371 |
+
Raises
|
| 372 |
+
------
|
| 373 |
+
ValueError
|
| 374 |
+
If `hsv` is not at least 2-D with shape (..., C=3, ...).
|
| 375 |
+
|
| 376 |
+
Notes
|
| 377 |
+
-----
|
| 378 |
+
Conversion between RGB and HSV color spaces results in some loss of
|
| 379 |
+
precision, due to integer arithmetic and rounding [1]_.
|
| 380 |
+
|
| 381 |
+
References
|
| 382 |
+
----------
|
| 383 |
+
.. [1] https://en.wikipedia.org/wiki/HSL_and_HSV
|
| 384 |
+
|
| 385 |
+
Examples
|
| 386 |
+
--------
|
| 387 |
+
>>> from skimage import data
|
| 388 |
+
>>> img = data.astronaut()
|
| 389 |
+
>>> img_hsv = rgb2hsv(img)
|
| 390 |
+
>>> img_rgb = hsv2rgb(img_hsv)
|
| 391 |
+
"""
|
| 392 |
+
arr = _prepare_colorarray(hsv, channel_axis=-1)
|
| 393 |
+
|
| 394 |
+
hi = np.floor(arr[..., 0] * 6)
|
| 395 |
+
f = arr[..., 0] * 6 - hi
|
| 396 |
+
p = arr[..., 2] * (1 - arr[..., 1])
|
| 397 |
+
q = arr[..., 2] * (1 - f * arr[..., 1])
|
| 398 |
+
t = arr[..., 2] * (1 - (1 - f) * arr[..., 1])
|
| 399 |
+
v = arr[..., 2]
|
| 400 |
+
|
| 401 |
+
hi = np.stack([hi, hi, hi], axis=-1).astype(np.uint8) % 6
|
| 402 |
+
out = np.choose(
|
| 403 |
+
hi,
|
| 404 |
+
np.stack(
|
| 405 |
+
[
|
| 406 |
+
np.stack((v, t, p), axis=-1),
|
| 407 |
+
np.stack((q, v, p), axis=-1),
|
| 408 |
+
np.stack((p, v, t), axis=-1),
|
| 409 |
+
np.stack((p, q, v), axis=-1),
|
| 410 |
+
np.stack((t, p, v), axis=-1),
|
| 411 |
+
np.stack((v, p, q), axis=-1),
|
| 412 |
+
]
|
| 413 |
+
),
|
| 414 |
+
)
|
| 415 |
+
|
| 416 |
+
return out
|
| 417 |
+
|
| 418 |
+
|
| 419 |
+
# ---------------------------------------------------------------
|
| 420 |
+
# Primaries for the coordinate systems
|
| 421 |
+
# ---------------------------------------------------------------
|
| 422 |
+
cie_primaries = np.array([700, 546.1, 435.8])
|
| 423 |
+
sb_primaries = np.array([1.0 / 155, 1.0 / 190, 1.0 / 225]) * 1e5
|
| 424 |
+
|
| 425 |
+
# ---------------------------------------------------------------
|
| 426 |
+
# Matrices that define conversion between different color spaces
|
| 427 |
+
# ---------------------------------------------------------------
|
| 428 |
+
|
| 429 |
+
# From sRGB specification
|
| 430 |
+
xyz_from_rgb = np.array(
|
| 431 |
+
[
|
| 432 |
+
[0.412453, 0.357580, 0.180423],
|
| 433 |
+
[0.212671, 0.715160, 0.072169],
|
| 434 |
+
[0.019334, 0.119193, 0.950227],
|
| 435 |
+
]
|
| 436 |
+
)
|
| 437 |
+
|
| 438 |
+
rgb_from_xyz = linalg.inv(xyz_from_rgb)
|
| 439 |
+
|
| 440 |
+
# From https://en.wikipedia.org/wiki/CIE_1931_color_space
|
| 441 |
+
# Note: Travis's code did not have the divide by 0.17697
|
| 442 |
+
xyz_from_rgbcie = (
|
| 443 |
+
np.array([[0.49, 0.31, 0.20], [0.17697, 0.81240, 0.01063], [0.00, 0.01, 0.99]])
|
| 444 |
+
/ 0.17697
|
| 445 |
+
)
|
| 446 |
+
|
| 447 |
+
rgbcie_from_xyz = linalg.inv(xyz_from_rgbcie)
|
| 448 |
+
|
| 449 |
+
# construct matrices to and from rgb:
|
| 450 |
+
rgbcie_from_rgb = rgbcie_from_xyz @ xyz_from_rgb
|
| 451 |
+
rgb_from_rgbcie = rgb_from_xyz @ xyz_from_rgbcie
|
| 452 |
+
|
| 453 |
+
|
| 454 |
+
gray_from_rgb = np.array([[0.2125, 0.7154, 0.0721], [0, 0, 0], [0, 0, 0]])
|
| 455 |
+
|
| 456 |
+
yuv_from_rgb = np.array(
|
| 457 |
+
[
|
| 458 |
+
[0.299, 0.587, 0.114],
|
| 459 |
+
[-0.14714119, -0.28886916, 0.43601035],
|
| 460 |
+
[0.61497538, -0.51496512, -0.10001026],
|
| 461 |
+
]
|
| 462 |
+
)
|
| 463 |
+
|
| 464 |
+
rgb_from_yuv = linalg.inv(yuv_from_rgb)
|
| 465 |
+
|
| 466 |
+
yiq_from_rgb = np.array(
|
| 467 |
+
[
|
| 468 |
+
[0.299, 0.587, 0.114],
|
| 469 |
+
[0.59590059, -0.27455667, -0.32134392],
|
| 470 |
+
[0.21153661, -0.52273617, 0.31119955],
|
| 471 |
+
]
|
| 472 |
+
)
|
| 473 |
+
|
| 474 |
+
rgb_from_yiq = linalg.inv(yiq_from_rgb)
|
| 475 |
+
|
| 476 |
+
ypbpr_from_rgb = np.array(
|
| 477 |
+
[[0.299, 0.587, 0.114], [-0.168736, -0.331264, 0.5], [0.5, -0.418688, -0.081312]]
|
| 478 |
+
)
|
| 479 |
+
|
| 480 |
+
rgb_from_ypbpr = linalg.inv(ypbpr_from_rgb)
|
| 481 |
+
|
| 482 |
+
ycbcr_from_rgb = np.array(
|
| 483 |
+
[[65.481, 128.553, 24.966], [-37.797, -74.203, 112.0], [112.0, -93.786, -18.214]]
|
| 484 |
+
)
|
| 485 |
+
|
| 486 |
+
rgb_from_ycbcr = linalg.inv(ycbcr_from_rgb)
|
| 487 |
+
|
| 488 |
+
ydbdr_from_rgb = np.array(
|
| 489 |
+
[[0.299, 0.587, 0.114], [-0.45, -0.883, 1.333], [-1.333, 1.116, 0.217]]
|
| 490 |
+
)
|
| 491 |
+
|
| 492 |
+
rgb_from_ydbdr = linalg.inv(ydbdr_from_rgb)
|
| 493 |
+
|
| 494 |
+
|
| 495 |
+
# CIE LAB constants for Observer=2A, Illuminant=D65
|
| 496 |
+
# NOTE: this is actually the XYZ values for the illuminant above.
|
| 497 |
+
lab_ref_white = np.array([0.95047, 1.0, 1.08883])
|
| 498 |
+
|
| 499 |
+
# CIE XYZ tristimulus values of the illuminants, scaled to [0, 1]. For each illuminant I
|
| 500 |
+
# we have:
|
| 501 |
+
#
|
| 502 |
+
# illuminant[I]['2'] corresponds to the CIE XYZ tristimulus values for the 2 degree
|
| 503 |
+
# field of view.
|
| 504 |
+
#
|
| 505 |
+
# illuminant[I]['10'] corresponds to the CIE XYZ tristimulus values for the 10 degree
|
| 506 |
+
# field of view.
|
| 507 |
+
#
|
| 508 |
+
# illuminant[I]['R'] corresponds to the CIE XYZ tristimulus values for R illuminants
|
| 509 |
+
# in grDevices::convertColor
|
| 510 |
+
#
|
| 511 |
+
# The CIE XYZ tristimulus values are calculated from [1], using the formula:
|
| 512 |
+
#
|
| 513 |
+
# X = x * ( Y / y )
|
| 514 |
+
# Y = Y
|
| 515 |
+
# Z = ( 1 - x - y ) * ( Y / y )
|
| 516 |
+
#
|
| 517 |
+
# where Y = 1. The only exception is the illuminant "D65" with aperture angle
|
| 518 |
+
# 2, whose coordinates are copied from 'lab_ref_white' for
|
| 519 |
+
# backward-compatibility reasons.
|
| 520 |
+
#
|
| 521 |
+
# References
|
| 522 |
+
# ----------
|
| 523 |
+
# .. [1] https://en.wikipedia.org/wiki/Standard_illuminant
|
| 524 |
+
|
| 525 |
+
_illuminants = {
|
| 526 |
+
"A": {
|
| 527 |
+
'2': (1.098466069456375, 1, 0.3558228003436005),
|
| 528 |
+
'10': (1.111420406956693, 1, 0.3519978321919493),
|
| 529 |
+
'R': (1.098466069456375, 1, 0.3558228003436005),
|
| 530 |
+
},
|
| 531 |
+
"B": {
|
| 532 |
+
'2': (0.9909274480248003, 1, 0.8531327322886154),
|
| 533 |
+
'10': (0.9917777147717607, 1, 0.8434930535866175),
|
| 534 |
+
'R': (0.9909274480248003, 1, 0.8531327322886154),
|
| 535 |
+
},
|
| 536 |
+
"C": {
|
| 537 |
+
'2': (0.980705971659919, 1, 1.1822494939271255),
|
| 538 |
+
'10': (0.9728569189782166, 1, 1.1614480488951577),
|
| 539 |
+
'R': (0.980705971659919, 1, 1.1822494939271255),
|
| 540 |
+
},
|
| 541 |
+
"D50": {
|
| 542 |
+
'2': (0.9642119944211994, 1, 0.8251882845188288),
|
| 543 |
+
'10': (0.9672062750333777, 1, 0.8142801513128616),
|
| 544 |
+
'R': (0.9639501491621826, 1, 0.8241280285499208),
|
| 545 |
+
},
|
| 546 |
+
"D55": {
|
| 547 |
+
'2': (0.956797052643698, 1, 0.9214805860173273),
|
| 548 |
+
'10': (0.9579665682254781, 1, 0.9092525159847462),
|
| 549 |
+
'R': (0.9565317453467969, 1, 0.9202554587037198),
|
| 550 |
+
},
|
| 551 |
+
"D65": {
|
| 552 |
+
'2': (0.95047, 1.0, 1.08883), # This was: `lab_ref_white`
|
| 553 |
+
'10': (0.94809667673716, 1, 1.0730513595166162),
|
| 554 |
+
'R': (0.9532057125493769, 1, 1.0853843816469158),
|
| 555 |
+
},
|
| 556 |
+
"D75": {
|
| 557 |
+
'2': (0.9497220898840717, 1, 1.226393520724154),
|
| 558 |
+
'10': (0.9441713925645873, 1, 1.2064272211720228),
|
| 559 |
+
'R': (0.9497220898840717, 1, 1.226393520724154),
|
| 560 |
+
},
|
| 561 |
+
"E": {'2': (1.0, 1.0, 1.0), '10': (1.0, 1.0, 1.0), 'R': (1.0, 1.0, 1.0)},
|
| 562 |
+
}
|
| 563 |
+
|
| 564 |
+
|
| 565 |
+
def xyz_tristimulus_values(*, illuminant, observer, dtype=float):
|
| 566 |
+
"""Get the CIE XYZ tristimulus values.
|
| 567 |
+
|
| 568 |
+
Given an illuminant and observer, this function returns the CIE XYZ tristimulus
|
| 569 |
+
values [2]_ scaled such that :math:`Y = 1`.
|
| 570 |
+
|
| 571 |
+
Parameters
|
| 572 |
+
----------
|
| 573 |
+
illuminant : {"A", "B", "C", "D50", "D55", "D65", "D75", "E"}
|
| 574 |
+
The name of the illuminant (the function is NOT case sensitive).
|
| 575 |
+
observer : {"2", "10", "R"}
|
| 576 |
+
One of: 2-degree observer, 10-degree observer, or 'R' observer as in
|
| 577 |
+
R function ``grDevices::convertColor`` [3]_.
|
| 578 |
+
dtype : dtype, optional
|
| 579 |
+
Output data type.
|
| 580 |
+
|
| 581 |
+
Returns
|
| 582 |
+
-------
|
| 583 |
+
values : array
|
| 584 |
+
Array with 3 elements :math:`X, Y, Z` containing the CIE XYZ tristimulus values
|
| 585 |
+
of the given illuminant.
|
| 586 |
+
|
| 587 |
+
Raises
|
| 588 |
+
------
|
| 589 |
+
ValueError
|
| 590 |
+
If either the illuminant or the observer angle are not supported or
|
| 591 |
+
unknown.
|
| 592 |
+
|
| 593 |
+
References
|
| 594 |
+
----------
|
| 595 |
+
.. [1] https://en.wikipedia.org/wiki/Standard_illuminant#White_points_of_standard_illuminants
|
| 596 |
+
.. [2] https://en.wikipedia.org/wiki/CIE_1931_color_space#Meaning_of_X,_Y_and_Z
|
| 597 |
+
.. [3] https://www.rdocumentation.org/packages/grDevices/versions/3.6.2/topics/convertColor
|
| 598 |
+
|
| 599 |
+
Notes
|
| 600 |
+
-----
|
| 601 |
+
The CIE XYZ tristimulus values are calculated from :math:`x, y` [1]_, using the
|
| 602 |
+
formula
|
| 603 |
+
|
| 604 |
+
.. math:: X = x / y
|
| 605 |
+
|
| 606 |
+
.. math:: Y = 1
|
| 607 |
+
|
| 608 |
+
.. math:: Z = (1 - x - y) / y
|
| 609 |
+
|
| 610 |
+
The only exception is the illuminant "D65" with aperture angle 2° for
|
| 611 |
+
backward-compatibility reasons.
|
| 612 |
+
|
| 613 |
+
Examples
|
| 614 |
+
--------
|
| 615 |
+
Get the CIE XYZ tristimulus values for a "D65" illuminant for a 10 degree field of
|
| 616 |
+
view
|
| 617 |
+
|
| 618 |
+
>>> xyz_tristimulus_values(illuminant="D65", observer="10")
|
| 619 |
+
array([0.94809668, 1. , 1.07305136])
|
| 620 |
+
"""
|
| 621 |
+
illuminant = illuminant.upper()
|
| 622 |
+
observer = observer.upper()
|
| 623 |
+
try:
|
| 624 |
+
return np.asarray(_illuminants[illuminant][observer], dtype=dtype)
|
| 625 |
+
except KeyError:
|
| 626 |
+
raise ValueError(
|
| 627 |
+
f'Unknown illuminant/observer combination '
|
| 628 |
+
f'(`{illuminant}`, `{observer}`)'
|
| 629 |
+
)
|
| 630 |
+
|
| 631 |
+
|
| 632 |
+
# Haematoxylin-Eosin-DAB colorspace
|
| 633 |
+
# From original Ruifrok's paper: A. C. Ruifrok and D. A. Johnston,
|
| 634 |
+
# "Quantification of histochemical staining by color deconvolution,"
|
| 635 |
+
# Analytical and quantitative cytology and histology / the International
|
| 636 |
+
# Academy of Cytology [and] American Society of Cytology, vol. 23, no. 4,
|
| 637 |
+
# pp. 291-9, Aug. 2001.
|
| 638 |
+
rgb_from_hed = np.array([[0.65, 0.70, 0.29], [0.07, 0.99, 0.11], [0.27, 0.57, 0.78]])
|
| 639 |
+
hed_from_rgb = linalg.inv(rgb_from_hed)
|
| 640 |
+
|
| 641 |
+
# Following matrices are adapted form the Java code written by G.Landini.
|
| 642 |
+
# The original code is available at:
|
| 643 |
+
# https://web.archive.org/web/20160624145052/http://www.mecourse.com/landinig/software/cdeconv/cdeconv.html
|
| 644 |
+
|
| 645 |
+
# Hematoxylin + DAB
|
| 646 |
+
rgb_from_hdx = np.array([[0.650, 0.704, 0.286], [0.268, 0.570, 0.776], [0.0, 0.0, 0.0]])
|
| 647 |
+
rgb_from_hdx[2, :] = np.cross(rgb_from_hdx[0, :], rgb_from_hdx[1, :])
|
| 648 |
+
hdx_from_rgb = linalg.inv(rgb_from_hdx)
|
| 649 |
+
|
| 650 |
+
# Feulgen + Light Green
|
| 651 |
+
rgb_from_fgx = np.array(
|
| 652 |
+
[
|
| 653 |
+
[0.46420921, 0.83008335, 0.30827187],
|
| 654 |
+
[0.94705542, 0.25373821, 0.19650764],
|
| 655 |
+
[0.0, 0.0, 0.0],
|
| 656 |
+
]
|
| 657 |
+
)
|
| 658 |
+
rgb_from_fgx[2, :] = np.cross(rgb_from_fgx[0, :], rgb_from_fgx[1, :])
|
| 659 |
+
fgx_from_rgb = linalg.inv(rgb_from_fgx)
|
| 660 |
+
|
| 661 |
+
# Giemsa: Methyl Blue + Eosin
|
| 662 |
+
rgb_from_bex = np.array(
|
| 663 |
+
[
|
| 664 |
+
[0.834750233, 0.513556283, 0.196330403],
|
| 665 |
+
[0.092789, 0.954111, 0.283111],
|
| 666 |
+
[0.0, 0.0, 0.0],
|
| 667 |
+
]
|
| 668 |
+
)
|
| 669 |
+
rgb_from_bex[2, :] = np.cross(rgb_from_bex[0, :], rgb_from_bex[1, :])
|
| 670 |
+
bex_from_rgb = linalg.inv(rgb_from_bex)
|
| 671 |
+
|
| 672 |
+
# FastRed + FastBlue + DAB
|
| 673 |
+
rgb_from_rbd = np.array(
|
| 674 |
+
[
|
| 675 |
+
[0.21393921, 0.85112669, 0.47794022],
|
| 676 |
+
[0.74890292, 0.60624161, 0.26731082],
|
| 677 |
+
[0.268, 0.570, 0.776],
|
| 678 |
+
]
|
| 679 |
+
)
|
| 680 |
+
rbd_from_rgb = linalg.inv(rgb_from_rbd)
|
| 681 |
+
|
| 682 |
+
# Methyl Green + DAB
|
| 683 |
+
rgb_from_gdx = np.array(
|
| 684 |
+
[[0.98003, 0.144316, 0.133146], [0.268, 0.570, 0.776], [0.0, 0.0, 0.0]]
|
| 685 |
+
)
|
| 686 |
+
rgb_from_gdx[2, :] = np.cross(rgb_from_gdx[0, :], rgb_from_gdx[1, :])
|
| 687 |
+
gdx_from_rgb = linalg.inv(rgb_from_gdx)
|
| 688 |
+
|
| 689 |
+
# Hematoxylin + AEC
|
| 690 |
+
rgb_from_hax = np.array(
|
| 691 |
+
[[0.650, 0.704, 0.286], [0.2743, 0.6796, 0.6803], [0.0, 0.0, 0.0]]
|
| 692 |
+
)
|
| 693 |
+
rgb_from_hax[2, :] = np.cross(rgb_from_hax[0, :], rgb_from_hax[1, :])
|
| 694 |
+
hax_from_rgb = linalg.inv(rgb_from_hax)
|
| 695 |
+
|
| 696 |
+
# Blue matrix Anilline Blue + Red matrix Azocarmine + Orange matrix Orange-G
|
| 697 |
+
rgb_from_bro = np.array(
|
| 698 |
+
[
|
| 699 |
+
[0.853033, 0.508733, 0.112656],
|
| 700 |
+
[0.09289875, 0.8662008, 0.49098468],
|
| 701 |
+
[0.10732849, 0.36765403, 0.9237484],
|
| 702 |
+
]
|
| 703 |
+
)
|
| 704 |
+
bro_from_rgb = linalg.inv(rgb_from_bro)
|
| 705 |
+
|
| 706 |
+
# Methyl Blue + Ponceau Fuchsin
|
| 707 |
+
rgb_from_bpx = np.array(
|
| 708 |
+
[
|
| 709 |
+
[0.7995107, 0.5913521, 0.10528667],
|
| 710 |
+
[0.09997159, 0.73738605, 0.6680326],
|
| 711 |
+
[0.0, 0.0, 0.0],
|
| 712 |
+
]
|
| 713 |
+
)
|
| 714 |
+
rgb_from_bpx[2, :] = np.cross(rgb_from_bpx[0, :], rgb_from_bpx[1, :])
|
| 715 |
+
bpx_from_rgb = linalg.inv(rgb_from_bpx)
|
| 716 |
+
|
| 717 |
+
# Alcian Blue + Hematoxylin
|
| 718 |
+
rgb_from_ahx = np.array(
|
| 719 |
+
[[0.874622, 0.457711, 0.158256], [0.552556, 0.7544, 0.353744], [0.0, 0.0, 0.0]]
|
| 720 |
+
)
|
| 721 |
+
rgb_from_ahx[2, :] = np.cross(rgb_from_ahx[0, :], rgb_from_ahx[1, :])
|
| 722 |
+
ahx_from_rgb = linalg.inv(rgb_from_ahx)
|
| 723 |
+
|
| 724 |
+
# Hematoxylin + PAS
|
| 725 |
+
rgb_from_hpx = np.array(
|
| 726 |
+
[[0.644211, 0.716556, 0.266844], [0.175411, 0.972178, 0.154589], [0.0, 0.0, 0.0]]
|
| 727 |
+
)
|
| 728 |
+
rgb_from_hpx[2, :] = np.cross(rgb_from_hpx[0, :], rgb_from_hpx[1, :])
|
| 729 |
+
hpx_from_rgb = linalg.inv(rgb_from_hpx)
|
| 730 |
+
|
| 731 |
+
# -------------------------------------------------------------
|
| 732 |
+
# The conversion functions that make use of the matrices above
|
| 733 |
+
# -------------------------------------------------------------
|
| 734 |
+
|
| 735 |
+
|
| 736 |
+
def _convert(matrix, arr):
|
| 737 |
+
"""Do the color space conversion.
|
| 738 |
+
|
| 739 |
+
Parameters
|
| 740 |
+
----------
|
| 741 |
+
matrix : array_like
|
| 742 |
+
The 3x3 matrix to use.
|
| 743 |
+
arr : (..., C=3, ...) array_like
|
| 744 |
+
The input array. By default, the final dimension denotes
|
| 745 |
+
channels.
|
| 746 |
+
|
| 747 |
+
Returns
|
| 748 |
+
-------
|
| 749 |
+
out : (..., C=3, ...) ndarray
|
| 750 |
+
The converted array. Same dimensions as input.
|
| 751 |
+
"""
|
| 752 |
+
arr = _prepare_colorarray(arr)
|
| 753 |
+
|
| 754 |
+
return arr @ matrix.T.astype(arr.dtype)
|
| 755 |
+
|
| 756 |
+
|
| 757 |
+
@channel_as_last_axis()
|
| 758 |
+
def xyz2rgb(xyz, *, channel_axis=-1):
|
| 759 |
+
"""XYZ to RGB color space conversion.
|
| 760 |
+
|
| 761 |
+
Parameters
|
| 762 |
+
----------
|
| 763 |
+
xyz : (..., C=3, ...) array_like
|
| 764 |
+
The image in XYZ format. By default, the final dimension denotes
|
| 765 |
+
channels.
|
| 766 |
+
channel_axis : int, optional
|
| 767 |
+
This parameter indicates which axis of the array corresponds to
|
| 768 |
+
channels.
|
| 769 |
+
|
| 770 |
+
.. versionadded:: 0.19
|
| 771 |
+
``channel_axis`` was added in 0.19.
|
| 772 |
+
|
| 773 |
+
Returns
|
| 774 |
+
-------
|
| 775 |
+
out : (..., C=3, ...) ndarray
|
| 776 |
+
The image in RGB format. Same dimensions as input.
|
| 777 |
+
|
| 778 |
+
Raises
|
| 779 |
+
------
|
| 780 |
+
ValueError
|
| 781 |
+
If `xyz` is not at least 2-D with shape (..., C=3, ...).
|
| 782 |
+
|
| 783 |
+
Notes
|
| 784 |
+
-----
|
| 785 |
+
The CIE XYZ color space is derived from the CIE RGB color space. Note
|
| 786 |
+
however that this function converts to sRGB.
|
| 787 |
+
|
| 788 |
+
References
|
| 789 |
+
----------
|
| 790 |
+
.. [1] https://en.wikipedia.org/wiki/CIE_1931_color_space
|
| 791 |
+
|
| 792 |
+
Examples
|
| 793 |
+
--------
|
| 794 |
+
>>> from skimage import data
|
| 795 |
+
>>> from skimage.color import rgb2xyz, xyz2rgb
|
| 796 |
+
>>> img = data.astronaut()
|
| 797 |
+
>>> img_xyz = rgb2xyz(img)
|
| 798 |
+
>>> img_rgb = xyz2rgb(img_xyz)
|
| 799 |
+
"""
|
| 800 |
+
# Follow the algorithm from http://www.easyrgb.com/index.php
|
| 801 |
+
# except we don't multiply/divide by 100 in the conversion
|
| 802 |
+
arr = _convert(rgb_from_xyz, xyz)
|
| 803 |
+
mask = arr > 0.0031308
|
| 804 |
+
arr[mask] = 1.055 * np.power(arr[mask], 1 / 2.4) - 0.055
|
| 805 |
+
arr[~mask] *= 12.92
|
| 806 |
+
np.clip(arr, 0, 1, out=arr)
|
| 807 |
+
return arr
|
| 808 |
+
|
| 809 |
+
|
| 810 |
+
@channel_as_last_axis()
|
| 811 |
+
def rgb2xyz(rgb, *, channel_axis=-1):
|
| 812 |
+
"""RGB to XYZ color space conversion.
|
| 813 |
+
|
| 814 |
+
Parameters
|
| 815 |
+
----------
|
| 816 |
+
rgb : (..., C=3, ...) array_like
|
| 817 |
+
The image in RGB format. By default, the final dimension denotes
|
| 818 |
+
channels.
|
| 819 |
+
channel_axis : int, optional
|
| 820 |
+
This parameter indicates which axis of the array corresponds to
|
| 821 |
+
channels.
|
| 822 |
+
|
| 823 |
+
.. versionadded:: 0.19
|
| 824 |
+
``channel_axis`` was added in 0.19.
|
| 825 |
+
|
| 826 |
+
Returns
|
| 827 |
+
-------
|
| 828 |
+
out : (..., C=3, ...) ndarray
|
| 829 |
+
The image in XYZ format. Same dimensions as input.
|
| 830 |
+
|
| 831 |
+
Raises
|
| 832 |
+
------
|
| 833 |
+
ValueError
|
| 834 |
+
If `rgb` is not at least 2-D with shape (..., C=3, ...).
|
| 835 |
+
|
| 836 |
+
Notes
|
| 837 |
+
-----
|
| 838 |
+
The CIE XYZ color space is derived from the CIE RGB color space. Note
|
| 839 |
+
however that this function converts from sRGB.
|
| 840 |
+
|
| 841 |
+
References
|
| 842 |
+
----------
|
| 843 |
+
.. [1] https://en.wikipedia.org/wiki/CIE_1931_color_space
|
| 844 |
+
|
| 845 |
+
Examples
|
| 846 |
+
--------
|
| 847 |
+
>>> from skimage import data
|
| 848 |
+
>>> img = data.astronaut()
|
| 849 |
+
>>> img_xyz = rgb2xyz(img)
|
| 850 |
+
"""
|
| 851 |
+
# Follow the algorithm from http://www.easyrgb.com/index.php
|
| 852 |
+
# except we don't multiply/divide by 100 in the conversion
|
| 853 |
+
arr = _prepare_colorarray(rgb, channel_axis=-1).copy()
|
| 854 |
+
mask = arr > 0.04045
|
| 855 |
+
arr[mask] = np.power((arr[mask] + 0.055) / 1.055, 2.4)
|
| 856 |
+
arr[~mask] /= 12.92
|
| 857 |
+
return arr @ xyz_from_rgb.T.astype(arr.dtype)
|
| 858 |
+
|
| 859 |
+
|
| 860 |
+
@channel_as_last_axis()
|
| 861 |
+
def rgb2rgbcie(rgb, *, channel_axis=-1):
|
| 862 |
+
"""RGB to RGB CIE color space conversion.
|
| 863 |
+
|
| 864 |
+
Parameters
|
| 865 |
+
----------
|
| 866 |
+
rgb : (..., C=3, ...) array_like
|
| 867 |
+
The image in RGB format. By default, the final dimension denotes
|
| 868 |
+
channels.
|
| 869 |
+
channel_axis : int, optional
|
| 870 |
+
This parameter indicates which axis of the array corresponds to
|
| 871 |
+
channels.
|
| 872 |
+
|
| 873 |
+
.. versionadded:: 0.19
|
| 874 |
+
``channel_axis`` was added in 0.19.
|
| 875 |
+
|
| 876 |
+
Returns
|
| 877 |
+
-------
|
| 878 |
+
out : (..., C=3, ...) ndarray
|
| 879 |
+
The image in RGB CIE format. Same dimensions as input.
|
| 880 |
+
|
| 881 |
+
Raises
|
| 882 |
+
------
|
| 883 |
+
ValueError
|
| 884 |
+
If `rgb` is not at least 2-D with shape (..., C=3, ...).
|
| 885 |
+
|
| 886 |
+
References
|
| 887 |
+
----------
|
| 888 |
+
.. [1] https://en.wikipedia.org/wiki/CIE_1931_color_space
|
| 889 |
+
|
| 890 |
+
Examples
|
| 891 |
+
--------
|
| 892 |
+
>>> from skimage import data
|
| 893 |
+
>>> from skimage.color import rgb2rgbcie
|
| 894 |
+
>>> img = data.astronaut()
|
| 895 |
+
>>> img_rgbcie = rgb2rgbcie(img)
|
| 896 |
+
"""
|
| 897 |
+
return _convert(rgbcie_from_rgb, rgb)
|
| 898 |
+
|
| 899 |
+
|
| 900 |
+
@channel_as_last_axis()
|
| 901 |
+
def rgbcie2rgb(rgbcie, *, channel_axis=-1):
|
| 902 |
+
"""RGB CIE to RGB color space conversion.
|
| 903 |
+
|
| 904 |
+
Parameters
|
| 905 |
+
----------
|
| 906 |
+
rgbcie : (..., C=3, ...) array_like
|
| 907 |
+
The image in RGB CIE format. By default, the final dimension denotes
|
| 908 |
+
channels.
|
| 909 |
+
channel_axis : int, optional
|
| 910 |
+
This parameter indicates which axis of the array corresponds to
|
| 911 |
+
channels.
|
| 912 |
+
|
| 913 |
+
.. versionadded:: 0.19
|
| 914 |
+
``channel_axis`` was added in 0.19.
|
| 915 |
+
|
| 916 |
+
Returns
|
| 917 |
+
-------
|
| 918 |
+
out : (..., C=3, ...) ndarray
|
| 919 |
+
The image in RGB format. Same dimensions as input.
|
| 920 |
+
|
| 921 |
+
Raises
|
| 922 |
+
------
|
| 923 |
+
ValueError
|
| 924 |
+
If `rgbcie` is not at least 2-D with shape (..., C=3, ...).
|
| 925 |
+
|
| 926 |
+
References
|
| 927 |
+
----------
|
| 928 |
+
.. [1] https://en.wikipedia.org/wiki/CIE_1931_color_space
|
| 929 |
+
|
| 930 |
+
Examples
|
| 931 |
+
--------
|
| 932 |
+
>>> from skimage import data
|
| 933 |
+
>>> from skimage.color import rgb2rgbcie, rgbcie2rgb
|
| 934 |
+
>>> img = data.astronaut()
|
| 935 |
+
>>> img_rgbcie = rgb2rgbcie(img)
|
| 936 |
+
>>> img_rgb = rgbcie2rgb(img_rgbcie)
|
| 937 |
+
"""
|
| 938 |
+
return _convert(rgb_from_rgbcie, rgbcie)
|
| 939 |
+
|
| 940 |
+
|
| 941 |
+
@channel_as_last_axis(multichannel_output=False)
|
| 942 |
+
def rgb2gray(rgb, *, channel_axis=-1):
|
| 943 |
+
"""Compute luminance of an RGB image.
|
| 944 |
+
|
| 945 |
+
Parameters
|
| 946 |
+
----------
|
| 947 |
+
rgb : (..., C=3, ...) array_like
|
| 948 |
+
The image in RGB format. By default, the final dimension denotes
|
| 949 |
+
channels.
|
| 950 |
+
|
| 951 |
+
Returns
|
| 952 |
+
-------
|
| 953 |
+
out : ndarray
|
| 954 |
+
The luminance image - an array which is the same size as the input
|
| 955 |
+
array, but with the channel dimension removed.
|
| 956 |
+
|
| 957 |
+
Raises
|
| 958 |
+
------
|
| 959 |
+
ValueError
|
| 960 |
+
If `rgb` is not at least 2-D with shape (..., C=3, ...).
|
| 961 |
+
|
| 962 |
+
Notes
|
| 963 |
+
-----
|
| 964 |
+
The weights used in this conversion are calibrated for contemporary
|
| 965 |
+
CRT phosphors::
|
| 966 |
+
|
| 967 |
+
Y = 0.2125 R + 0.7154 G + 0.0721 B
|
| 968 |
+
|
| 969 |
+
If there is an alpha channel present, it is ignored.
|
| 970 |
+
|
| 971 |
+
References
|
| 972 |
+
----------
|
| 973 |
+
.. [1] http://poynton.ca/PDFs/ColorFAQ.pdf
|
| 974 |
+
|
| 975 |
+
Examples
|
| 976 |
+
--------
|
| 977 |
+
>>> from skimage.color import rgb2gray
|
| 978 |
+
>>> from skimage import data
|
| 979 |
+
>>> img = data.astronaut()
|
| 980 |
+
>>> img_gray = rgb2gray(img)
|
| 981 |
+
"""
|
| 982 |
+
rgb = _prepare_colorarray(rgb)
|
| 983 |
+
coeffs = np.array([0.2125, 0.7154, 0.0721], dtype=rgb.dtype)
|
| 984 |
+
return rgb @ coeffs
|
| 985 |
+
|
| 986 |
+
|
| 987 |
+
def gray2rgba(image, alpha=None, *, channel_axis=-1):
|
| 988 |
+
"""Create a RGBA representation of a gray-level image.
|
| 989 |
+
|
| 990 |
+
Parameters
|
| 991 |
+
----------
|
| 992 |
+
image : array_like
|
| 993 |
+
Input image.
|
| 994 |
+
alpha : array_like, optional
|
| 995 |
+
Alpha channel of the output image. It may be a scalar or an
|
| 996 |
+
array that can be broadcast to ``image``. If not specified it is
|
| 997 |
+
set to the maximum limit corresponding to the ``image`` dtype.
|
| 998 |
+
channel_axis : int, optional
|
| 999 |
+
This parameter indicates which axis of the output array will correspond
|
| 1000 |
+
to channels.
|
| 1001 |
+
|
| 1002 |
+
.. versionadded:: 0.19
|
| 1003 |
+
``channel_axis`` was added in 0.19.
|
| 1004 |
+
|
| 1005 |
+
Returns
|
| 1006 |
+
-------
|
| 1007 |
+
rgba : ndarray
|
| 1008 |
+
RGBA image. A new dimension of length 4 is added to input
|
| 1009 |
+
image shape.
|
| 1010 |
+
"""
|
| 1011 |
+
arr = np.asarray(image)
|
| 1012 |
+
if alpha is None:
|
| 1013 |
+
_, alpha = dtype_limits(arr, clip_negative=False)
|
| 1014 |
+
with np.errstate(over="ignore", under="ignore"):
|
| 1015 |
+
alpha_arr = np.asarray(alpha).astype(arr.dtype)
|
| 1016 |
+
if not np.array_equal(alpha_arr, alpha):
|
| 1017 |
+
warn(
|
| 1018 |
+
f'alpha cannot be safely cast to image dtype {arr.dtype.name}', stacklevel=2
|
| 1019 |
+
)
|
| 1020 |
+
try:
|
| 1021 |
+
alpha_arr = np.broadcast_to(alpha_arr, arr.shape)
|
| 1022 |
+
except ValueError as e:
|
| 1023 |
+
raise ValueError("alpha.shape must match image.shape") from e
|
| 1024 |
+
rgba = np.stack((arr,) * 3 + (alpha_arr,), axis=channel_axis)
|
| 1025 |
+
return rgba
|
| 1026 |
+
|
| 1027 |
+
|
| 1028 |
+
def gray2rgb(image, *, channel_axis=-1):
|
| 1029 |
+
"""Create an RGB representation of a gray-level image.
|
| 1030 |
+
|
| 1031 |
+
Parameters
|
| 1032 |
+
----------
|
| 1033 |
+
image : array_like
|
| 1034 |
+
Input image.
|
| 1035 |
+
channel_axis : int, optional
|
| 1036 |
+
This parameter indicates which axis of the output array will correspond
|
| 1037 |
+
to channels.
|
| 1038 |
+
|
| 1039 |
+
Returns
|
| 1040 |
+
-------
|
| 1041 |
+
rgb : (..., C=3, ...) ndarray
|
| 1042 |
+
RGB image. A new dimension of length 3 is added to input image.
|
| 1043 |
+
|
| 1044 |
+
Notes
|
| 1045 |
+
-----
|
| 1046 |
+
If the input is a 1-dimensional image of shape ``(M,)``, the output
|
| 1047 |
+
will be shape ``(M, C=3)``.
|
| 1048 |
+
"""
|
| 1049 |
+
return np.stack(3 * (image,), axis=channel_axis)
|
| 1050 |
+
|
| 1051 |
+
|
| 1052 |
+
@channel_as_last_axis()
|
| 1053 |
+
def xyz2lab(xyz, illuminant="D65", observer="2", *, channel_axis=-1):
|
| 1054 |
+
"""XYZ to CIE-LAB color space conversion.
|
| 1055 |
+
|
| 1056 |
+
Parameters
|
| 1057 |
+
----------
|
| 1058 |
+
xyz : (..., C=3, ...) array_like
|
| 1059 |
+
The image in XYZ format. By default, the final dimension denotes
|
| 1060 |
+
channels.
|
| 1061 |
+
illuminant : {"A", "B", "C", "D50", "D55", "D65", "D75", "E"}, optional
|
| 1062 |
+
The name of the illuminant (the function is NOT case sensitive).
|
| 1063 |
+
observer : {"2", "10", "R"}, optional
|
| 1064 |
+
One of: 2-degree observer, 10-degree observer, or 'R' observer as in
|
| 1065 |
+
R function grDevices::convertColor.
|
| 1066 |
+
channel_axis : int, optional
|
| 1067 |
+
This parameter indicates which axis of the array corresponds to
|
| 1068 |
+
channels.
|
| 1069 |
+
|
| 1070 |
+
.. versionadded:: 0.19
|
| 1071 |
+
``channel_axis`` was added in 0.19.
|
| 1072 |
+
|
| 1073 |
+
Returns
|
| 1074 |
+
-------
|
| 1075 |
+
out : (..., C=3, ...) ndarray
|
| 1076 |
+
The image in CIE-LAB format. Same dimensions as input.
|
| 1077 |
+
|
| 1078 |
+
Raises
|
| 1079 |
+
------
|
| 1080 |
+
ValueError
|
| 1081 |
+
If `xyz` is not at least 2-D with shape (..., C=3, ...).
|
| 1082 |
+
ValueError
|
| 1083 |
+
If either the illuminant or the observer angle is unsupported or
|
| 1084 |
+
unknown.
|
| 1085 |
+
|
| 1086 |
+
Notes
|
| 1087 |
+
-----
|
| 1088 |
+
By default Observer="2", Illuminant="D65". CIE XYZ tristimulus values
|
| 1089 |
+
x_ref=95.047, y_ref=100., z_ref=108.883. See function
|
| 1090 |
+
:func:`~.xyz_tristimulus_values` for a list of supported illuminants.
|
| 1091 |
+
|
| 1092 |
+
References
|
| 1093 |
+
----------
|
| 1094 |
+
.. [1] http://www.easyrgb.com/en/math.php
|
| 1095 |
+
.. [2] https://en.wikipedia.org/wiki/CIELAB_color_space
|
| 1096 |
+
|
| 1097 |
+
Examples
|
| 1098 |
+
--------
|
| 1099 |
+
>>> from skimage import data
|
| 1100 |
+
>>> from skimage.color import rgb2xyz, xyz2lab
|
| 1101 |
+
>>> img = data.astronaut()
|
| 1102 |
+
>>> img_xyz = rgb2xyz(img)
|
| 1103 |
+
>>> img_lab = xyz2lab(img_xyz)
|
| 1104 |
+
"""
|
| 1105 |
+
arr = _prepare_colorarray(xyz, channel_axis=-1)
|
| 1106 |
+
|
| 1107 |
+
xyz_ref_white = xyz_tristimulus_values(
|
| 1108 |
+
illuminant=illuminant, observer=observer, dtype=arr.dtype
|
| 1109 |
+
)
|
| 1110 |
+
|
| 1111 |
+
# scale by CIE XYZ tristimulus values of the reference white point
|
| 1112 |
+
arr = arr / xyz_ref_white
|
| 1113 |
+
|
| 1114 |
+
# Nonlinear distortion and linear transformation
|
| 1115 |
+
mask = arr > 0.008856
|
| 1116 |
+
arr[mask] = np.cbrt(arr[mask])
|
| 1117 |
+
arr[~mask] = 7.787 * arr[~mask] + 16.0 / 116.0
|
| 1118 |
+
|
| 1119 |
+
x, y, z = arr[..., 0], arr[..., 1], arr[..., 2]
|
| 1120 |
+
|
| 1121 |
+
# Vector scaling
|
| 1122 |
+
L = (116.0 * y) - 16.0
|
| 1123 |
+
a = 500.0 * (x - y)
|
| 1124 |
+
b = 200.0 * (y - z)
|
| 1125 |
+
|
| 1126 |
+
return np.concatenate([x[..., np.newaxis] for x in [L, a, b]], axis=-1)
|
| 1127 |
+
|
| 1128 |
+
|
| 1129 |
+
@channel_as_last_axis()
|
| 1130 |
+
def lab2xyz(lab, illuminant="D65", observer="2", *, channel_axis=-1):
|
| 1131 |
+
"""Convert image in CIE-LAB to XYZ color space.
|
| 1132 |
+
|
| 1133 |
+
Parameters
|
| 1134 |
+
----------
|
| 1135 |
+
lab : (..., C=3, ...) array_like
|
| 1136 |
+
The input image in CIE-LAB color space.
|
| 1137 |
+
Unless `channel_axis` is set, the final dimension denotes the CIE-LAB
|
| 1138 |
+
channels.
|
| 1139 |
+
The L* values range from 0 to 100;
|
| 1140 |
+
the a* and b* values range from -128 to 127.
|
| 1141 |
+
illuminant : {"A", "B", "C", "D50", "D55", "D65", "D75", "E"}, optional
|
| 1142 |
+
The name of the illuminant (the function is NOT case sensitive).
|
| 1143 |
+
observer : {"2", "10", "R"}, optional
|
| 1144 |
+
The aperture angle of the observer.
|
| 1145 |
+
channel_axis : int, optional
|
| 1146 |
+
This parameter indicates which axis of the array corresponds to
|
| 1147 |
+
channels.
|
| 1148 |
+
|
| 1149 |
+
.. versionadded:: 0.19
|
| 1150 |
+
``channel_axis`` was added in 0.19.
|
| 1151 |
+
|
| 1152 |
+
Returns
|
| 1153 |
+
-------
|
| 1154 |
+
out : (..., C=3, ...) ndarray
|
| 1155 |
+
The image in XYZ color space, of same shape as input.
|
| 1156 |
+
|
| 1157 |
+
Raises
|
| 1158 |
+
------
|
| 1159 |
+
ValueError
|
| 1160 |
+
If `lab` is not at least 2-D with shape (..., C=3, ...).
|
| 1161 |
+
ValueError
|
| 1162 |
+
If either the illuminant or the observer angle are not supported or
|
| 1163 |
+
unknown.
|
| 1164 |
+
UserWarning
|
| 1165 |
+
If any of the pixels are invalid (Z < 0).
|
| 1166 |
+
|
| 1167 |
+
Notes
|
| 1168 |
+
-----
|
| 1169 |
+
The CIE XYZ tristimulus values are x_ref = 95.047, y_ref = 100., and
|
| 1170 |
+
z_ref = 108.883. See function :func:`~.xyz_tristimulus_values` for a list of
|
| 1171 |
+
supported illuminants.
|
| 1172 |
+
|
| 1173 |
+
See Also
|
| 1174 |
+
--------
|
| 1175 |
+
xyz2lab
|
| 1176 |
+
|
| 1177 |
+
References
|
| 1178 |
+
----------
|
| 1179 |
+
.. [1] http://www.easyrgb.com/en/math.php
|
| 1180 |
+
.. [2] https://en.wikipedia.org/wiki/CIELAB_color_space
|
| 1181 |
+
"""
|
| 1182 |
+
xyz, n_invalid = _lab2xyz(lab, illuminant, observer)
|
| 1183 |
+
if n_invalid != 0:
|
| 1184 |
+
warn(
|
| 1185 |
+
"Conversion from CIE-LAB to XYZ color space resulted in "
|
| 1186 |
+
f"{n_invalid} negative Z values that have been clipped to zero",
|
| 1187 |
+
stacklevel=3,
|
| 1188 |
+
)
|
| 1189 |
+
return xyz
|
| 1190 |
+
|
| 1191 |
+
|
| 1192 |
+
def _lab2xyz(lab, illuminant, observer):
|
| 1193 |
+
"""Convert CIE-LAB to XYZ color space.
|
| 1194 |
+
|
| 1195 |
+
Internal function for :func:`~.lab2xyz` and others. In addition to the
|
| 1196 |
+
converted image, return the number of invalid pixels in the Z channel for
|
| 1197 |
+
correct warning propagation.
|
| 1198 |
+
|
| 1199 |
+
Returns
|
| 1200 |
+
-------
|
| 1201 |
+
out : (..., C=3, ...) ndarray
|
| 1202 |
+
The image in XYZ format. Same dimensions as input.
|
| 1203 |
+
n_invalid : int
|
| 1204 |
+
Number of invalid pixels in the Z channel after conversion.
|
| 1205 |
+
"""
|
| 1206 |
+
arr = _prepare_colorarray(lab, channel_axis=-1).copy()
|
| 1207 |
+
|
| 1208 |
+
L, a, b = arr[..., 0], arr[..., 1], arr[..., 2]
|
| 1209 |
+
y = (L + 16.0) / 116.0
|
| 1210 |
+
x = (a / 500.0) + y
|
| 1211 |
+
z = y - (b / 200.0)
|
| 1212 |
+
|
| 1213 |
+
invalid = np.atleast_1d(z < 0).nonzero()
|
| 1214 |
+
n_invalid = invalid[0].size
|
| 1215 |
+
if n_invalid != 0:
|
| 1216 |
+
# Warning should be emitted by caller
|
| 1217 |
+
if z.ndim > 0:
|
| 1218 |
+
z[invalid] = 0
|
| 1219 |
+
else:
|
| 1220 |
+
z = 0
|
| 1221 |
+
|
| 1222 |
+
out = np.stack([x, y, z], axis=-1)
|
| 1223 |
+
|
| 1224 |
+
mask = out > 0.2068966
|
| 1225 |
+
out[mask] = np.power(out[mask], 3.0)
|
| 1226 |
+
out[~mask] = (out[~mask] - 16.0 / 116.0) / 7.787
|
| 1227 |
+
|
| 1228 |
+
# rescale to the reference white (illuminant)
|
| 1229 |
+
xyz_ref_white = xyz_tristimulus_values(illuminant=illuminant, observer=observer)
|
| 1230 |
+
out *= xyz_ref_white
|
| 1231 |
+
return out, n_invalid
|
| 1232 |
+
|
| 1233 |
+
|
| 1234 |
+
@channel_as_last_axis()
|
| 1235 |
+
def rgb2lab(rgb, illuminant="D65", observer="2", *, channel_axis=-1):
|
| 1236 |
+
"""Conversion from the sRGB color space (IEC 61966-2-1:1999)
|
| 1237 |
+
to the CIE Lab colorspace under the given illuminant and observer.
|
| 1238 |
+
|
| 1239 |
+
Parameters
|
| 1240 |
+
----------
|
| 1241 |
+
rgb : (..., C=3, ...) array_like
|
| 1242 |
+
The image in RGB format. By default, the final dimension denotes
|
| 1243 |
+
channels.
|
| 1244 |
+
illuminant : {"A", "B", "C", "D50", "D55", "D65", "D75", "E"}, optional
|
| 1245 |
+
The name of the illuminant (the function is NOT case sensitive).
|
| 1246 |
+
observer : {"2", "10", "R"}, optional
|
| 1247 |
+
The aperture angle of the observer.
|
| 1248 |
+
channel_axis : int, optional
|
| 1249 |
+
This parameter indicates which axis of the array corresponds to
|
| 1250 |
+
channels.
|
| 1251 |
+
|
| 1252 |
+
.. versionadded:: 0.19
|
| 1253 |
+
``channel_axis`` was added in 0.19.
|
| 1254 |
+
|
| 1255 |
+
Returns
|
| 1256 |
+
-------
|
| 1257 |
+
out : (..., C=3, ...) ndarray
|
| 1258 |
+
The image in Lab format. Same dimensions as input.
|
| 1259 |
+
|
| 1260 |
+
Raises
|
| 1261 |
+
------
|
| 1262 |
+
ValueError
|
| 1263 |
+
If `rgb` is not at least 2-D with shape (..., C=3, ...).
|
| 1264 |
+
|
| 1265 |
+
Notes
|
| 1266 |
+
-----
|
| 1267 |
+
RGB is a device-dependent color space so, if you use this function, be
|
| 1268 |
+
sure that the image you are analyzing has been mapped to the sRGB color
|
| 1269 |
+
space.
|
| 1270 |
+
|
| 1271 |
+
This function uses rgb2xyz and xyz2lab.
|
| 1272 |
+
By default Observer="2", Illuminant="D65". CIE XYZ tristimulus values
|
| 1273 |
+
x_ref=95.047, y_ref=100., z_ref=108.883. See function
|
| 1274 |
+
:func:`~.xyz_tristimulus_values` for a list of supported illuminants.
|
| 1275 |
+
|
| 1276 |
+
References
|
| 1277 |
+
----------
|
| 1278 |
+
.. [1] https://en.wikipedia.org/wiki/Standard_illuminant
|
| 1279 |
+
"""
|
| 1280 |
+
return xyz2lab(rgb2xyz(rgb), illuminant, observer)
|
| 1281 |
+
|
| 1282 |
+
|
| 1283 |
+
@channel_as_last_axis()
|
| 1284 |
+
def lab2rgb(lab, illuminant="D65", observer="2", *, channel_axis=-1):
|
| 1285 |
+
"""Convert image in CIE-LAB to sRGB color space.
|
| 1286 |
+
|
| 1287 |
+
Parameters
|
| 1288 |
+
----------
|
| 1289 |
+
lab : (..., C=3, ...) array_like
|
| 1290 |
+
The input image in CIE-LAB color space.
|
| 1291 |
+
Unless `channel_axis` is set, the final dimension denotes the CIE-LAB
|
| 1292 |
+
channels.
|
| 1293 |
+
The L* values range from 0 to 100;
|
| 1294 |
+
the a* and b* values range from -128 to 127.
|
| 1295 |
+
illuminant : {"A", "B", "C", "D50", "D55", "D65", "D75", "E"}, optional
|
| 1296 |
+
The name of the illuminant (the function is NOT case sensitive).
|
| 1297 |
+
observer : {"2", "10", "R"}, optional
|
| 1298 |
+
The aperture angle of the observer.
|
| 1299 |
+
channel_axis : int, optional
|
| 1300 |
+
This parameter indicates which axis of the array corresponds to
|
| 1301 |
+
channels.
|
| 1302 |
+
|
| 1303 |
+
.. versionadded:: 0.19
|
| 1304 |
+
``channel_axis`` was added in 0.19.
|
| 1305 |
+
|
| 1306 |
+
Returns
|
| 1307 |
+
-------
|
| 1308 |
+
out : (..., C=3, ...) ndarray
|
| 1309 |
+
The image in sRGB color space, of same shape as input.
|
| 1310 |
+
|
| 1311 |
+
Raises
|
| 1312 |
+
------
|
| 1313 |
+
ValueError
|
| 1314 |
+
If `lab` is not at least 2-D with shape (..., C=3, ...).
|
| 1315 |
+
|
| 1316 |
+
Notes
|
| 1317 |
+
-----
|
| 1318 |
+
This function uses :func:`~.lab2xyz` and :func:`~.xyz2rgb`.
|
| 1319 |
+
The CIE XYZ tristimulus values are x_ref = 95.047, y_ref = 100., and
|
| 1320 |
+
z_ref = 108.883. See function :func:`~.xyz_tristimulus_values` for a list of
|
| 1321 |
+
supported illuminants.
|
| 1322 |
+
|
| 1323 |
+
See Also
|
| 1324 |
+
--------
|
| 1325 |
+
rgb2lab
|
| 1326 |
+
|
| 1327 |
+
References
|
| 1328 |
+
----------
|
| 1329 |
+
.. [1] https://en.wikipedia.org/wiki/Standard_illuminant
|
| 1330 |
+
.. [2] https://en.wikipedia.org/wiki/CIELAB_color_space
|
| 1331 |
+
"""
|
| 1332 |
+
xyz, n_invalid = _lab2xyz(lab, illuminant, observer)
|
| 1333 |
+
if n_invalid != 0:
|
| 1334 |
+
warn(
|
| 1335 |
+
"Conversion from CIE-LAB, via XYZ to sRGB color space resulted in "
|
| 1336 |
+
f"{n_invalid} negative Z values that have been clipped to zero",
|
| 1337 |
+
stacklevel=3,
|
| 1338 |
+
)
|
| 1339 |
+
return xyz2rgb(xyz)
|
| 1340 |
+
|
| 1341 |
+
|
| 1342 |
+
@channel_as_last_axis()
|
| 1343 |
+
def xyz2luv(xyz, illuminant="D65", observer="2", *, channel_axis=-1):
|
| 1344 |
+
"""XYZ to CIE-Luv color space conversion.
|
| 1345 |
+
|
| 1346 |
+
Parameters
|
| 1347 |
+
----------
|
| 1348 |
+
xyz : (..., C=3, ...) array_like
|
| 1349 |
+
The image in XYZ format. By default, the final dimension denotes
|
| 1350 |
+
channels.
|
| 1351 |
+
illuminant : {"A", "B", "C", "D50", "D55", "D65", "D75", "E"}, optional
|
| 1352 |
+
The name of the illuminant (the function is NOT case sensitive).
|
| 1353 |
+
observer : {"2", "10", "R"}, optional
|
| 1354 |
+
The aperture angle of the observer.
|
| 1355 |
+
channel_axis : int, optional
|
| 1356 |
+
This parameter indicates which axis of the array corresponds to
|
| 1357 |
+
channels.
|
| 1358 |
+
|
| 1359 |
+
.. versionadded:: 0.19
|
| 1360 |
+
``channel_axis`` was added in 0.19.
|
| 1361 |
+
|
| 1362 |
+
Returns
|
| 1363 |
+
-------
|
| 1364 |
+
out : (..., C=3, ...) ndarray
|
| 1365 |
+
The image in CIE-Luv format. Same dimensions as input.
|
| 1366 |
+
|
| 1367 |
+
Raises
|
| 1368 |
+
------
|
| 1369 |
+
ValueError
|
| 1370 |
+
If `xyz` is not at least 2-D with shape (..., C=3, ...).
|
| 1371 |
+
ValueError
|
| 1372 |
+
If either the illuminant or the observer angle are not supported or
|
| 1373 |
+
unknown.
|
| 1374 |
+
|
| 1375 |
+
Notes
|
| 1376 |
+
-----
|
| 1377 |
+
By default XYZ conversion weights use observer=2A. Reference whitepoint
|
| 1378 |
+
for D65 Illuminant, with XYZ tristimulus values of ``(95.047, 100.,
|
| 1379 |
+
108.883)``. See function :func:`~.xyz_tristimulus_values` for a list of supported
|
| 1380 |
+
illuminants.
|
| 1381 |
+
|
| 1382 |
+
References
|
| 1383 |
+
----------
|
| 1384 |
+
.. [1] http://www.easyrgb.com/en/math.php
|
| 1385 |
+
.. [2] https://en.wikipedia.org/wiki/CIELUV
|
| 1386 |
+
|
| 1387 |
+
Examples
|
| 1388 |
+
--------
|
| 1389 |
+
>>> from skimage import data
|
| 1390 |
+
>>> from skimage.color import rgb2xyz, xyz2luv
|
| 1391 |
+
>>> img = data.astronaut()
|
| 1392 |
+
>>> img_xyz = rgb2xyz(img)
|
| 1393 |
+
>>> img_luv = xyz2luv(img_xyz)
|
| 1394 |
+
"""
|
| 1395 |
+
input_is_one_pixel = xyz.ndim == 1
|
| 1396 |
+
if input_is_one_pixel:
|
| 1397 |
+
xyz = xyz[np.newaxis, ...]
|
| 1398 |
+
|
| 1399 |
+
arr = _prepare_colorarray(xyz, channel_axis=-1)
|
| 1400 |
+
|
| 1401 |
+
# extract channels
|
| 1402 |
+
x, y, z = arr[..., 0], arr[..., 1], arr[..., 2]
|
| 1403 |
+
|
| 1404 |
+
eps = np.finfo(arr.dtype).eps
|
| 1405 |
+
|
| 1406 |
+
# compute y_r and L
|
| 1407 |
+
xyz_ref_white = xyz_tristimulus_values(
|
| 1408 |
+
illuminant=illuminant, observer=observer, dtype=arr.dtype
|
| 1409 |
+
)
|
| 1410 |
+
L = y / xyz_ref_white[1]
|
| 1411 |
+
mask = L > 0.008856
|
| 1412 |
+
L[mask] = 116.0 * np.cbrt(L[mask]) - 16.0
|
| 1413 |
+
L[~mask] = 903.3 * L[~mask]
|
| 1414 |
+
|
| 1415 |
+
uv_weights = np.array([1, 15, 3], dtype=arr.dtype)
|
| 1416 |
+
u0 = 4 * xyz_ref_white[0] / (uv_weights @ xyz_ref_white)
|
| 1417 |
+
v0 = 9 * xyz_ref_white[1] / (uv_weights @ xyz_ref_white)
|
| 1418 |
+
|
| 1419 |
+
# u' and v' helper functions
|
| 1420 |
+
def fu(X, Y, Z):
|
| 1421 |
+
return (4.0 * X) / (X + 15.0 * Y + 3.0 * Z + eps)
|
| 1422 |
+
|
| 1423 |
+
def fv(X, Y, Z):
|
| 1424 |
+
return (9.0 * Y) / (X + 15.0 * Y + 3.0 * Z + eps)
|
| 1425 |
+
|
| 1426 |
+
# compute u and v using helper functions
|
| 1427 |
+
u = 13.0 * L * (fu(x, y, z) - u0)
|
| 1428 |
+
v = 13.0 * L * (fv(x, y, z) - v0)
|
| 1429 |
+
|
| 1430 |
+
out = np.stack([L, u, v], axis=-1)
|
| 1431 |
+
|
| 1432 |
+
if input_is_one_pixel:
|
| 1433 |
+
out = np.squeeze(out, axis=0)
|
| 1434 |
+
|
| 1435 |
+
return out
|
| 1436 |
+
|
| 1437 |
+
|
| 1438 |
+
@channel_as_last_axis()
|
| 1439 |
+
def luv2xyz(luv, illuminant="D65", observer="2", *, channel_axis=-1):
|
| 1440 |
+
"""CIE-Luv to XYZ color space conversion.
|
| 1441 |
+
|
| 1442 |
+
Parameters
|
| 1443 |
+
----------
|
| 1444 |
+
luv : (..., C=3, ...) array_like
|
| 1445 |
+
The image in CIE-Luv format. By default, the final dimension denotes
|
| 1446 |
+
channels.
|
| 1447 |
+
illuminant : {"A", "B", "C", "D50", "D55", "D65", "D75", "E"}, optional
|
| 1448 |
+
The name of the illuminant (the function is NOT case sensitive).
|
| 1449 |
+
observer : {"2", "10", "R"}, optional
|
| 1450 |
+
The aperture angle of the observer.
|
| 1451 |
+
channel_axis : int, optional
|
| 1452 |
+
This parameter indicates which axis of the array corresponds to
|
| 1453 |
+
channels.
|
| 1454 |
+
|
| 1455 |
+
.. versionadded:: 0.19
|
| 1456 |
+
``channel_axis`` was added in 0.19.
|
| 1457 |
+
|
| 1458 |
+
Returns
|
| 1459 |
+
-------
|
| 1460 |
+
out : (..., C=3, ...) ndarray
|
| 1461 |
+
The image in XYZ format. Same dimensions as input.
|
| 1462 |
+
|
| 1463 |
+
Raises
|
| 1464 |
+
------
|
| 1465 |
+
ValueError
|
| 1466 |
+
If `luv` is not at least 2-D with shape (..., C=3, ...).
|
| 1467 |
+
ValueError
|
| 1468 |
+
If either the illuminant or the observer angle are not supported or
|
| 1469 |
+
unknown.
|
| 1470 |
+
|
| 1471 |
+
Notes
|
| 1472 |
+
-----
|
| 1473 |
+
XYZ conversion weights use observer=2A. Reference whitepoint for D65
|
| 1474 |
+
Illuminant, with XYZ tristimulus values of ``(95.047, 100., 108.883)``. See
|
| 1475 |
+
function :func:`~.xyz_tristimulus_values` for a list of supported illuminants.
|
| 1476 |
+
|
| 1477 |
+
References
|
| 1478 |
+
----------
|
| 1479 |
+
.. [1] http://www.easyrgb.com/en/math.php
|
| 1480 |
+
.. [2] https://en.wikipedia.org/wiki/CIELUV
|
| 1481 |
+
"""
|
| 1482 |
+
arr = _prepare_colorarray(luv, channel_axis=-1).copy()
|
| 1483 |
+
|
| 1484 |
+
L, u, v = arr[..., 0], arr[..., 1], arr[..., 2]
|
| 1485 |
+
|
| 1486 |
+
eps = np.finfo(arr.dtype).eps
|
| 1487 |
+
|
| 1488 |
+
# compute y
|
| 1489 |
+
y = L.copy()
|
| 1490 |
+
mask = y > 7.999625
|
| 1491 |
+
y[mask] = np.power((y[mask] + 16.0) / 116.0, 3.0)
|
| 1492 |
+
y[~mask] = y[~mask] / 903.3
|
| 1493 |
+
xyz_ref_white = xyz_tristimulus_values(
|
| 1494 |
+
illuminant=illuminant, observer=observer, dtype=arr.dtype
|
| 1495 |
+
)
|
| 1496 |
+
y *= xyz_ref_white[1]
|
| 1497 |
+
|
| 1498 |
+
# reference white x,z
|
| 1499 |
+
uv_weights = np.array([1, 15, 3], dtype=arr.dtype)
|
| 1500 |
+
u0 = 4 * xyz_ref_white[0] / (uv_weights @ xyz_ref_white)
|
| 1501 |
+
v0 = 9 * xyz_ref_white[1] / (uv_weights @ xyz_ref_white)
|
| 1502 |
+
|
| 1503 |
+
# compute intermediate values
|
| 1504 |
+
a = u0 + u / (13.0 * L + eps)
|
| 1505 |
+
b = v0 + v / (13.0 * L + eps)
|
| 1506 |
+
c = 3 * y * (5 * b - 3)
|
| 1507 |
+
|
| 1508 |
+
# compute x and z
|
| 1509 |
+
z = ((a - 4) * c - 15 * a * b * y) / (12 * b)
|
| 1510 |
+
x = -(c / b + 3.0 * z)
|
| 1511 |
+
|
| 1512 |
+
return np.concatenate([q[..., np.newaxis] for q in [x, y, z]], axis=-1)
|
| 1513 |
+
|
| 1514 |
+
|
| 1515 |
+
@channel_as_last_axis()
|
| 1516 |
+
def rgb2luv(rgb, *, channel_axis=-1):
|
| 1517 |
+
"""RGB to CIE-Luv color space conversion.
|
| 1518 |
+
|
| 1519 |
+
Parameters
|
| 1520 |
+
----------
|
| 1521 |
+
rgb : (..., C=3, ...) array_like
|
| 1522 |
+
The image in RGB format. By default, the final dimension denotes
|
| 1523 |
+
channels.
|
| 1524 |
+
channel_axis : int, optional
|
| 1525 |
+
This parameter indicates which axis of the array corresponds to
|
| 1526 |
+
channels.
|
| 1527 |
+
|
| 1528 |
+
.. versionadded:: 0.19
|
| 1529 |
+
``channel_axis`` was added in 0.19.
|
| 1530 |
+
|
| 1531 |
+
Returns
|
| 1532 |
+
-------
|
| 1533 |
+
out : (..., C=3, ...) ndarray
|
| 1534 |
+
The image in CIE Luv format. Same dimensions as input.
|
| 1535 |
+
|
| 1536 |
+
Raises
|
| 1537 |
+
------
|
| 1538 |
+
ValueError
|
| 1539 |
+
If `rgb` is not at least 2-D with shape (..., C=3, ...).
|
| 1540 |
+
|
| 1541 |
+
Notes
|
| 1542 |
+
-----
|
| 1543 |
+
This function uses rgb2xyz and xyz2luv.
|
| 1544 |
+
|
| 1545 |
+
References
|
| 1546 |
+
----------
|
| 1547 |
+
.. [1] http://www.easyrgb.com/en/math.php
|
| 1548 |
+
.. [2] https://en.wikipedia.org/wiki/CIELUV
|
| 1549 |
+
"""
|
| 1550 |
+
return xyz2luv(rgb2xyz(rgb))
|
| 1551 |
+
|
| 1552 |
+
|
| 1553 |
+
@channel_as_last_axis()
|
| 1554 |
+
def luv2rgb(luv, *, channel_axis=-1):
|
| 1555 |
+
"""Luv to RGB color space conversion.
|
| 1556 |
+
|
| 1557 |
+
Parameters
|
| 1558 |
+
----------
|
| 1559 |
+
luv : (..., C=3, ...) array_like
|
| 1560 |
+
The image in CIE Luv format. By default, the final dimension denotes
|
| 1561 |
+
channels.
|
| 1562 |
+
|
| 1563 |
+
Returns
|
| 1564 |
+
-------
|
| 1565 |
+
out : (..., C=3, ...) ndarray
|
| 1566 |
+
The image in RGB format. Same dimensions as input.
|
| 1567 |
+
|
| 1568 |
+
Raises
|
| 1569 |
+
------
|
| 1570 |
+
ValueError
|
| 1571 |
+
If `luv` is not at least 2-D with shape (..., C=3, ...).
|
| 1572 |
+
|
| 1573 |
+
Notes
|
| 1574 |
+
-----
|
| 1575 |
+
This function uses luv2xyz and xyz2rgb.
|
| 1576 |
+
"""
|
| 1577 |
+
return xyz2rgb(luv2xyz(luv))
|
| 1578 |
+
|
| 1579 |
+
|
| 1580 |
+
@channel_as_last_axis()
|
| 1581 |
+
def rgb2hed(rgb, *, channel_axis=-1):
|
| 1582 |
+
"""RGB to Haematoxylin-Eosin-DAB (HED) color space conversion.
|
| 1583 |
+
|
| 1584 |
+
Parameters
|
| 1585 |
+
----------
|
| 1586 |
+
rgb : (..., C=3, ...) array_like
|
| 1587 |
+
The image in RGB format. By default, the final dimension denotes
|
| 1588 |
+
channels.
|
| 1589 |
+
channel_axis : int, optional
|
| 1590 |
+
This parameter indicates which axis of the array corresponds to
|
| 1591 |
+
channels.
|
| 1592 |
+
|
| 1593 |
+
.. versionadded:: 0.19
|
| 1594 |
+
``channel_axis`` was added in 0.19.
|
| 1595 |
+
|
| 1596 |
+
Returns
|
| 1597 |
+
-------
|
| 1598 |
+
out : (..., C=3, ...) ndarray
|
| 1599 |
+
The image in HED format. Same dimensions as input.
|
| 1600 |
+
|
| 1601 |
+
Raises
|
| 1602 |
+
------
|
| 1603 |
+
ValueError
|
| 1604 |
+
If `rgb` is not at least 2-D with shape (..., C=3, ...).
|
| 1605 |
+
|
| 1606 |
+
References
|
| 1607 |
+
----------
|
| 1608 |
+
.. [1] A. C. Ruifrok and D. A. Johnston, "Quantification of histochemical
|
| 1609 |
+
staining by color deconvolution.," Analytical and quantitative
|
| 1610 |
+
cytology and histology / the International Academy of Cytology [and]
|
| 1611 |
+
American Society of Cytology, vol. 23, no. 4, pp. 291-9, Aug. 2001.
|
| 1612 |
+
|
| 1613 |
+
Examples
|
| 1614 |
+
--------
|
| 1615 |
+
>>> from skimage import data
|
| 1616 |
+
>>> from skimage.color import rgb2hed
|
| 1617 |
+
>>> ihc = data.immunohistochemistry()
|
| 1618 |
+
>>> ihc_hed = rgb2hed(ihc)
|
| 1619 |
+
"""
|
| 1620 |
+
return separate_stains(rgb, hed_from_rgb)
|
| 1621 |
+
|
| 1622 |
+
|
| 1623 |
+
@channel_as_last_axis()
|
| 1624 |
+
def hed2rgb(hed, *, channel_axis=-1):
|
| 1625 |
+
"""Haematoxylin-Eosin-DAB (HED) to RGB color space conversion.
|
| 1626 |
+
|
| 1627 |
+
Parameters
|
| 1628 |
+
----------
|
| 1629 |
+
hed : (..., C=3, ...) array_like
|
| 1630 |
+
The image in the HED color space. By default, the final dimension
|
| 1631 |
+
denotes channels.
|
| 1632 |
+
channel_axis : int, optional
|
| 1633 |
+
This parameter indicates which axis of the array corresponds to
|
| 1634 |
+
channels.
|
| 1635 |
+
|
| 1636 |
+
.. versionadded:: 0.19
|
| 1637 |
+
``channel_axis`` was added in 0.19.
|
| 1638 |
+
|
| 1639 |
+
Returns
|
| 1640 |
+
-------
|
| 1641 |
+
out : (..., C=3, ...) ndarray
|
| 1642 |
+
The image in RGB. Same dimensions as input.
|
| 1643 |
+
|
| 1644 |
+
Raises
|
| 1645 |
+
------
|
| 1646 |
+
ValueError
|
| 1647 |
+
If `hed` is not at least 2-D with shape (..., C=3, ...).
|
| 1648 |
+
|
| 1649 |
+
References
|
| 1650 |
+
----------
|
| 1651 |
+
.. [1] A. C. Ruifrok and D. A. Johnston, "Quantification of histochemical
|
| 1652 |
+
staining by color deconvolution.," Analytical and quantitative
|
| 1653 |
+
cytology and histology / the International Academy of Cytology [and]
|
| 1654 |
+
American Society of Cytology, vol. 23, no. 4, pp. 291-9, Aug. 2001.
|
| 1655 |
+
|
| 1656 |
+
Examples
|
| 1657 |
+
--------
|
| 1658 |
+
>>> from skimage import data
|
| 1659 |
+
>>> from skimage.color import rgb2hed, hed2rgb
|
| 1660 |
+
>>> ihc = data.immunohistochemistry()
|
| 1661 |
+
>>> ihc_hed = rgb2hed(ihc)
|
| 1662 |
+
>>> ihc_rgb = hed2rgb(ihc_hed)
|
| 1663 |
+
"""
|
| 1664 |
+
return combine_stains(hed, rgb_from_hed)
|
| 1665 |
+
|
| 1666 |
+
|
| 1667 |
+
@channel_as_last_axis()
|
| 1668 |
+
def separate_stains(rgb, conv_matrix, *, channel_axis=-1):
|
| 1669 |
+
"""RGB to stain color space conversion.
|
| 1670 |
+
|
| 1671 |
+
Parameters
|
| 1672 |
+
----------
|
| 1673 |
+
rgb : (..., C=3, ...) array_like
|
| 1674 |
+
The image in RGB format. By default, the final dimension denotes
|
| 1675 |
+
channels.
|
| 1676 |
+
conv_matrix : ndarray
|
| 1677 |
+
The stain separation matrix as described by G. Landini [1]_.
|
| 1678 |
+
channel_axis : int, optional
|
| 1679 |
+
This parameter indicates which axis of the array corresponds to
|
| 1680 |
+
channels.
|
| 1681 |
+
|
| 1682 |
+
.. versionadded:: 0.19
|
| 1683 |
+
``channel_axis`` was added in 0.19.
|
| 1684 |
+
|
| 1685 |
+
Returns
|
| 1686 |
+
-------
|
| 1687 |
+
out : (..., C=3, ...) ndarray
|
| 1688 |
+
The image in stain color space. Same dimensions as input.
|
| 1689 |
+
|
| 1690 |
+
Raises
|
| 1691 |
+
------
|
| 1692 |
+
ValueError
|
| 1693 |
+
If `rgb` is not at least 2-D with shape (..., C=3, ...).
|
| 1694 |
+
|
| 1695 |
+
Notes
|
| 1696 |
+
-----
|
| 1697 |
+
Stain separation matrices available in the ``color`` module and their
|
| 1698 |
+
respective colorspace:
|
| 1699 |
+
|
| 1700 |
+
* ``hed_from_rgb``: Hematoxylin + Eosin + DAB
|
| 1701 |
+
* ``hdx_from_rgb``: Hematoxylin + DAB
|
| 1702 |
+
* ``fgx_from_rgb``: Feulgen + Light Green
|
| 1703 |
+
* ``bex_from_rgb``: Giemsa stain : Methyl Blue + Eosin
|
| 1704 |
+
* ``rbd_from_rgb``: FastRed + FastBlue + DAB
|
| 1705 |
+
* ``gdx_from_rgb``: Methyl Green + DAB
|
| 1706 |
+
* ``hax_from_rgb``: Hematoxylin + AEC
|
| 1707 |
+
* ``bro_from_rgb``: Blue matrix Anilline Blue + Red matrix Azocarmine\
|
| 1708 |
+
+ Orange matrix Orange-G
|
| 1709 |
+
* ``bpx_from_rgb``: Methyl Blue + Ponceau Fuchsin
|
| 1710 |
+
* ``ahx_from_rgb``: Alcian Blue + Hematoxylin
|
| 1711 |
+
* ``hpx_from_rgb``: Hematoxylin + PAS
|
| 1712 |
+
|
| 1713 |
+
This implementation borrows some ideas from DIPlib [2]_, e.g. the
|
| 1714 |
+
compensation using a small value to avoid log artifacts when
|
| 1715 |
+
calculating the Beer-Lambert law.
|
| 1716 |
+
|
| 1717 |
+
References
|
| 1718 |
+
----------
|
| 1719 |
+
.. [1] https://web.archive.org/web/20160624145052/http://www.mecourse.com/landinig/software/cdeconv/cdeconv.html
|
| 1720 |
+
.. [2] https://github.com/DIPlib/diplib/
|
| 1721 |
+
.. [3] A. C. Ruifrok and D. A. Johnston, “Quantification of histochemical
|
| 1722 |
+
staining by color deconvolution,” Anal. Quant. Cytol. Histol., vol.
|
| 1723 |
+
23, no. 4, pp. 291–299, Aug. 2001.
|
| 1724 |
+
|
| 1725 |
+
Examples
|
| 1726 |
+
--------
|
| 1727 |
+
>>> from skimage import data
|
| 1728 |
+
>>> from skimage.color import separate_stains, hdx_from_rgb
|
| 1729 |
+
>>> ihc = data.immunohistochemistry()
|
| 1730 |
+
>>> ihc_hdx = separate_stains(ihc, hdx_from_rgb)
|
| 1731 |
+
"""
|
| 1732 |
+
rgb = _prepare_colorarray(rgb, force_copy=True, channel_axis=-1)
|
| 1733 |
+
np.maximum(rgb, 1e-6, out=rgb) # avoiding log artifacts
|
| 1734 |
+
log_adjust = np.log(1e-6) # used to compensate the sum above
|
| 1735 |
+
|
| 1736 |
+
stains = (np.log(rgb) / log_adjust) @ conv_matrix
|
| 1737 |
+
|
| 1738 |
+
np.maximum(stains, 0, out=stains)
|
| 1739 |
+
|
| 1740 |
+
return stains
|
| 1741 |
+
|
| 1742 |
+
|
| 1743 |
+
@channel_as_last_axis()
|
| 1744 |
+
def combine_stains(stains, conv_matrix, *, channel_axis=-1):
|
| 1745 |
+
"""Stain to RGB color space conversion.
|
| 1746 |
+
|
| 1747 |
+
Parameters
|
| 1748 |
+
----------
|
| 1749 |
+
stains : (..., C=3, ...) array_like
|
| 1750 |
+
The image in stain color space. By default, the final dimension denotes
|
| 1751 |
+
channels.
|
| 1752 |
+
conv_matrix : ndarray
|
| 1753 |
+
The stain separation matrix as described by G. Landini [1]_.
|
| 1754 |
+
channel_axis : int, optional
|
| 1755 |
+
This parameter indicates which axis of the array corresponds to
|
| 1756 |
+
channels.
|
| 1757 |
+
|
| 1758 |
+
.. versionadded:: 0.19
|
| 1759 |
+
``channel_axis`` was added in 0.19.
|
| 1760 |
+
|
| 1761 |
+
Returns
|
| 1762 |
+
-------
|
| 1763 |
+
out : (..., C=3, ...) ndarray
|
| 1764 |
+
The image in RGB format. Same dimensions as input.
|
| 1765 |
+
|
| 1766 |
+
Raises
|
| 1767 |
+
------
|
| 1768 |
+
ValueError
|
| 1769 |
+
If `stains` is not at least 2-D with shape (..., C=3, ...).
|
| 1770 |
+
|
| 1771 |
+
Notes
|
| 1772 |
+
-----
|
| 1773 |
+
Stain combination matrices available in the ``color`` module and their
|
| 1774 |
+
respective colorspace:
|
| 1775 |
+
|
| 1776 |
+
* ``rgb_from_hed``: Hematoxylin + Eosin + DAB
|
| 1777 |
+
* ``rgb_from_hdx``: Hematoxylin + DAB
|
| 1778 |
+
* ``rgb_from_fgx``: Feulgen + Light Green
|
| 1779 |
+
* ``rgb_from_bex``: Giemsa stain : Methyl Blue + Eosin
|
| 1780 |
+
* ``rgb_from_rbd``: FastRed + FastBlue + DAB
|
| 1781 |
+
* ``rgb_from_gdx``: Methyl Green + DAB
|
| 1782 |
+
* ``rgb_from_hax``: Hematoxylin + AEC
|
| 1783 |
+
* ``rgb_from_bro``: Blue matrix Anilline Blue + Red matrix Azocarmine\
|
| 1784 |
+
+ Orange matrix Orange-G
|
| 1785 |
+
* ``rgb_from_bpx``: Methyl Blue + Ponceau Fuchsin
|
| 1786 |
+
* ``rgb_from_ahx``: Alcian Blue + Hematoxylin
|
| 1787 |
+
* ``rgb_from_hpx``: Hematoxylin + PAS
|
| 1788 |
+
|
| 1789 |
+
References
|
| 1790 |
+
----------
|
| 1791 |
+
.. [1] https://web.archive.org/web/20160624145052/http://www.mecourse.com/landinig/software/cdeconv/cdeconv.html
|
| 1792 |
+
.. [2] A. C. Ruifrok and D. A. Johnston, “Quantification of histochemical
|
| 1793 |
+
staining by color deconvolution,” Anal. Quant. Cytol. Histol., vol.
|
| 1794 |
+
23, no. 4, pp. 291–299, Aug. 2001.
|
| 1795 |
+
|
| 1796 |
+
Examples
|
| 1797 |
+
--------
|
| 1798 |
+
>>> from skimage import data
|
| 1799 |
+
>>> from skimage.color import (separate_stains, combine_stains,
|
| 1800 |
+
... hdx_from_rgb, rgb_from_hdx)
|
| 1801 |
+
>>> ihc = data.immunohistochemistry()
|
| 1802 |
+
>>> ihc_hdx = separate_stains(ihc, hdx_from_rgb)
|
| 1803 |
+
>>> ihc_rgb = combine_stains(ihc_hdx, rgb_from_hdx)
|
| 1804 |
+
"""
|
| 1805 |
+
stains = _prepare_colorarray(stains, channel_axis=-1)
|
| 1806 |
+
|
| 1807 |
+
# log_adjust here is used to compensate the sum within separate_stains().
|
| 1808 |
+
log_adjust = -np.log(1e-6)
|
| 1809 |
+
log_rgb = -(stains * log_adjust) @ conv_matrix
|
| 1810 |
+
rgb = np.exp(log_rgb)
|
| 1811 |
+
|
| 1812 |
+
return np.clip(rgb, a_min=0, a_max=1)
|
| 1813 |
+
|
| 1814 |
+
|
| 1815 |
+
@channel_as_last_axis()
|
| 1816 |
+
def lab2lch(lab, *, channel_axis=-1):
|
| 1817 |
+
"""Convert image in CIE-LAB to CIE-LCh color space.
|
| 1818 |
+
|
| 1819 |
+
CIE-LCh is the cylindrical representation of the CIE-LAB (Cartesian) color
|
| 1820 |
+
space.
|
| 1821 |
+
|
| 1822 |
+
Parameters
|
| 1823 |
+
----------
|
| 1824 |
+
lab : (..., C=3, ...) array_like
|
| 1825 |
+
The input image in CIE-LAB color space.
|
| 1826 |
+
Unless `channel_axis` is set, the final dimension denotes the CIE-LAB
|
| 1827 |
+
channels.
|
| 1828 |
+
The L* values range from 0 to 100;
|
| 1829 |
+
the a* and b* values range from -128 to 127.
|
| 1830 |
+
channel_axis : int, optional
|
| 1831 |
+
This parameter indicates which axis of the array corresponds to
|
| 1832 |
+
channels.
|
| 1833 |
+
|
| 1834 |
+
.. versionadded:: 0.19
|
| 1835 |
+
``channel_axis`` was added in 0.19.
|
| 1836 |
+
|
| 1837 |
+
Returns
|
| 1838 |
+
-------
|
| 1839 |
+
out : (..., C=3, ...) ndarray
|
| 1840 |
+
The image in CIE-LCh color space, of same shape as input.
|
| 1841 |
+
|
| 1842 |
+
Raises
|
| 1843 |
+
------
|
| 1844 |
+
ValueError
|
| 1845 |
+
If `lab` does not have at least 3 channels (i.e., L*, a*, and b*).
|
| 1846 |
+
|
| 1847 |
+
Notes
|
| 1848 |
+
-----
|
| 1849 |
+
The h channel (i.e., hue) is expressed as an angle in range ``(0, 2*pi)``.
|
| 1850 |
+
|
| 1851 |
+
See Also
|
| 1852 |
+
--------
|
| 1853 |
+
lch2lab
|
| 1854 |
+
|
| 1855 |
+
References
|
| 1856 |
+
----------
|
| 1857 |
+
.. [1] http://www.easyrgb.com/en/math.php
|
| 1858 |
+
.. [2] https://en.wikipedia.org/wiki/CIELAB_color_space
|
| 1859 |
+
.. [3] https://en.wikipedia.org/wiki/HCL_color_space
|
| 1860 |
+
|
| 1861 |
+
Examples
|
| 1862 |
+
--------
|
| 1863 |
+
>>> from skimage import data
|
| 1864 |
+
>>> from skimage.color import rgb2lab, lab2lch
|
| 1865 |
+
>>> img = data.astronaut()
|
| 1866 |
+
>>> img_lab = rgb2lab(img)
|
| 1867 |
+
>>> img_lch = lab2lch(img_lab)
|
| 1868 |
+
"""
|
| 1869 |
+
lch = _prepare_lab_array(lab)
|
| 1870 |
+
|
| 1871 |
+
a, b = lch[..., 1], lch[..., 2]
|
| 1872 |
+
lch[..., 1], lch[..., 2] = _cart2polar_2pi(a, b)
|
| 1873 |
+
return lch
|
| 1874 |
+
|
| 1875 |
+
|
| 1876 |
+
def _cart2polar_2pi(x, y):
|
| 1877 |
+
"""convert cartesian coordinates to polar (uses non-standard theta range!)
|
| 1878 |
+
|
| 1879 |
+
NON-STANDARD RANGE! Maps to ``(0, 2*pi)`` rather than usual ``(-pi, +pi)``
|
| 1880 |
+
"""
|
| 1881 |
+
r, t = np.hypot(x, y), np.arctan2(y, x)
|
| 1882 |
+
t += np.where(t < 0.0, 2 * np.pi, 0)
|
| 1883 |
+
return r, t
|
| 1884 |
+
|
| 1885 |
+
|
| 1886 |
+
@channel_as_last_axis()
|
| 1887 |
+
def lch2lab(lch, *, channel_axis=-1):
|
| 1888 |
+
"""Convert image in CIE-LCh to CIE-LAB color space.
|
| 1889 |
+
|
| 1890 |
+
CIE-LCh is the cylindrical representation of the CIE-LAB (Cartesian) color
|
| 1891 |
+
space.
|
| 1892 |
+
|
| 1893 |
+
Parameters
|
| 1894 |
+
----------
|
| 1895 |
+
lch : (..., C=3, ...) array_like
|
| 1896 |
+
The input image in CIE-LCh color space.
|
| 1897 |
+
Unless `channel_axis` is set, the final dimension denotes the CIE-LAB
|
| 1898 |
+
channels.
|
| 1899 |
+
The L* values range from 0 to 100;
|
| 1900 |
+
the C values range from 0 to 100;
|
| 1901 |
+
the h values range from 0 to ``2*pi``.
|
| 1902 |
+
channel_axis : int, optional
|
| 1903 |
+
This parameter indicates which axis of the array corresponds to
|
| 1904 |
+
channels.
|
| 1905 |
+
|
| 1906 |
+
.. versionadded:: 0.19
|
| 1907 |
+
``channel_axis`` was added in 0.19.
|
| 1908 |
+
|
| 1909 |
+
Returns
|
| 1910 |
+
-------
|
| 1911 |
+
out : (..., C=3, ...) ndarray
|
| 1912 |
+
The image in CIE-LAB format, of same shape as input.
|
| 1913 |
+
|
| 1914 |
+
Raises
|
| 1915 |
+
------
|
| 1916 |
+
ValueError
|
| 1917 |
+
If `lch` does not have at least 3 channels (i.e., L*, C, and h).
|
| 1918 |
+
|
| 1919 |
+
Notes
|
| 1920 |
+
-----
|
| 1921 |
+
The h channel (i.e., hue) is expressed as an angle in range ``(0, 2*pi)``.
|
| 1922 |
+
|
| 1923 |
+
See Also
|
| 1924 |
+
--------
|
| 1925 |
+
lab2lch
|
| 1926 |
+
|
| 1927 |
+
References
|
| 1928 |
+
----------
|
| 1929 |
+
.. [1] http://www.easyrgb.com/en/math.php
|
| 1930 |
+
.. [2] https://en.wikipedia.org/wiki/HCL_color_space
|
| 1931 |
+
.. [3] https://en.wikipedia.org/wiki/CIELAB_color_space
|
| 1932 |
+
|
| 1933 |
+
Examples
|
| 1934 |
+
--------
|
| 1935 |
+
>>> from skimage import data
|
| 1936 |
+
>>> from skimage.color import rgb2lab, lch2lab, lab2lch
|
| 1937 |
+
>>> img = data.astronaut()
|
| 1938 |
+
>>> img_lab = rgb2lab(img)
|
| 1939 |
+
>>> img_lch = lab2lch(img_lab)
|
| 1940 |
+
>>> img_lab2 = lch2lab(img_lch)
|
| 1941 |
+
"""
|
| 1942 |
+
lch = _prepare_lab_array(lch)
|
| 1943 |
+
|
| 1944 |
+
c, h = lch[..., 1], lch[..., 2]
|
| 1945 |
+
lch[..., 1], lch[..., 2] = c * np.cos(h), c * np.sin(h)
|
| 1946 |
+
return lch
|
| 1947 |
+
|
| 1948 |
+
|
| 1949 |
+
def _prepare_lab_array(arr, force_copy=True):
|
| 1950 |
+
"""Ensure input for lab2lch and lch2lab is well-formed.
|
| 1951 |
+
|
| 1952 |
+
Input array must be in floating point and have at least 3 elements in the
|
| 1953 |
+
last dimension. Returns a new array by default.
|
| 1954 |
+
"""
|
| 1955 |
+
arr = np.asarray(arr)
|
| 1956 |
+
shape = arr.shape
|
| 1957 |
+
if shape[-1] < 3:
|
| 1958 |
+
raise ValueError('Input image has less than 3 channels.')
|
| 1959 |
+
float_dtype = _supported_float_type(arr.dtype)
|
| 1960 |
+
if float_dtype == np.float32:
|
| 1961 |
+
_func = dtype.img_as_float32
|
| 1962 |
+
else:
|
| 1963 |
+
_func = dtype.img_as_float64
|
| 1964 |
+
return _func(arr, force_copy=force_copy)
|
| 1965 |
+
|
| 1966 |
+
|
| 1967 |
+
@channel_as_last_axis()
|
| 1968 |
+
def rgb2yuv(rgb, *, channel_axis=-1):
|
| 1969 |
+
"""RGB to YUV color space conversion.
|
| 1970 |
+
|
| 1971 |
+
Parameters
|
| 1972 |
+
----------
|
| 1973 |
+
rgb : (..., C=3, ...) array_like
|
| 1974 |
+
The image in RGB format. By default, the final dimension denotes
|
| 1975 |
+
channels.
|
| 1976 |
+
channel_axis : int, optional
|
| 1977 |
+
This parameter indicates which axis of the array corresponds to
|
| 1978 |
+
channels.
|
| 1979 |
+
|
| 1980 |
+
.. versionadded:: 0.19
|
| 1981 |
+
``channel_axis`` was added in 0.19.
|
| 1982 |
+
|
| 1983 |
+
Returns
|
| 1984 |
+
-------
|
| 1985 |
+
out : (..., C=3, ...) ndarray
|
| 1986 |
+
The image in YUV format. Same dimensions as input.
|
| 1987 |
+
|
| 1988 |
+
Raises
|
| 1989 |
+
------
|
| 1990 |
+
ValueError
|
| 1991 |
+
If `rgb` is not at least 2-D with shape (..., C=3, ...).
|
| 1992 |
+
|
| 1993 |
+
Notes
|
| 1994 |
+
-----
|
| 1995 |
+
Y is between 0 and 1. Use YCbCr instead of YUV for the color space
|
| 1996 |
+
commonly used by video codecs, where Y ranges from 16 to 235.
|
| 1997 |
+
|
| 1998 |
+
References
|
| 1999 |
+
----------
|
| 2000 |
+
.. [1] https://en.wikipedia.org/wiki/YUV
|
| 2001 |
+
"""
|
| 2002 |
+
return _convert(yuv_from_rgb, rgb)
|
| 2003 |
+
|
| 2004 |
+
|
| 2005 |
+
@channel_as_last_axis()
|
| 2006 |
+
def rgb2yiq(rgb, *, channel_axis=-1):
|
| 2007 |
+
"""RGB to YIQ color space conversion.
|
| 2008 |
+
|
| 2009 |
+
Parameters
|
| 2010 |
+
----------
|
| 2011 |
+
rgb : (..., C=3, ...) array_like
|
| 2012 |
+
The image in RGB format. By default, the final dimension denotes
|
| 2013 |
+
channels.
|
| 2014 |
+
channel_axis : int, optional
|
| 2015 |
+
This parameter indicates which axis of the array corresponds to
|
| 2016 |
+
channels.
|
| 2017 |
+
|
| 2018 |
+
.. versionadded:: 0.19
|
| 2019 |
+
``channel_axis`` was added in 0.19.
|
| 2020 |
+
|
| 2021 |
+
Returns
|
| 2022 |
+
-------
|
| 2023 |
+
out : (..., C=3, ...) ndarray
|
| 2024 |
+
The image in YIQ format. Same dimensions as input.
|
| 2025 |
+
|
| 2026 |
+
Raises
|
| 2027 |
+
------
|
| 2028 |
+
ValueError
|
| 2029 |
+
If `rgb` is not at least 2-D with shape (..., C=3, ...).
|
| 2030 |
+
"""
|
| 2031 |
+
return _convert(yiq_from_rgb, rgb)
|
| 2032 |
+
|
| 2033 |
+
|
| 2034 |
+
@channel_as_last_axis()
|
| 2035 |
+
def rgb2ypbpr(rgb, *, channel_axis=-1):
|
| 2036 |
+
"""RGB to YPbPr color space conversion.
|
| 2037 |
+
|
| 2038 |
+
Parameters
|
| 2039 |
+
----------
|
| 2040 |
+
rgb : (..., C=3, ...) array_like
|
| 2041 |
+
The image in RGB format. By default, the final dimension denotes
|
| 2042 |
+
channels.
|
| 2043 |
+
channel_axis : int, optional
|
| 2044 |
+
This parameter indicates which axis of the array corresponds to
|
| 2045 |
+
channels.
|
| 2046 |
+
|
| 2047 |
+
.. versionadded:: 0.19
|
| 2048 |
+
``channel_axis`` was added in 0.19.
|
| 2049 |
+
|
| 2050 |
+
Returns
|
| 2051 |
+
-------
|
| 2052 |
+
out : (..., C=3, ...) ndarray
|
| 2053 |
+
The image in YPbPr format. Same dimensions as input.
|
| 2054 |
+
|
| 2055 |
+
Raises
|
| 2056 |
+
------
|
| 2057 |
+
ValueError
|
| 2058 |
+
If `rgb` is not at least 2-D with shape (..., C=3, ...).
|
| 2059 |
+
|
| 2060 |
+
References
|
| 2061 |
+
----------
|
| 2062 |
+
.. [1] https://en.wikipedia.org/wiki/YPbPr
|
| 2063 |
+
"""
|
| 2064 |
+
return _convert(ypbpr_from_rgb, rgb)
|
| 2065 |
+
|
| 2066 |
+
|
| 2067 |
+
@channel_as_last_axis()
|
| 2068 |
+
def rgb2ycbcr(rgb, *, channel_axis=-1):
|
| 2069 |
+
"""RGB to YCbCr color space conversion.
|
| 2070 |
+
|
| 2071 |
+
Parameters
|
| 2072 |
+
----------
|
| 2073 |
+
rgb : (..., C=3, ...) array_like
|
| 2074 |
+
The image in RGB format. By default, the final dimension denotes
|
| 2075 |
+
channels.
|
| 2076 |
+
channel_axis : int, optional
|
| 2077 |
+
This parameter indicates which axis of the array corresponds to
|
| 2078 |
+
channels.
|
| 2079 |
+
|
| 2080 |
+
.. versionadded:: 0.19
|
| 2081 |
+
``channel_axis`` was added in 0.19.
|
| 2082 |
+
|
| 2083 |
+
Returns
|
| 2084 |
+
-------
|
| 2085 |
+
out : (..., C=3, ...) ndarray
|
| 2086 |
+
The image in YCbCr format. Same dimensions as input.
|
| 2087 |
+
|
| 2088 |
+
Raises
|
| 2089 |
+
------
|
| 2090 |
+
ValueError
|
| 2091 |
+
If `rgb` is not at least 2-D with shape (..., C=3, ...).
|
| 2092 |
+
|
| 2093 |
+
Notes
|
| 2094 |
+
-----
|
| 2095 |
+
Y is between 16 and 235. This is the color space commonly used by video
|
| 2096 |
+
codecs; it is sometimes incorrectly called "YUV".
|
| 2097 |
+
|
| 2098 |
+
References
|
| 2099 |
+
----------
|
| 2100 |
+
.. [1] https://en.wikipedia.org/wiki/YCbCr
|
| 2101 |
+
"""
|
| 2102 |
+
arr = _convert(ycbcr_from_rgb, rgb)
|
| 2103 |
+
arr[..., 0] += 16
|
| 2104 |
+
arr[..., 1] += 128
|
| 2105 |
+
arr[..., 2] += 128
|
| 2106 |
+
return arr
|
| 2107 |
+
|
| 2108 |
+
|
| 2109 |
+
@channel_as_last_axis()
|
| 2110 |
+
def rgb2ydbdr(rgb, *, channel_axis=-1):
|
| 2111 |
+
"""RGB to YDbDr color space conversion.
|
| 2112 |
+
|
| 2113 |
+
Parameters
|
| 2114 |
+
----------
|
| 2115 |
+
rgb : (..., C=3, ...) array_like
|
| 2116 |
+
The image in RGB format. By default, the final dimension denotes
|
| 2117 |
+
channels.
|
| 2118 |
+
channel_axis : int, optional
|
| 2119 |
+
This parameter indicates which axis of the array corresponds to
|
| 2120 |
+
channels.
|
| 2121 |
+
|
| 2122 |
+
.. versionadded:: 0.19
|
| 2123 |
+
``channel_axis`` was added in 0.19.
|
| 2124 |
+
|
| 2125 |
+
Returns
|
| 2126 |
+
-------
|
| 2127 |
+
out : (..., C=3, ...) ndarray
|
| 2128 |
+
The image in YDbDr format. Same dimensions as input.
|
| 2129 |
+
|
| 2130 |
+
Raises
|
| 2131 |
+
------
|
| 2132 |
+
ValueError
|
| 2133 |
+
If `rgb` is not at least 2-D with shape (..., C=3, ...).
|
| 2134 |
+
|
| 2135 |
+
Notes
|
| 2136 |
+
-----
|
| 2137 |
+
This is the color space commonly used by video codecs. It is also the
|
| 2138 |
+
reversible color transform in JPEG2000.
|
| 2139 |
+
|
| 2140 |
+
References
|
| 2141 |
+
----------
|
| 2142 |
+
.. [1] https://en.wikipedia.org/wiki/YDbDr
|
| 2143 |
+
"""
|
| 2144 |
+
arr = _convert(ydbdr_from_rgb, rgb)
|
| 2145 |
+
return arr
|
| 2146 |
+
|
| 2147 |
+
|
| 2148 |
+
@channel_as_last_axis()
|
| 2149 |
+
def yuv2rgb(yuv, *, channel_axis=-1):
|
| 2150 |
+
"""YUV to RGB color space conversion.
|
| 2151 |
+
|
| 2152 |
+
Parameters
|
| 2153 |
+
----------
|
| 2154 |
+
yuv : (..., C=3, ...) array_like
|
| 2155 |
+
The image in YUV format. By default, the final dimension denotes
|
| 2156 |
+
channels.
|
| 2157 |
+
|
| 2158 |
+
Returns
|
| 2159 |
+
-------
|
| 2160 |
+
out : (..., C=3, ...) ndarray
|
| 2161 |
+
The image in RGB format. Same dimensions as input.
|
| 2162 |
+
|
| 2163 |
+
Raises
|
| 2164 |
+
------
|
| 2165 |
+
ValueError
|
| 2166 |
+
If `yuv` is not at least 2-D with shape (..., C=3, ...).
|
| 2167 |
+
|
| 2168 |
+
References
|
| 2169 |
+
----------
|
| 2170 |
+
.. [1] https://en.wikipedia.org/wiki/YUV
|
| 2171 |
+
"""
|
| 2172 |
+
return _convert(rgb_from_yuv, yuv)
|
| 2173 |
+
|
| 2174 |
+
|
| 2175 |
+
@channel_as_last_axis()
|
| 2176 |
+
def yiq2rgb(yiq, *, channel_axis=-1):
|
| 2177 |
+
"""YIQ to RGB color space conversion.
|
| 2178 |
+
|
| 2179 |
+
Parameters
|
| 2180 |
+
----------
|
| 2181 |
+
yiq : (..., C=3, ...) array_like
|
| 2182 |
+
The image in YIQ format. By default, the final dimension denotes
|
| 2183 |
+
channels.
|
| 2184 |
+
channel_axis : int, optional
|
| 2185 |
+
This parameter indicates which axis of the array corresponds to
|
| 2186 |
+
channels.
|
| 2187 |
+
|
| 2188 |
+
.. versionadded:: 0.19
|
| 2189 |
+
``channel_axis`` was added in 0.19.
|
| 2190 |
+
|
| 2191 |
+
Returns
|
| 2192 |
+
-------
|
| 2193 |
+
out : (..., C=3, ...) ndarray
|
| 2194 |
+
The image in RGB format. Same dimensions as input.
|
| 2195 |
+
|
| 2196 |
+
Raises
|
| 2197 |
+
------
|
| 2198 |
+
ValueError
|
| 2199 |
+
If `yiq` is not at least 2-D with shape (..., C=3, ...).
|
| 2200 |
+
"""
|
| 2201 |
+
return _convert(rgb_from_yiq, yiq)
|
| 2202 |
+
|
| 2203 |
+
|
| 2204 |
+
@channel_as_last_axis()
|
| 2205 |
+
def ypbpr2rgb(ypbpr, *, channel_axis=-1):
|
| 2206 |
+
"""YPbPr to RGB color space conversion.
|
| 2207 |
+
|
| 2208 |
+
Parameters
|
| 2209 |
+
----------
|
| 2210 |
+
ypbpr : (..., C=3, ...) array_like
|
| 2211 |
+
The image in YPbPr format. By default, the final dimension denotes
|
| 2212 |
+
channels.
|
| 2213 |
+
channel_axis : int, optional
|
| 2214 |
+
This parameter indicates which axis of the array corresponds to
|
| 2215 |
+
channels.
|
| 2216 |
+
|
| 2217 |
+
.. versionadded:: 0.19
|
| 2218 |
+
``channel_axis`` was added in 0.19.
|
| 2219 |
+
|
| 2220 |
+
Returns
|
| 2221 |
+
-------
|
| 2222 |
+
out : (..., C=3, ...) ndarray
|
| 2223 |
+
The image in RGB format. Same dimensions as input.
|
| 2224 |
+
|
| 2225 |
+
Raises
|
| 2226 |
+
------
|
| 2227 |
+
ValueError
|
| 2228 |
+
If `ypbpr` is not at least 2-D with shape (..., C=3, ...).
|
| 2229 |
+
|
| 2230 |
+
References
|
| 2231 |
+
----------
|
| 2232 |
+
.. [1] https://en.wikipedia.org/wiki/YPbPr
|
| 2233 |
+
"""
|
| 2234 |
+
return _convert(rgb_from_ypbpr, ypbpr)
|
| 2235 |
+
|
| 2236 |
+
|
| 2237 |
+
@channel_as_last_axis()
|
| 2238 |
+
def ycbcr2rgb(ycbcr, *, channel_axis=-1):
|
| 2239 |
+
"""YCbCr to RGB color space conversion.
|
| 2240 |
+
|
| 2241 |
+
Parameters
|
| 2242 |
+
----------
|
| 2243 |
+
ycbcr : (..., C=3, ...) array_like
|
| 2244 |
+
The image in YCbCr format. By default, the final dimension denotes
|
| 2245 |
+
channels.
|
| 2246 |
+
channel_axis : int, optional
|
| 2247 |
+
This parameter indicates which axis of the array corresponds to
|
| 2248 |
+
channels.
|
| 2249 |
+
|
| 2250 |
+
.. versionadded:: 0.19
|
| 2251 |
+
``channel_axis`` was added in 0.19.
|
| 2252 |
+
|
| 2253 |
+
Returns
|
| 2254 |
+
-------
|
| 2255 |
+
out : (..., C=3, ...) ndarray
|
| 2256 |
+
The image in RGB format. Same dimensions as input.
|
| 2257 |
+
|
| 2258 |
+
Raises
|
| 2259 |
+
------
|
| 2260 |
+
ValueError
|
| 2261 |
+
If `ycbcr` is not at least 2-D with shape (..., C=3, ...).
|
| 2262 |
+
|
| 2263 |
+
Notes
|
| 2264 |
+
-----
|
| 2265 |
+
Y is between 16 and 235. This is the color space commonly used by video
|
| 2266 |
+
codecs; it is sometimes incorrectly called "YUV".
|
| 2267 |
+
|
| 2268 |
+
References
|
| 2269 |
+
----------
|
| 2270 |
+
.. [1] https://en.wikipedia.org/wiki/YCbCr
|
| 2271 |
+
"""
|
| 2272 |
+
arr = ycbcr.copy()
|
| 2273 |
+
arr[..., 0] -= 16
|
| 2274 |
+
arr[..., 1] -= 128
|
| 2275 |
+
arr[..., 2] -= 128
|
| 2276 |
+
return _convert(rgb_from_ycbcr, arr)
|
| 2277 |
+
|
| 2278 |
+
|
| 2279 |
+
@channel_as_last_axis()
|
| 2280 |
+
def ydbdr2rgb(ydbdr, *, channel_axis=-1):
|
| 2281 |
+
"""YDbDr to RGB color space conversion.
|
| 2282 |
+
|
| 2283 |
+
Parameters
|
| 2284 |
+
----------
|
| 2285 |
+
ydbdr : (..., C=3, ...) array_like
|
| 2286 |
+
The image in YDbDr format. By default, the final dimension denotes
|
| 2287 |
+
channels.
|
| 2288 |
+
channel_axis : int, optional
|
| 2289 |
+
This parameter indicates which axis of the array corresponds to
|
| 2290 |
+
channels.
|
| 2291 |
+
|
| 2292 |
+
.. versionadded:: 0.19
|
| 2293 |
+
``channel_axis`` was added in 0.19.
|
| 2294 |
+
|
| 2295 |
+
Returns
|
| 2296 |
+
-------
|
| 2297 |
+
out : (..., C=3, ...) ndarray
|
| 2298 |
+
The image in RGB format. Same dimensions as input.
|
| 2299 |
+
|
| 2300 |
+
Raises
|
| 2301 |
+
------
|
| 2302 |
+
ValueError
|
| 2303 |
+
If `ydbdr` is not at least 2-D with shape (..., C=3, ...).
|
| 2304 |
+
|
| 2305 |
+
Notes
|
| 2306 |
+
-----
|
| 2307 |
+
This is the color space commonly used by video codecs, also called the
|
| 2308 |
+
reversible color transform in JPEG2000.
|
| 2309 |
+
|
| 2310 |
+
References
|
| 2311 |
+
----------
|
| 2312 |
+
.. [1] https://en.wikipedia.org/wiki/YDbDr
|
| 2313 |
+
"""
|
| 2314 |
+
return _convert(rgb_from_ydbdr, ydbdr)
|
envs/kitoverlay/skimage/color/colorlabel.py
ADDED
|
@@ -0,0 +1,299 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import itertools
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
from .._shared.utils import _supported_float_type, warn
|
| 6 |
+
from ..util import img_as_float
|
| 7 |
+
from . import rgb_colors
|
| 8 |
+
from .colorconv import gray2rgb, rgb2hsv, hsv2rgb
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
__all__ = ['color_dict', 'label2rgb', 'DEFAULT_COLORS']
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
DEFAULT_COLORS = (
|
| 15 |
+
'red',
|
| 16 |
+
'blue',
|
| 17 |
+
'yellow',
|
| 18 |
+
'magenta',
|
| 19 |
+
'green',
|
| 20 |
+
'indigo',
|
| 21 |
+
'darkorange',
|
| 22 |
+
'cyan',
|
| 23 |
+
'pink',
|
| 24 |
+
'yellowgreen',
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
color_dict = {k: v for k, v in rgb_colors.__dict__.items() if isinstance(v, tuple)}
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def _rgb_vector(color):
|
| 32 |
+
"""Return RGB color as (1, 3) array.
|
| 33 |
+
|
| 34 |
+
This RGB array gets multiplied by masked regions of an RGB image, which are
|
| 35 |
+
partially flattened by masking (i.e. dimensions 2D + RGB -> 1D + RGB).
|
| 36 |
+
|
| 37 |
+
Parameters
|
| 38 |
+
----------
|
| 39 |
+
color : str or array
|
| 40 |
+
Color name in ``skimage.color.color_dict`` or RGB float values between [0, 1].
|
| 41 |
+
"""
|
| 42 |
+
if isinstance(color, str):
|
| 43 |
+
color = color_dict[color]
|
| 44 |
+
# Slice to handle RGBA colors.
|
| 45 |
+
return np.array(color[:3])
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def _match_label_with_color(label, colors, bg_label, bg_color):
|
| 49 |
+
"""Return `unique_labels` and `color_cycle` for label array and color list.
|
| 50 |
+
|
| 51 |
+
Colors are cycled for normal labels, but the background color should only
|
| 52 |
+
be used for the background.
|
| 53 |
+
"""
|
| 54 |
+
# Temporarily set background color; it will be removed later.
|
| 55 |
+
if bg_color is None:
|
| 56 |
+
bg_color = (0, 0, 0)
|
| 57 |
+
bg_color = _rgb_vector(bg_color)
|
| 58 |
+
|
| 59 |
+
# map labels to their ranks among all labels from small to large
|
| 60 |
+
unique_labels, mapped_labels = np.unique(label, return_inverse=True)
|
| 61 |
+
# unique_inverse is no longer flat in NumPy 2.0
|
| 62 |
+
mapped_labels = mapped_labels.reshape(-1)
|
| 63 |
+
|
| 64 |
+
# get rank of bg_label
|
| 65 |
+
bg_label_rank_list = mapped_labels[label.flat == bg_label]
|
| 66 |
+
|
| 67 |
+
# The rank of each label is the index of the color it is matched to in
|
| 68 |
+
# color cycle. bg_label should always be mapped to the first color, so
|
| 69 |
+
# its rank must be 0. Other labels should be ranked from small to large
|
| 70 |
+
# from 1.
|
| 71 |
+
if len(bg_label_rank_list) > 0:
|
| 72 |
+
bg_label_rank = bg_label_rank_list[0]
|
| 73 |
+
mapped_labels[mapped_labels < bg_label_rank] += 1
|
| 74 |
+
mapped_labels[label.flat == bg_label] = 0
|
| 75 |
+
else:
|
| 76 |
+
mapped_labels += 1
|
| 77 |
+
|
| 78 |
+
# Modify labels and color cycle so background color is used only once.
|
| 79 |
+
color_cycle = itertools.cycle(colors)
|
| 80 |
+
color_cycle = itertools.chain([bg_color], color_cycle)
|
| 81 |
+
|
| 82 |
+
return mapped_labels, color_cycle
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def label2rgb(
|
| 86 |
+
label,
|
| 87 |
+
image=None,
|
| 88 |
+
colors=None,
|
| 89 |
+
alpha=0.3,
|
| 90 |
+
bg_label=0,
|
| 91 |
+
bg_color=(0, 0, 0),
|
| 92 |
+
image_alpha=1,
|
| 93 |
+
kind='overlay',
|
| 94 |
+
*,
|
| 95 |
+
saturation=0,
|
| 96 |
+
channel_axis=-1,
|
| 97 |
+
):
|
| 98 |
+
"""Return an RGB image where color-coded labels are painted over the image.
|
| 99 |
+
|
| 100 |
+
Parameters
|
| 101 |
+
----------
|
| 102 |
+
label : ndarray
|
| 103 |
+
Integer array of labels with the same shape as `image`.
|
| 104 |
+
image : ndarray, optional
|
| 105 |
+
Image used as underlay for labels. It should have the same shape as
|
| 106 |
+
`labels`, optionally with an additional RGB (channels) axis. If `image`
|
| 107 |
+
is an RGB image, it is converted to grayscale before coloring.
|
| 108 |
+
colors : list, optional
|
| 109 |
+
List of colors. If the number of labels exceeds the number of colors,
|
| 110 |
+
then the colors are cycled.
|
| 111 |
+
alpha : float [0, 1], optional
|
| 112 |
+
Opacity of colorized labels. Ignored if image is `None`.
|
| 113 |
+
bg_label : int, optional
|
| 114 |
+
Label that's treated as the background. If `bg_label` is specified,
|
| 115 |
+
`bg_color` is `None`, and `kind` is `overlay`,
|
| 116 |
+
background is not painted by any colors.
|
| 117 |
+
bg_color : str or array, optional
|
| 118 |
+
Background color. Must be a name in ``skimage.color.color_dict`` or RGB float
|
| 119 |
+
values between [0, 1].
|
| 120 |
+
image_alpha : float [0, 1], optional
|
| 121 |
+
Opacity of the image.
|
| 122 |
+
kind : string, one of {'overlay', 'avg'}
|
| 123 |
+
The kind of color image desired. 'overlay' cycles over defined colors
|
| 124 |
+
and overlays the colored labels over the original image. 'avg' replaces
|
| 125 |
+
each labeled segment with its average color, for a stained-class or
|
| 126 |
+
pastel painting appearance.
|
| 127 |
+
saturation : float [0, 1], optional
|
| 128 |
+
Parameter to control the saturation applied to the original image
|
| 129 |
+
between fully saturated (original RGB, `saturation=1`) and fully
|
| 130 |
+
unsaturated (grayscale, `saturation=0`). Only applies when
|
| 131 |
+
`kind='overlay'`.
|
| 132 |
+
channel_axis : int, optional
|
| 133 |
+
This parameter indicates which axis of the output array will correspond
|
| 134 |
+
to channels. If `image` is provided, this must also match the axis of
|
| 135 |
+
`image` that corresponds to channels.
|
| 136 |
+
|
| 137 |
+
.. versionadded:: 0.19
|
| 138 |
+
``channel_axis`` was added in 0.19.
|
| 139 |
+
|
| 140 |
+
Returns
|
| 141 |
+
-------
|
| 142 |
+
result : ndarray of float, same shape as `image`
|
| 143 |
+
The result of blending a cycling colormap (`colors`) for each distinct
|
| 144 |
+
value in `label` with the image, at a certain alpha value.
|
| 145 |
+
"""
|
| 146 |
+
if image is not None:
|
| 147 |
+
image = np.moveaxis(image, source=channel_axis, destination=-1)
|
| 148 |
+
if kind == 'overlay':
|
| 149 |
+
rgb = _label2rgb_overlay(
|
| 150 |
+
label, image, colors, alpha, bg_label, bg_color, image_alpha, saturation
|
| 151 |
+
)
|
| 152 |
+
elif kind == 'avg':
|
| 153 |
+
rgb = _label2rgb_avg(label, image, bg_label, bg_color)
|
| 154 |
+
else:
|
| 155 |
+
raise ValueError("`kind` must be either 'overlay' or 'avg'.")
|
| 156 |
+
return np.moveaxis(rgb, source=-1, destination=channel_axis)
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
def _label2rgb_overlay(
|
| 160 |
+
label,
|
| 161 |
+
image=None,
|
| 162 |
+
colors=None,
|
| 163 |
+
alpha=0.3,
|
| 164 |
+
bg_label=-1,
|
| 165 |
+
bg_color=None,
|
| 166 |
+
image_alpha=1,
|
| 167 |
+
saturation=0,
|
| 168 |
+
):
|
| 169 |
+
"""Return an RGB image where color-coded labels are painted over the image.
|
| 170 |
+
|
| 171 |
+
Parameters
|
| 172 |
+
----------
|
| 173 |
+
label : ndarray
|
| 174 |
+
Integer array of labels with the same shape as `image`.
|
| 175 |
+
image : ndarray, optional
|
| 176 |
+
Image used as underlay for labels. It should have the same shape as
|
| 177 |
+
`labels`, optionally with an additional RGB (channels) axis. If `image`
|
| 178 |
+
is an RGB image, it is converted to grayscale before coloring.
|
| 179 |
+
colors : list, optional
|
| 180 |
+
List of colors. If the number of labels exceeds the number of colors,
|
| 181 |
+
then the colors are cycled.
|
| 182 |
+
alpha : float [0, 1], optional
|
| 183 |
+
Opacity of colorized labels. Ignored if image is `None`.
|
| 184 |
+
bg_label : int, optional
|
| 185 |
+
Label that's treated as the background. If `bg_label` is specified and
|
| 186 |
+
`bg_color` is `None`, background is not painted by any colors.
|
| 187 |
+
bg_color : str or array, optional
|
| 188 |
+
Background color. Must be a name in ``skimage.color.color_dict`` or RGB float
|
| 189 |
+
values between [0, 1].
|
| 190 |
+
image_alpha : float [0, 1], optional
|
| 191 |
+
Opacity of the image.
|
| 192 |
+
saturation : float [0, 1], optional
|
| 193 |
+
Parameter to control the saturation applied to the original image
|
| 194 |
+
between fully saturated (original RGB, `saturation=1`) and fully
|
| 195 |
+
unsaturated (grayscale, `saturation=0`).
|
| 196 |
+
|
| 197 |
+
Returns
|
| 198 |
+
-------
|
| 199 |
+
result : ndarray of float, same shape as `image`
|
| 200 |
+
The result of blending a cycling colormap (`colors`) for each distinct
|
| 201 |
+
value in `label` with the image, at a certain alpha value.
|
| 202 |
+
"""
|
| 203 |
+
if not 0 <= saturation <= 1:
|
| 204 |
+
warn(f'saturation must be in range [0, 1], got {saturation}')
|
| 205 |
+
|
| 206 |
+
if colors is None:
|
| 207 |
+
colors = DEFAULT_COLORS
|
| 208 |
+
colors = [_rgb_vector(c) for c in colors]
|
| 209 |
+
|
| 210 |
+
if image is None:
|
| 211 |
+
image = np.zeros(label.shape + (3,), dtype=np.float64)
|
| 212 |
+
# Opacity doesn't make sense if no image exists.
|
| 213 |
+
alpha = 1
|
| 214 |
+
else:
|
| 215 |
+
if image.shape[: label.ndim] != label.shape or image.ndim > label.ndim + 1:
|
| 216 |
+
raise ValueError("`image` and `label` must be the same shape")
|
| 217 |
+
|
| 218 |
+
if image.ndim == label.ndim + 1 and image.shape[-1] != 3:
|
| 219 |
+
raise ValueError("`image` must be RGB (image.shape[-1] must be 3).")
|
| 220 |
+
|
| 221 |
+
if image.min() < 0:
|
| 222 |
+
warn("Negative intensities in `image` are not supported")
|
| 223 |
+
|
| 224 |
+
float_dtype = _supported_float_type(image.dtype)
|
| 225 |
+
image = img_as_float(image).astype(float_dtype, copy=False)
|
| 226 |
+
if image.ndim > label.ndim:
|
| 227 |
+
hsv = rgb2hsv(image)
|
| 228 |
+
hsv[..., 1] *= saturation
|
| 229 |
+
image = hsv2rgb(hsv)
|
| 230 |
+
elif image.ndim == label.ndim:
|
| 231 |
+
image = gray2rgb(image)
|
| 232 |
+
image = image * image_alpha + (1 - image_alpha)
|
| 233 |
+
|
| 234 |
+
# Ensure that all labels are non-negative so we can index into
|
| 235 |
+
# `label_to_color` correctly.
|
| 236 |
+
offset = min(label.min(), bg_label)
|
| 237 |
+
if offset != 0:
|
| 238 |
+
label = label - offset # Make sure you don't modify the input array.
|
| 239 |
+
bg_label -= offset
|
| 240 |
+
|
| 241 |
+
new_type = np.min_scalar_type(int(label.max()))
|
| 242 |
+
if new_type == bool:
|
| 243 |
+
new_type = np.uint8
|
| 244 |
+
label = label.astype(new_type)
|
| 245 |
+
|
| 246 |
+
mapped_labels_flat, color_cycle = _match_label_with_color(
|
| 247 |
+
label, colors, bg_label, bg_color
|
| 248 |
+
)
|
| 249 |
+
|
| 250 |
+
if len(mapped_labels_flat) == 0:
|
| 251 |
+
return image
|
| 252 |
+
|
| 253 |
+
dense_labels = range(np.max(mapped_labels_flat) + 1)
|
| 254 |
+
|
| 255 |
+
label_to_color = np.stack([c for i, c in zip(dense_labels, color_cycle)])
|
| 256 |
+
|
| 257 |
+
mapped_labels = label
|
| 258 |
+
mapped_labels.flat = mapped_labels_flat
|
| 259 |
+
result = label_to_color[mapped_labels] * alpha + image * (1 - alpha)
|
| 260 |
+
|
| 261 |
+
# Remove background label if its color was not specified.
|
| 262 |
+
remove_background = 0 in mapped_labels_flat and bg_color is None
|
| 263 |
+
if remove_background:
|
| 264 |
+
result[label == bg_label] = image[label == bg_label]
|
| 265 |
+
|
| 266 |
+
return result
|
| 267 |
+
|
| 268 |
+
|
| 269 |
+
def _label2rgb_avg(label_field, image, bg_label=0, bg_color=(0, 0, 0)):
|
| 270 |
+
"""Visualise each segment in `label_field` with its mean color in `image`.
|
| 271 |
+
|
| 272 |
+
Parameters
|
| 273 |
+
----------
|
| 274 |
+
label_field : ndarray of int
|
| 275 |
+
A segmentation of an image.
|
| 276 |
+
image : array, shape ``label_field.shape + (3,)``
|
| 277 |
+
A color image of the same spatial shape as `label_field`.
|
| 278 |
+
bg_label : int, optional
|
| 279 |
+
A value in `label_field` to be treated as background.
|
| 280 |
+
bg_color : 3-tuple of int, optional
|
| 281 |
+
The color for the background label
|
| 282 |
+
|
| 283 |
+
Returns
|
| 284 |
+
-------
|
| 285 |
+
out : ndarray, same shape and type as `image`
|
| 286 |
+
The output visualization.
|
| 287 |
+
"""
|
| 288 |
+
out = np.zeros(label_field.shape + (3,), dtype=image.dtype)
|
| 289 |
+
labels = np.unique(label_field)
|
| 290 |
+
bg = labels == bg_label
|
| 291 |
+
if bg.any():
|
| 292 |
+
labels = labels[labels != bg_label]
|
| 293 |
+
mask = (label_field == bg_label).nonzero()
|
| 294 |
+
out[mask] = bg_color
|
| 295 |
+
for label in labels:
|
| 296 |
+
mask = (label_field == label).nonzero()
|
| 297 |
+
color = image[mask].mean(axis=0)
|
| 298 |
+
out[mask] = color
|
| 299 |
+
return out
|
envs/kitoverlay/skimage/color/delta_e.py
ADDED
|
@@ -0,0 +1,393 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Functions for calculating the "distance" between colors.
|
| 3 |
+
|
| 4 |
+
Implicit in these definitions of "distance" is the notion of "Just Noticeable
|
| 5 |
+
Distance" (JND). This represents the distance between colors where a human can
|
| 6 |
+
perceive different colors. Humans are more sensitive to certain colors than
|
| 7 |
+
others, which different deltaE metrics correct for with varying degrees of
|
| 8 |
+
sophistication.
|
| 9 |
+
|
| 10 |
+
The literature often mentions 1 as the minimum distance for visual
|
| 11 |
+
differentiation, but more recent studies (Mahy 1994) peg JND at 2.3
|
| 12 |
+
|
| 13 |
+
The delta-E notation comes from the German word for "Sensation" (Empfindung).
|
| 14 |
+
|
| 15 |
+
References
|
| 16 |
+
----------
|
| 17 |
+
.. [1] https://en.wikipedia.org/wiki/Color_difference
|
| 18 |
+
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
+
import numpy as np
|
| 22 |
+
|
| 23 |
+
from .._shared.utils import _supported_float_type
|
| 24 |
+
from .colorconv import lab2lch, _cart2polar_2pi
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def _float_inputs(lab1, lab2, allow_float32=True):
|
| 28 |
+
lab1 = np.asarray(lab1)
|
| 29 |
+
lab2 = np.asarray(lab2)
|
| 30 |
+
if allow_float32:
|
| 31 |
+
float_dtype = _supported_float_type((lab1.dtype, lab2.dtype))
|
| 32 |
+
else:
|
| 33 |
+
float_dtype = np.float64
|
| 34 |
+
lab1 = lab1.astype(float_dtype, copy=False)
|
| 35 |
+
lab2 = lab2.astype(float_dtype, copy=False)
|
| 36 |
+
return lab1, lab2
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def deltaE_cie76(lab1, lab2, channel_axis=-1):
|
| 40 |
+
"""Euclidean distance between two points in Lab color space
|
| 41 |
+
|
| 42 |
+
Parameters
|
| 43 |
+
----------
|
| 44 |
+
lab1 : array_like
|
| 45 |
+
reference color (Lab colorspace)
|
| 46 |
+
lab2 : array_like
|
| 47 |
+
comparison color (Lab colorspace)
|
| 48 |
+
channel_axis : int, optional
|
| 49 |
+
This parameter indicates which axis of the arrays corresponds to
|
| 50 |
+
channels.
|
| 51 |
+
|
| 52 |
+
.. versionadded:: 0.19
|
| 53 |
+
``channel_axis`` was added in 0.19.
|
| 54 |
+
|
| 55 |
+
Returns
|
| 56 |
+
-------
|
| 57 |
+
dE : array_like
|
| 58 |
+
distance between colors `lab1` and `lab2`
|
| 59 |
+
|
| 60 |
+
References
|
| 61 |
+
----------
|
| 62 |
+
.. [1] https://en.wikipedia.org/wiki/Color_difference
|
| 63 |
+
.. [2] A. R. Robertson, "The CIE 1976 color-difference formulae,"
|
| 64 |
+
Color Res. Appl. 2, 7-11 (1977).
|
| 65 |
+
"""
|
| 66 |
+
lab1, lab2 = _float_inputs(lab1, lab2, allow_float32=True)
|
| 67 |
+
L1, a1, b1 = np.moveaxis(lab1, source=channel_axis, destination=0)[:3]
|
| 68 |
+
L2, a2, b2 = np.moveaxis(lab2, source=channel_axis, destination=0)[:3]
|
| 69 |
+
return np.sqrt((L2 - L1) ** 2 + (a2 - a1) ** 2 + (b2 - b1) ** 2)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def deltaE_ciede94(
|
| 73 |
+
lab1, lab2, kH=1, kC=1, kL=1, k1=0.045, k2=0.015, *, channel_axis=-1
|
| 74 |
+
):
|
| 75 |
+
"""Color difference according to CIEDE 94 standard
|
| 76 |
+
|
| 77 |
+
Accommodates perceptual non-uniformities through the use of application
|
| 78 |
+
specific scale factors (`kH`, `kC`, `kL`, `k1`, and `k2`).
|
| 79 |
+
|
| 80 |
+
Parameters
|
| 81 |
+
----------
|
| 82 |
+
lab1 : array_like
|
| 83 |
+
reference color (Lab colorspace)
|
| 84 |
+
lab2 : array_like
|
| 85 |
+
comparison color (Lab colorspace)
|
| 86 |
+
kH : float, optional
|
| 87 |
+
Hue scale
|
| 88 |
+
kC : float, optional
|
| 89 |
+
Chroma scale
|
| 90 |
+
kL : float, optional
|
| 91 |
+
Lightness scale
|
| 92 |
+
k1 : float, optional
|
| 93 |
+
first scale parameter
|
| 94 |
+
k2 : float, optional
|
| 95 |
+
second scale parameter
|
| 96 |
+
channel_axis : int, optional
|
| 97 |
+
This parameter indicates which axis of the arrays corresponds to
|
| 98 |
+
channels.
|
| 99 |
+
|
| 100 |
+
.. versionadded:: 0.19
|
| 101 |
+
``channel_axis`` was added in 0.19.
|
| 102 |
+
|
| 103 |
+
Returns
|
| 104 |
+
-------
|
| 105 |
+
dE : array_like
|
| 106 |
+
color difference between `lab1` and `lab2`
|
| 107 |
+
|
| 108 |
+
Notes
|
| 109 |
+
-----
|
| 110 |
+
deltaE_ciede94 is not symmetric with respect to lab1 and lab2. CIEDE94
|
| 111 |
+
defines the scales for the lightness, hue, and chroma in terms of the first
|
| 112 |
+
color. Consequently, the first color should be regarded as the "reference"
|
| 113 |
+
color.
|
| 114 |
+
|
| 115 |
+
`kL`, `k1`, `k2` depend on the application and default to the values
|
| 116 |
+
suggested for graphic arts
|
| 117 |
+
|
| 118 |
+
========== ============== ==========
|
| 119 |
+
Parameter Graphic Arts Textiles
|
| 120 |
+
========== ============== ==========
|
| 121 |
+
`kL` 1.000 2.000
|
| 122 |
+
`k1` 0.045 0.048
|
| 123 |
+
`k2` 0.015 0.014
|
| 124 |
+
========== ============== ==========
|
| 125 |
+
|
| 126 |
+
References
|
| 127 |
+
----------
|
| 128 |
+
.. [1] https://en.wikipedia.org/wiki/Color_difference
|
| 129 |
+
.. [2] http://www.brucelindbloom.com/index.html?Eqn_DeltaE_CIE94.html
|
| 130 |
+
"""
|
| 131 |
+
lab1, lab2 = _float_inputs(lab1, lab2, allow_float32=True)
|
| 132 |
+
lab1 = np.moveaxis(lab1, source=channel_axis, destination=0)
|
| 133 |
+
lab2 = np.moveaxis(lab2, source=channel_axis, destination=0)
|
| 134 |
+
|
| 135 |
+
L1, C1 = lab2lch(lab1, channel_axis=0)[:2]
|
| 136 |
+
L2, C2 = lab2lch(lab2, channel_axis=0)[:2]
|
| 137 |
+
|
| 138 |
+
dL = L1 - L2
|
| 139 |
+
dC = C1 - C2
|
| 140 |
+
dH2 = get_dH2(lab1, lab2, channel_axis=0)
|
| 141 |
+
|
| 142 |
+
SL = 1
|
| 143 |
+
SC = 1 + k1 * C1
|
| 144 |
+
SH = 1 + k2 * C1
|
| 145 |
+
|
| 146 |
+
dE2 = (dL / (kL * SL)) ** 2
|
| 147 |
+
dE2 += (dC / (kC * SC)) ** 2
|
| 148 |
+
dE2 += dH2 / (kH * SH) ** 2
|
| 149 |
+
return np.sqrt(np.maximum(dE2, 0))
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
def deltaE_ciede2000(lab1, lab2, kL=1, kC=1, kH=1, *, channel_axis=-1):
|
| 153 |
+
"""Color difference as given by the CIEDE 2000 standard.
|
| 154 |
+
|
| 155 |
+
CIEDE 2000 is a major revision of CIDE94. The perceptual calibration is
|
| 156 |
+
largely based on experience with automotive paint on smooth surfaces.
|
| 157 |
+
|
| 158 |
+
Parameters
|
| 159 |
+
----------
|
| 160 |
+
lab1 : array_like
|
| 161 |
+
reference color (Lab colorspace)
|
| 162 |
+
lab2 : array_like
|
| 163 |
+
comparison color (Lab colorspace)
|
| 164 |
+
kL : float (range), optional
|
| 165 |
+
lightness scale factor, 1 for "acceptably close"; 2 for "imperceptible"
|
| 166 |
+
see deltaE_cmc
|
| 167 |
+
kC : float (range), optional
|
| 168 |
+
chroma scale factor, usually 1
|
| 169 |
+
kH : float (range), optional
|
| 170 |
+
hue scale factor, usually 1
|
| 171 |
+
channel_axis : int, optional
|
| 172 |
+
This parameter indicates which axis of the arrays corresponds to
|
| 173 |
+
channels.
|
| 174 |
+
|
| 175 |
+
.. versionadded:: 0.19
|
| 176 |
+
``channel_axis`` was added in 0.19.
|
| 177 |
+
|
| 178 |
+
Returns
|
| 179 |
+
-------
|
| 180 |
+
deltaE : array_like
|
| 181 |
+
The distance between `lab1` and `lab2`
|
| 182 |
+
|
| 183 |
+
Notes
|
| 184 |
+
-----
|
| 185 |
+
CIEDE 2000 assumes parametric weighting factors for the lightness, chroma,
|
| 186 |
+
and hue (`kL`, `kC`, `kH` respectively). These default to 1.
|
| 187 |
+
|
| 188 |
+
References
|
| 189 |
+
----------
|
| 190 |
+
.. [1] https://en.wikipedia.org/wiki/Color_difference
|
| 191 |
+
.. [2] http://www.ece.rochester.edu/~gsharma/ciede2000/ciede2000noteCRNA.pdf
|
| 192 |
+
:DOI:`10.1364/AO.33.008069`
|
| 193 |
+
.. [3] M. Melgosa, J. Quesada, and E. Hita, "Uniformity of some recent
|
| 194 |
+
color metrics tested with an accurate color-difference tolerance
|
| 195 |
+
dataset," Appl. Opt. 33, 8069-8077 (1994).
|
| 196 |
+
"""
|
| 197 |
+
lab1, lab2 = _float_inputs(lab1, lab2, allow_float32=True)
|
| 198 |
+
|
| 199 |
+
channel_axis = channel_axis % lab1.ndim
|
| 200 |
+
unroll = False
|
| 201 |
+
if lab1.ndim == 1 and lab2.ndim == 1:
|
| 202 |
+
unroll = True
|
| 203 |
+
if lab1.ndim == 1:
|
| 204 |
+
lab1 = lab1[None, :]
|
| 205 |
+
if lab2.ndim == 1:
|
| 206 |
+
lab2 = lab2[None, :]
|
| 207 |
+
channel_axis += 1
|
| 208 |
+
L1, a1, b1 = np.moveaxis(lab1, source=channel_axis, destination=0)[:3]
|
| 209 |
+
L2, a2, b2 = np.moveaxis(lab2, source=channel_axis, destination=0)[:3]
|
| 210 |
+
|
| 211 |
+
# distort `a` based on average chroma
|
| 212 |
+
# then convert to lch coordinates from distorted `a`
|
| 213 |
+
# all subsequence calculations are in the new coordinates
|
| 214 |
+
# (often denoted "prime" in the literature)
|
| 215 |
+
Cbar = 0.5 * (np.hypot(a1, b1) + np.hypot(a2, b2))
|
| 216 |
+
c7 = Cbar**7
|
| 217 |
+
G = 0.5 * (1 - np.sqrt(c7 / (c7 + 25**7)))
|
| 218 |
+
scale = 1 + G
|
| 219 |
+
C1, h1 = _cart2polar_2pi(a1 * scale, b1)
|
| 220 |
+
C2, h2 = _cart2polar_2pi(a2 * scale, b2)
|
| 221 |
+
# recall that c, h are polar coordinates. c==r, h==theta
|
| 222 |
+
|
| 223 |
+
# cide2000 has four terms to delta_e:
|
| 224 |
+
# 1) Luminance term
|
| 225 |
+
# 2) Hue term
|
| 226 |
+
# 3) Chroma term
|
| 227 |
+
# 4) hue Rotation term
|
| 228 |
+
|
| 229 |
+
# lightness term
|
| 230 |
+
Lbar = 0.5 * (L1 + L2)
|
| 231 |
+
tmp = (Lbar - 50) ** 2
|
| 232 |
+
SL = 1 + 0.015 * tmp / np.sqrt(20 + tmp)
|
| 233 |
+
L_term = (L2 - L1) / (kL * SL)
|
| 234 |
+
|
| 235 |
+
# chroma term
|
| 236 |
+
Cbar = 0.5 * (C1 + C2) # new coordinates
|
| 237 |
+
SC = 1 + 0.045 * Cbar
|
| 238 |
+
C_term = (C2 - C1) / (kC * SC)
|
| 239 |
+
|
| 240 |
+
# hue term
|
| 241 |
+
h_diff = h2 - h1
|
| 242 |
+
h_sum = h1 + h2
|
| 243 |
+
CC = C1 * C2
|
| 244 |
+
|
| 245 |
+
dH = h_diff.copy()
|
| 246 |
+
dH[h_diff > np.pi] -= 2 * np.pi
|
| 247 |
+
dH[h_diff < -np.pi] += 2 * np.pi
|
| 248 |
+
dH[CC == 0.0] = 0.0 # if r == 0, dtheta == 0
|
| 249 |
+
dH_term = 2 * np.sqrt(CC) * np.sin(dH / 2)
|
| 250 |
+
|
| 251 |
+
Hbar = h_sum.copy()
|
| 252 |
+
mask = np.logical_and(CC != 0.0, np.abs(h_diff) > np.pi)
|
| 253 |
+
Hbar[mask * (h_sum < 2 * np.pi)] += 2 * np.pi
|
| 254 |
+
Hbar[mask * (h_sum >= 2 * np.pi)] -= 2 * np.pi
|
| 255 |
+
Hbar[CC == 0.0] *= 2
|
| 256 |
+
Hbar *= 0.5
|
| 257 |
+
|
| 258 |
+
T = (
|
| 259 |
+
1
|
| 260 |
+
- 0.17 * np.cos(Hbar - np.deg2rad(30))
|
| 261 |
+
+ 0.24 * np.cos(2 * Hbar)
|
| 262 |
+
+ 0.32 * np.cos(3 * Hbar + np.deg2rad(6))
|
| 263 |
+
- 0.20 * np.cos(4 * Hbar - np.deg2rad(63))
|
| 264 |
+
)
|
| 265 |
+
SH = 1 + 0.015 * Cbar * T
|
| 266 |
+
|
| 267 |
+
H_term = dH_term / (kH * SH)
|
| 268 |
+
|
| 269 |
+
# hue rotation
|
| 270 |
+
c7 = Cbar**7
|
| 271 |
+
Rc = 2 * np.sqrt(c7 / (c7 + 25**7))
|
| 272 |
+
dtheta = np.deg2rad(30) * np.exp(-(((np.rad2deg(Hbar) - 275) / 25) ** 2))
|
| 273 |
+
R_term = -np.sin(2 * dtheta) * Rc * C_term * H_term
|
| 274 |
+
|
| 275 |
+
# put it all together
|
| 276 |
+
dE2 = L_term**2
|
| 277 |
+
dE2 += C_term**2
|
| 278 |
+
dE2 += H_term**2
|
| 279 |
+
dE2 += R_term
|
| 280 |
+
ans = np.sqrt(np.maximum(dE2, 0))
|
| 281 |
+
if unroll:
|
| 282 |
+
ans = ans[0]
|
| 283 |
+
return ans
|
| 284 |
+
|
| 285 |
+
|
| 286 |
+
def deltaE_cmc(lab1, lab2, kL=1, kC=1, *, channel_axis=-1):
|
| 287 |
+
"""Color difference from the CMC l:c standard.
|
| 288 |
+
|
| 289 |
+
This color difference was developed by the Colour Measurement Committee
|
| 290 |
+
(CMC) of the Society of Dyers and Colourists (United Kingdom). It is
|
| 291 |
+
intended for use in the textile industry.
|
| 292 |
+
|
| 293 |
+
The scale factors `kL`, `kC` set the weight given to differences in
|
| 294 |
+
lightness and chroma relative to differences in hue. The usual values are
|
| 295 |
+
``kL=2``, ``kC=1`` for "acceptability" and ``kL=1``, ``kC=1`` for
|
| 296 |
+
"imperceptibility". Colors with ``dE > 1`` are "different" for the given
|
| 297 |
+
scale factors.
|
| 298 |
+
|
| 299 |
+
Parameters
|
| 300 |
+
----------
|
| 301 |
+
lab1 : array_like
|
| 302 |
+
reference color (Lab colorspace)
|
| 303 |
+
lab2 : array_like
|
| 304 |
+
comparison color (Lab colorspace)
|
| 305 |
+
channel_axis : int, optional
|
| 306 |
+
This parameter indicates which axis of the arrays corresponds to
|
| 307 |
+
channels.
|
| 308 |
+
|
| 309 |
+
.. versionadded:: 0.19
|
| 310 |
+
``channel_axis`` was added in 0.19.
|
| 311 |
+
|
| 312 |
+
Returns
|
| 313 |
+
-------
|
| 314 |
+
dE : array_like
|
| 315 |
+
distance between colors `lab1` and `lab2`
|
| 316 |
+
|
| 317 |
+
Notes
|
| 318 |
+
-----
|
| 319 |
+
deltaE_cmc the defines the scales for the lightness, hue, and chroma
|
| 320 |
+
in terms of the first color. Consequently
|
| 321 |
+
``deltaE_cmc(lab1, lab2) != deltaE_cmc(lab2, lab1)``
|
| 322 |
+
|
| 323 |
+
References
|
| 324 |
+
----------
|
| 325 |
+
.. [1] https://en.wikipedia.org/wiki/Color_difference
|
| 326 |
+
.. [2] http://www.brucelindbloom.com/index.html?Eqn_DeltaE_CIE94.html
|
| 327 |
+
.. [3] F. J. J. Clarke, R. McDonald, and B. Rigg, "Modification to the
|
| 328 |
+
JPC79 colour-difference formula," J. Soc. Dyers Colour. 100, 128-132
|
| 329 |
+
(1984).
|
| 330 |
+
"""
|
| 331 |
+
lab1, lab2 = _float_inputs(lab1, lab2, allow_float32=True)
|
| 332 |
+
lab1 = np.moveaxis(lab1, source=channel_axis, destination=0)
|
| 333 |
+
lab2 = np.moveaxis(lab2, source=channel_axis, destination=0)
|
| 334 |
+
L1, C1, h1 = lab2lch(lab1, channel_axis=0)[:3]
|
| 335 |
+
L2, C2, h2 = lab2lch(lab2, channel_axis=0)[:3]
|
| 336 |
+
|
| 337 |
+
dC = C1 - C2
|
| 338 |
+
dL = L1 - L2
|
| 339 |
+
dH2 = get_dH2(lab1, lab2, channel_axis=0)
|
| 340 |
+
|
| 341 |
+
T = np.where(
|
| 342 |
+
np.logical_and(np.rad2deg(h1) >= 164, np.rad2deg(h1) <= 345),
|
| 343 |
+
0.56 + 0.2 * np.abs(np.cos(h1 + np.deg2rad(168))),
|
| 344 |
+
0.36 + 0.4 * np.abs(np.cos(h1 + np.deg2rad(35))),
|
| 345 |
+
)
|
| 346 |
+
c1_4 = C1**4
|
| 347 |
+
F = np.sqrt(c1_4 / (c1_4 + 1900))
|
| 348 |
+
|
| 349 |
+
SL = np.where(L1 < 16, 0.511, 0.040975 * L1 / (1.0 + 0.01765 * L1))
|
| 350 |
+
SC = 0.638 + 0.0638 * C1 / (1.0 + 0.0131 * C1)
|
| 351 |
+
SH = SC * (F * T + 1 - F)
|
| 352 |
+
|
| 353 |
+
dE2 = (dL / (kL * SL)) ** 2
|
| 354 |
+
dE2 += (dC / (kC * SC)) ** 2
|
| 355 |
+
dE2 += dH2 / (SH**2)
|
| 356 |
+
|
| 357 |
+
return np.sqrt(np.maximum(dE2, 0))
|
| 358 |
+
|
| 359 |
+
|
| 360 |
+
def get_dH2(lab1, lab2, *, channel_axis=-1):
|
| 361 |
+
"""squared hue difference term occurring in deltaE_cmc and deltaE_ciede94
|
| 362 |
+
|
| 363 |
+
Despite its name, "dH" is not a simple difference of hue values. We avoid
|
| 364 |
+
working directly with the hue value, since differencing angles is
|
| 365 |
+
troublesome. The hue term is usually written as:
|
| 366 |
+
c1 = sqrt(a1**2 + b1**2)
|
| 367 |
+
c2 = sqrt(a2**2 + b2**2)
|
| 368 |
+
term = (a1-a2)**2 + (b1-b2)**2 - (c1-c2)**2
|
| 369 |
+
dH = sqrt(term)
|
| 370 |
+
|
| 371 |
+
However, this has poor roundoff properties when a or b is dominant.
|
| 372 |
+
Instead, ab is a vector with elements a and b. The same dH term can be
|
| 373 |
+
re-written as:
|
| 374 |
+
|ab1-ab2|**2 - (|ab1| - |ab2|)**2
|
| 375 |
+
and then simplified to:
|
| 376 |
+
2*|ab1|*|ab2| - 2*dot(ab1, ab2)
|
| 377 |
+
"""
|
| 378 |
+
# This function needs double precision internally for accuracy
|
| 379 |
+
input_is_float_32 = _supported_float_type((lab1.dtype, lab2.dtype)) == np.float32
|
| 380 |
+
lab1, lab2 = _float_inputs(lab1, lab2, allow_float32=False)
|
| 381 |
+
|
| 382 |
+
a1, b1 = np.moveaxis(lab1, source=channel_axis, destination=0)[1:3]
|
| 383 |
+
a2, b2 = np.moveaxis(lab2, source=channel_axis, destination=0)[1:3]
|
| 384 |
+
|
| 385 |
+
# magnitude of (a, b) is the chroma
|
| 386 |
+
C1 = np.hypot(a1, b1)
|
| 387 |
+
C2 = np.hypot(a2, b2)
|
| 388 |
+
|
| 389 |
+
term = (C1 * C2) - (a1 * a2 + b1 * b2)
|
| 390 |
+
out = 2 * term
|
| 391 |
+
if input_is_float_32:
|
| 392 |
+
out = out.astype(np.float32)
|
| 393 |
+
return out
|
envs/kitoverlay/skimage/color/rgb_colors.py
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
aliceblue = (0.941, 0.973, 1)
|
| 2 |
+
antiquewhite = (0.98, 0.922, 0.843)
|
| 3 |
+
aqua = (0, 1, 1)
|
| 4 |
+
aquamarine = (0.498, 1, 0.831)
|
| 5 |
+
azure = (0.941, 1, 1)
|
| 6 |
+
beige = (0.961, 0.961, 0.863)
|
| 7 |
+
bisque = (1, 0.894, 0.769)
|
| 8 |
+
black = (0, 0, 0)
|
| 9 |
+
blanchedalmond = (1, 0.922, 0.804)
|
| 10 |
+
blue = (0, 0, 1)
|
| 11 |
+
blueviolet = (0.541, 0.169, 0.886)
|
| 12 |
+
brown = (0.647, 0.165, 0.165)
|
| 13 |
+
burlywood = (0.871, 0.722, 0.529)
|
| 14 |
+
cadetblue = (0.373, 0.62, 0.627)
|
| 15 |
+
chartreuse = (0.498, 1, 0)
|
| 16 |
+
chocolate = (0.824, 0.412, 0.118)
|
| 17 |
+
coral = (1, 0.498, 0.314)
|
| 18 |
+
cornflowerblue = (0.392, 0.584, 0.929)
|
| 19 |
+
cornsilk = (1, 0.973, 0.863)
|
| 20 |
+
crimson = (0.863, 0.0784, 0.235)
|
| 21 |
+
cyan = (0, 1, 1)
|
| 22 |
+
darkblue = (0, 0, 0.545)
|
| 23 |
+
darkcyan = (0, 0.545, 0.545)
|
| 24 |
+
darkgoldenrod = (0.722, 0.525, 0.0431)
|
| 25 |
+
darkgray = (0.663, 0.663, 0.663)
|
| 26 |
+
darkgreen = (0, 0.392, 0)
|
| 27 |
+
darkgrey = (0.663, 0.663, 0.663)
|
| 28 |
+
darkkhaki = (0.741, 0.718, 0.42)
|
| 29 |
+
darkmagenta = (0.545, 0, 0.545)
|
| 30 |
+
darkolivegreen = (0.333, 0.42, 0.184)
|
| 31 |
+
darkorange = (1, 0.549, 0)
|
| 32 |
+
darkorchid = (0.6, 0.196, 0.8)
|
| 33 |
+
darkred = (0.545, 0, 0)
|
| 34 |
+
darksalmon = (0.914, 0.588, 0.478)
|
| 35 |
+
darkseagreen = (0.561, 0.737, 0.561)
|
| 36 |
+
darkslateblue = (0.282, 0.239, 0.545)
|
| 37 |
+
darkslategray = (0.184, 0.31, 0.31)
|
| 38 |
+
darkslategrey = (0.184, 0.31, 0.31)
|
| 39 |
+
darkturquoise = (0, 0.808, 0.82)
|
| 40 |
+
darkviolet = (0.58, 0, 0.827)
|
| 41 |
+
deeppink = (1, 0.0784, 0.576)
|
| 42 |
+
deepskyblue = (0, 0.749, 1)
|
| 43 |
+
dimgray = (0.412, 0.412, 0.412)
|
| 44 |
+
dimgrey = (0.412, 0.412, 0.412)
|
| 45 |
+
dodgerblue = (0.118, 0.565, 1)
|
| 46 |
+
firebrick = (0.698, 0.133, 0.133)
|
| 47 |
+
floralwhite = (1, 0.98, 0.941)
|
| 48 |
+
forestgreen = (0.133, 0.545, 0.133)
|
| 49 |
+
fuchsia = (1, 0, 1)
|
| 50 |
+
gainsboro = (0.863, 0.863, 0.863)
|
| 51 |
+
ghostwhite = (0.973, 0.973, 1)
|
| 52 |
+
gold = (1, 0.843, 0)
|
| 53 |
+
goldenrod = (0.855, 0.647, 0.125)
|
| 54 |
+
gray = (0.502, 0.502, 0.502)
|
| 55 |
+
green = (0, 0.502, 0)
|
| 56 |
+
greenyellow = (0.678, 1, 0.184)
|
| 57 |
+
grey = (0.502, 0.502, 0.502)
|
| 58 |
+
honeydew = (0.941, 1, 0.941)
|
| 59 |
+
hotpink = (1, 0.412, 0.706)
|
| 60 |
+
indianred = (0.804, 0.361, 0.361)
|
| 61 |
+
indigo = (0.294, 0, 0.51)
|
| 62 |
+
ivory = (1, 1, 0.941)
|
| 63 |
+
khaki = (0.941, 0.902, 0.549)
|
| 64 |
+
lavender = (0.902, 0.902, 0.98)
|
| 65 |
+
lavenderblush = (1, 0.941, 0.961)
|
| 66 |
+
lawngreen = (0.486, 0.988, 0)
|
| 67 |
+
lemonchiffon = (1, 0.98, 0.804)
|
| 68 |
+
lightblue = (0.678, 0.847, 0.902)
|
| 69 |
+
lightcoral = (0.941, 0.502, 0.502)
|
| 70 |
+
lightcyan = (0.878, 1, 1)
|
| 71 |
+
lightgoldenrodyellow = (0.98, 0.98, 0.824)
|
| 72 |
+
lightgray = (0.827, 0.827, 0.827)
|
| 73 |
+
lightgreen = (0.565, 0.933, 0.565)
|
| 74 |
+
lightgrey = (0.827, 0.827, 0.827)
|
| 75 |
+
lightpink = (1, 0.714, 0.757)
|
| 76 |
+
lightsalmon = (1, 0.627, 0.478)
|
| 77 |
+
lightseagreen = (0.125, 0.698, 0.667)
|
| 78 |
+
lightskyblue = (0.529, 0.808, 0.98)
|
| 79 |
+
lightslategray = (0.467, 0.533, 0.6)
|
| 80 |
+
lightslategrey = (0.467, 0.533, 0.6)
|
| 81 |
+
lightsteelblue = (0.69, 0.769, 0.871)
|
| 82 |
+
lightyellow = (1, 1, 0.878)
|
| 83 |
+
lime = (0, 1, 0)
|
| 84 |
+
limegreen = (0.196, 0.804, 0.196)
|
| 85 |
+
linen = (0.98, 0.941, 0.902)
|
| 86 |
+
magenta = (1, 0, 1)
|
| 87 |
+
maroon = (0.502, 0, 0)
|
| 88 |
+
mediumaquamarine = (0.4, 0.804, 0.667)
|
| 89 |
+
mediumblue = (0, 0, 0.804)
|
| 90 |
+
mediumorchid = (0.729, 0.333, 0.827)
|
| 91 |
+
mediumpurple = (0.576, 0.439, 0.859)
|
| 92 |
+
mediumseagreen = (0.235, 0.702, 0.443)
|
| 93 |
+
mediumslateblue = (0.482, 0.408, 0.933)
|
| 94 |
+
mediumspringgreen = (0, 0.98, 0.604)
|
| 95 |
+
mediumturquoise = (0.282, 0.82, 0.8)
|
| 96 |
+
mediumvioletred = (0.78, 0.0824, 0.522)
|
| 97 |
+
midnightblue = (0.098, 0.098, 0.439)
|
| 98 |
+
mintcream = (0.961, 1, 0.98)
|
| 99 |
+
mistyrose = (1, 0.894, 0.882)
|
| 100 |
+
moccasin = (1, 0.894, 0.71)
|
| 101 |
+
navajowhite = (1, 0.871, 0.678)
|
| 102 |
+
navy = (0, 0, 0.502)
|
| 103 |
+
oldlace = (0.992, 0.961, 0.902)
|
| 104 |
+
olive = (0.502, 0.502, 0)
|
| 105 |
+
olivedrab = (0.42, 0.557, 0.137)
|
| 106 |
+
orange = (1, 0.647, 0)
|
| 107 |
+
orangered = (1, 0.271, 0)
|
| 108 |
+
orchid = (0.855, 0.439, 0.839)
|
| 109 |
+
palegoldenrod = (0.933, 0.91, 0.667)
|
| 110 |
+
palegreen = (0.596, 0.984, 0.596)
|
| 111 |
+
palevioletred = (0.686, 0.933, 0.933)
|
| 112 |
+
papayawhip = (1, 0.937, 0.835)
|
| 113 |
+
peachpuff = (1, 0.855, 0.725)
|
| 114 |
+
peru = (0.804, 0.522, 0.247)
|
| 115 |
+
pink = (1, 0.753, 0.796)
|
| 116 |
+
plum = (0.867, 0.627, 0.867)
|
| 117 |
+
powderblue = (0.69, 0.878, 0.902)
|
| 118 |
+
purple = (0.502, 0, 0.502)
|
| 119 |
+
red = (1, 0, 0)
|
| 120 |
+
rosybrown = (0.737, 0.561, 0.561)
|
| 121 |
+
royalblue = (0.255, 0.412, 0.882)
|
| 122 |
+
saddlebrown = (0.545, 0.271, 0.0745)
|
| 123 |
+
salmon = (0.98, 0.502, 0.447)
|
| 124 |
+
sandybrown = (0.98, 0.643, 0.376)
|
| 125 |
+
seagreen = (0.18, 0.545, 0.341)
|
| 126 |
+
seashell = (1, 0.961, 0.933)
|
| 127 |
+
sienna = (0.627, 0.322, 0.176)
|
| 128 |
+
silver = (0.753, 0.753, 0.753)
|
| 129 |
+
skyblue = (0.529, 0.808, 0.922)
|
| 130 |
+
slateblue = (0.416, 0.353, 0.804)
|
| 131 |
+
slategray = (0.439, 0.502, 0.565)
|
| 132 |
+
slategrey = (0.439, 0.502, 0.565)
|
| 133 |
+
snow = (1, 0.98, 0.98)
|
| 134 |
+
springgreen = (0, 1, 0.498)
|
| 135 |
+
steelblue = (0.275, 0.51, 0.706)
|
| 136 |
+
tan = (0.824, 0.706, 0.549)
|
| 137 |
+
teal = (0, 0.502, 0.502)
|
| 138 |
+
thistle = (0.847, 0.749, 0.847)
|
| 139 |
+
tomato = (1, 0.388, 0.278)
|
| 140 |
+
turquoise = (0.251, 0.878, 0.816)
|
| 141 |
+
violet = (0.933, 0.51, 0.933)
|
| 142 |
+
wheat = (0.961, 0.871, 0.702)
|
| 143 |
+
white = (1, 1, 1)
|
| 144 |
+
whitesmoke = (0.961, 0.961, 0.961)
|
| 145 |
+
yellow = (1, 1, 0)
|
| 146 |
+
yellowgreen = (0.604, 0.804, 0.196)
|
envs/kitoverlay/skimage/exposure/__init__.py
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Image intensity adjustment, e.g., histogram equalization, etc."""
|
| 2 |
+
|
| 3 |
+
import lazy_loader as _lazy
|
| 4 |
+
|
| 5 |
+
__getattr__, __dir__, __all__ = _lazy.attach_stub(__name__, __file__)
|
envs/kitoverlay/skimage/exposure/__init__.pyi
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Explicitly setting `__all__` is necessary for type inference engines
|
| 2 |
+
# to know which symbols are exported. See
|
| 3 |
+
# https://peps.python.org/pep-0484/#stub-files
|
| 4 |
+
|
| 5 |
+
__all__ = [
|
| 6 |
+
'histogram',
|
| 7 |
+
'equalize_hist',
|
| 8 |
+
'equalize_adapthist',
|
| 9 |
+
'rescale_intensity',
|
| 10 |
+
'cumulative_distribution',
|
| 11 |
+
'adjust_gamma',
|
| 12 |
+
'adjust_sigmoid',
|
| 13 |
+
'adjust_log',
|
| 14 |
+
'is_low_contrast',
|
| 15 |
+
'match_histograms',
|
| 16 |
+
]
|
| 17 |
+
|
| 18 |
+
from ._adapthist import equalize_adapthist
|
| 19 |
+
from .histogram_matching import match_histograms
|
| 20 |
+
from .exposure import (
|
| 21 |
+
histogram,
|
| 22 |
+
equalize_hist,
|
| 23 |
+
rescale_intensity,
|
| 24 |
+
cumulative_distribution,
|
| 25 |
+
adjust_gamma,
|
| 26 |
+
adjust_sigmoid,
|
| 27 |
+
adjust_log,
|
| 28 |
+
is_low_contrast,
|
| 29 |
+
)
|
envs/kitoverlay/skimage/exposure/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (425 Bytes). View file
|
|
|
envs/kitoverlay/skimage/exposure/__pycache__/_adapthist.cpython-311.pyc
ADDED
|
Binary file (16.6 kB). View file
|
|
|
envs/kitoverlay/skimage/exposure/__pycache__/exposure.cpython-311.pyc
ADDED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|
|
| 1 |
+
"""
|
| 2 |
+
Adapted from "Contrast Limited Adaptive Histogram Equalization" by Karel
|
| 3 |
+
Zuiderveld, Graphics Gems IV, Academic Press, 1994.
|
| 4 |
+
|
| 5 |
+
http://tog.acm.org/resources/GraphicsGems/
|
| 6 |
+
|
| 7 |
+
Relicensed with permission of the author under the Modified BSD license.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import math
|
| 11 |
+
import numbers
|
| 12 |
+
|
| 13 |
+
import numpy as np
|
| 14 |
+
|
| 15 |
+
from .._shared.utils import _supported_float_type
|
| 16 |
+
from ..color.adapt_rgb import adapt_rgb, hsv_value
|
| 17 |
+
from .exposure import rescale_intensity
|
| 18 |
+
from ..util import img_as_uint
|
| 19 |
+
|
| 20 |
+
NR_OF_GRAY = 2**14 # number of grayscale levels to use in CLAHE algorithm
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
@adapt_rgb(hsv_value)
|
| 24 |
+
def equalize_adapthist(image, kernel_size=None, clip_limit=0.01, nbins=256):
|
| 25 |
+
"""Contrast Limited Adaptive Histogram Equalization (CLAHE).
|
| 26 |
+
|
| 27 |
+
An algorithm for local contrast enhancement, that uses histograms computed
|
| 28 |
+
over different tile regions of the image. Local details can therefore be
|
| 29 |
+
enhanced even in regions that are darker or lighter than most of the image.
|
| 30 |
+
|
| 31 |
+
Parameters
|
| 32 |
+
----------
|
| 33 |
+
image : (M[, ...][, C]) ndarray
|
| 34 |
+
Input image.
|
| 35 |
+
kernel_size : int or array_like, optional
|
| 36 |
+
Defines the shape of contextual regions used in the algorithm. If
|
| 37 |
+
iterable is passed, it must have the same number of elements as
|
| 38 |
+
``image.ndim`` (without color channel). If integer, it is broadcasted
|
| 39 |
+
to each `image` dimension. By default, ``kernel_size`` is 1/8 of
|
| 40 |
+
``image`` height by 1/8 of its width.
|
| 41 |
+
clip_limit : float, optional
|
| 42 |
+
Clipping limit, normalized between 0 and 1 (higher values give more
|
| 43 |
+
contrast).
|
| 44 |
+
nbins : int, optional
|
| 45 |
+
Number of gray bins for histogram ("data range").
|
| 46 |
+
|
| 47 |
+
Returns
|
| 48 |
+
-------
|
| 49 |
+
out : (M[, ...][, C]) ndarray
|
| 50 |
+
Equalized image with float64 dtype.
|
| 51 |
+
|
| 52 |
+
See Also
|
| 53 |
+
--------
|
| 54 |
+
equalize_hist, rescale_intensity
|
| 55 |
+
|
| 56 |
+
Notes
|
| 57 |
+
-----
|
| 58 |
+
* For color images, the following steps are performed:
|
| 59 |
+
- The image is converted to HSV color space
|
| 60 |
+
- The CLAHE algorithm is run on the V (Value) channel
|
| 61 |
+
- The image is converted back to RGB space and returned
|
| 62 |
+
* For RGBA images, the original alpha channel is removed.
|
| 63 |
+
|
| 64 |
+
.. versionchanged:: 0.17
|
| 65 |
+
The values returned by this function are slightly shifted upwards
|
| 66 |
+
because of an internal change in rounding behavior.
|
| 67 |
+
|
| 68 |
+
References
|
| 69 |
+
----------
|
| 70 |
+
.. [1] http://tog.acm.org/resources/GraphicsGems/
|
| 71 |
+
.. [2] https://en.wikipedia.org/wiki/CLAHE#CLAHE
|
| 72 |
+
"""
|
| 73 |
+
|
| 74 |
+
float_dtype = _supported_float_type(image.dtype)
|
| 75 |
+
image = img_as_uint(image)
|
| 76 |
+
image = np.round(rescale_intensity(image, out_range=(0, NR_OF_GRAY - 1))).astype(
|
| 77 |
+
np.min_scalar_type(NR_OF_GRAY)
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
if kernel_size is None:
|
| 81 |
+
kernel_size = tuple([max(s // 8, 1) for s in image.shape])
|
| 82 |
+
elif isinstance(kernel_size, numbers.Number):
|
| 83 |
+
kernel_size = (kernel_size,) * image.ndim
|
| 84 |
+
elif len(kernel_size) != image.ndim:
|
| 85 |
+
raise ValueError(f'Incorrect value of `kernel_size`: {kernel_size}')
|
| 86 |
+
|
| 87 |
+
kernel_size = [int(k) for k in kernel_size]
|
| 88 |
+
|
| 89 |
+
image = _clahe(image, kernel_size, clip_limit, nbins)
|
| 90 |
+
image = image.astype(float_dtype, copy=False)
|
| 91 |
+
return rescale_intensity(image)
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def _clahe(image, kernel_size, clip_limit, nbins):
|
| 95 |
+
"""Contrast Limited Adaptive Histogram Equalization.
|
| 96 |
+
|
| 97 |
+
Parameters
|
| 98 |
+
----------
|
| 99 |
+
image : (M[, ...]) ndarray
|
| 100 |
+
Input image.
|
| 101 |
+
kernel_size : int or N-tuple of int
|
| 102 |
+
Defines the shape of contextual regions used in the algorithm.
|
| 103 |
+
clip_limit : float
|
| 104 |
+
Normalized clipping limit between 0 and 1 (higher values give more
|
| 105 |
+
contrast).
|
| 106 |
+
nbins : int
|
| 107 |
+
Number of gray bins for histogram ("data range").
|
| 108 |
+
|
| 109 |
+
Returns
|
| 110 |
+
-------
|
| 111 |
+
out : (M[, ...]) ndarray
|
| 112 |
+
Equalized image.
|
| 113 |
+
|
| 114 |
+
The number of "effective" graylevels in the output image is set by `nbins`;
|
| 115 |
+
selecting a small value (e.g. 128) speeds up processing and still produces
|
| 116 |
+
an output image of good quality. A clip limit of 0 or larger than or equal
|
| 117 |
+
to 1 results in standard (non-contrast limited) AHE.
|
| 118 |
+
"""
|
| 119 |
+
ndim = image.ndim
|
| 120 |
+
dtype = image.dtype
|
| 121 |
+
|
| 122 |
+
# pad the image such that the shape in each dimension
|
| 123 |
+
# - is a multiple of the kernel_size and
|
| 124 |
+
# - is preceded by half a kernel size
|
| 125 |
+
pad_start_per_dim = [k // 2 for k in kernel_size]
|
| 126 |
+
|
| 127 |
+
pad_end_per_dim = [
|
| 128 |
+
(k - s % k) % k + int(np.ceil(k / 2.0))
|
| 129 |
+
for k, s in zip(kernel_size, image.shape)
|
| 130 |
+
]
|
| 131 |
+
|
| 132 |
+
image = np.pad(
|
| 133 |
+
image,
|
| 134 |
+
[[p_i, p_f] for p_i, p_f in zip(pad_start_per_dim, pad_end_per_dim)],
|
| 135 |
+
mode='reflect',
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
# determine gray value bins
|
| 139 |
+
bin_size = 1 + NR_OF_GRAY // nbins
|
| 140 |
+
lut = np.arange(NR_OF_GRAY, dtype=np.min_scalar_type(NR_OF_GRAY))
|
| 141 |
+
lut //= bin_size
|
| 142 |
+
|
| 143 |
+
image = lut[image]
|
| 144 |
+
|
| 145 |
+
# calculate graylevel mappings for each contextual region
|
| 146 |
+
# rearrange image into flattened contextual regions
|
| 147 |
+
ns_hist = [int(s / k) - 1 for s, k in zip(image.shape, kernel_size)]
|
| 148 |
+
hist_blocks_shape = np.array([ns_hist, kernel_size]).T.flatten()
|
| 149 |
+
hist_blocks_axis_order = np.array(
|
| 150 |
+
[np.arange(0, ndim * 2, 2), np.arange(1, ndim * 2, 2)]
|
| 151 |
+
).flatten()
|
| 152 |
+
hist_slices = [slice(k // 2, k // 2 + n * k) for k, n in zip(kernel_size, ns_hist)]
|
| 153 |
+
hist_blocks = image[tuple(hist_slices)].reshape(hist_blocks_shape)
|
| 154 |
+
hist_blocks = np.transpose(hist_blocks, axes=hist_blocks_axis_order)
|
| 155 |
+
hist_block_assembled_shape = hist_blocks.shape
|
| 156 |
+
hist_blocks = hist_blocks.reshape((math.prod(ns_hist), -1))
|
| 157 |
+
|
| 158 |
+
# Calculate actual clip limit
|
| 159 |
+
kernel_elements = math.prod(kernel_size)
|
| 160 |
+
if clip_limit > 0.0:
|
| 161 |
+
clim = int(np.clip(clip_limit * kernel_elements, 1, None))
|
| 162 |
+
else:
|
| 163 |
+
# largest possible value, i.e., do not clip (AHE)
|
| 164 |
+
clim = kernel_elements
|
| 165 |
+
|
| 166 |
+
hist = np.apply_along_axis(np.bincount, -1, hist_blocks, minlength=nbins)
|
| 167 |
+
hist = np.apply_along_axis(clip_histogram, -1, hist, clip_limit=clim)
|
| 168 |
+
hist = map_histogram(hist, 0, NR_OF_GRAY - 1, kernel_elements)
|
| 169 |
+
hist = hist.reshape(hist_block_assembled_shape[:ndim] + (-1,))
|
| 170 |
+
|
| 171 |
+
# duplicate leading mappings in each dim
|
| 172 |
+
map_array = np.pad(hist, [[1, 1] for _ in range(ndim)] + [[0, 0]], mode='edge')
|
| 173 |
+
|
| 174 |
+
# Perform multilinear interpolation of graylevel mappings
|
| 175 |
+
# using the convention described here:
|
| 176 |
+
# https://en.wikipedia.org/w/index.php?title=Adaptive_histogram_
|
| 177 |
+
# equalization&oldid=936814673#Efficient_computation_by_interpolation
|
| 178 |
+
|
| 179 |
+
# rearrange image into blocks for vectorized processing
|
| 180 |
+
ns_proc = [int(s / k) for s, k in zip(image.shape, kernel_size)]
|
| 181 |
+
blocks_shape = np.array([ns_proc, kernel_size]).T.flatten()
|
| 182 |
+
blocks_axis_order = np.array(
|
| 183 |
+
[np.arange(0, ndim * 2, 2), np.arange(1, ndim * 2, 2)]
|
| 184 |
+
).flatten()
|
| 185 |
+
blocks = image.reshape(blocks_shape)
|
| 186 |
+
blocks = np.transpose(blocks, axes=blocks_axis_order)
|
| 187 |
+
blocks_flattened_shape = blocks.shape
|
| 188 |
+
blocks = np.reshape(blocks, (math.prod(ns_proc), math.prod(blocks.shape[ndim:])))
|
| 189 |
+
|
| 190 |
+
# calculate interpolation coefficients
|
| 191 |
+
coeffs = np.meshgrid(
|
| 192 |
+
*tuple([np.arange(k) / k for k in kernel_size[::-1]]), indexing='ij'
|
| 193 |
+
)
|
| 194 |
+
coeffs = [np.transpose(c).flatten() for c in coeffs]
|
| 195 |
+
inv_coeffs = [1 - c for dim, c in enumerate(coeffs)]
|
| 196 |
+
|
| 197 |
+
# sum over contributions of neighboring contextual
|
| 198 |
+
# regions in each direction
|
| 199 |
+
result = np.zeros(blocks.shape, dtype=np.float32)
|
| 200 |
+
for iedge, edge in enumerate(np.ndindex(*([2] * ndim))):
|
| 201 |
+
edge_maps = map_array[tuple([slice(e, e + n) for e, n in zip(edge, ns_proc)])]
|
| 202 |
+
edge_maps = edge_maps.reshape((math.prod(ns_proc), -1))
|
| 203 |
+
|
| 204 |
+
# apply map
|
| 205 |
+
edge_mapped = np.take_along_axis(edge_maps, blocks, axis=-1)
|
| 206 |
+
|
| 207 |
+
# interpolate
|
| 208 |
+
edge_coeffs = np.prod(
|
| 209 |
+
[[inv_coeffs, coeffs][e][d] for d, e in enumerate(edge[::-1])], 0
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
+
result += (edge_mapped * edge_coeffs).astype(result.dtype)
|
| 213 |
+
|
| 214 |
+
result = result.astype(dtype)
|
| 215 |
+
|
| 216 |
+
# rebuild result image from blocks
|
| 217 |
+
result = result.reshape(blocks_flattened_shape)
|
| 218 |
+
blocks_axis_rebuild_order = np.array(
|
| 219 |
+
[np.arange(0, ndim), np.arange(ndim, ndim * 2)]
|
| 220 |
+
).T.flatten()
|
| 221 |
+
result = np.transpose(result, axes=blocks_axis_rebuild_order)
|
| 222 |
+
result = result.reshape(image.shape)
|
| 223 |
+
|
| 224 |
+
# undo padding
|
| 225 |
+
unpad_slices = tuple(
|
| 226 |
+
[
|
| 227 |
+
slice(p_i, s - p_f)
|
| 228 |
+
for p_i, p_f, s in zip(pad_start_per_dim, pad_end_per_dim, image.shape)
|
| 229 |
+
]
|
| 230 |
+
)
|
| 231 |
+
result = result[unpad_slices]
|
| 232 |
+
|
| 233 |
+
return result
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
def clip_histogram(hist, clip_limit):
|
| 237 |
+
"""Perform clipping of the histogram and redistribution of bins.
|
| 238 |
+
|
| 239 |
+
The histogram is clipped and the number of excess pixels is counted.
|
| 240 |
+
Afterwards the excess pixels are equally redistributed across the
|
| 241 |
+
whole histogram (providing the bin count is smaller than the cliplimit).
|
| 242 |
+
|
| 243 |
+
Parameters
|
| 244 |
+
----------
|
| 245 |
+
hist : ndarray
|
| 246 |
+
Histogram array.
|
| 247 |
+
clip_limit : int
|
| 248 |
+
Maximum allowed bin count.
|
| 249 |
+
|
| 250 |
+
Returns
|
| 251 |
+
-------
|
| 252 |
+
hist : ndarray
|
| 253 |
+
Clipped histogram.
|
| 254 |
+
"""
|
| 255 |
+
# calculate total number of excess pixels
|
| 256 |
+
excess_mask = hist > clip_limit
|
| 257 |
+
excess = hist[excess_mask]
|
| 258 |
+
n_excess = excess.sum() - excess.size * clip_limit
|
| 259 |
+
hist[excess_mask] = clip_limit
|
| 260 |
+
|
| 261 |
+
# Second part: clip histogram and redistribute excess pixels in each bin
|
| 262 |
+
bin_incr = n_excess // hist.size # average binincrement
|
| 263 |
+
upper = clip_limit - bin_incr # Bins larger than upper set to cliplimit
|
| 264 |
+
|
| 265 |
+
low_mask = hist < upper
|
| 266 |
+
n_excess -= hist[low_mask].size * bin_incr
|
| 267 |
+
hist[low_mask] += bin_incr
|
| 268 |
+
|
| 269 |
+
mid_mask = np.logical_and(hist >= upper, hist < clip_limit)
|
| 270 |
+
mid = hist[mid_mask]
|
| 271 |
+
n_excess += mid.sum() - mid.size * clip_limit
|
| 272 |
+
hist[mid_mask] = clip_limit
|
| 273 |
+
|
| 274 |
+
while n_excess > 0: # Redistribute remaining excess
|
| 275 |
+
prev_n_excess = n_excess
|
| 276 |
+
for index in range(hist.size):
|
| 277 |
+
under_mask = hist < clip_limit
|
| 278 |
+
step_size = max(1, np.count_nonzero(under_mask) // n_excess)
|
| 279 |
+
under_mask = under_mask[index::step_size]
|
| 280 |
+
hist[index::step_size][under_mask] += 1
|
| 281 |
+
n_excess -= np.count_nonzero(under_mask)
|
| 282 |
+
if n_excess <= 0:
|
| 283 |
+
break
|
| 284 |
+
if prev_n_excess == n_excess:
|
| 285 |
+
break
|
| 286 |
+
|
| 287 |
+
return hist
|
| 288 |
+
|
| 289 |
+
|
| 290 |
+
def map_histogram(hist, min_val, max_val, n_pixels):
|
| 291 |
+
"""Calculate the equalized lookup table (mapping).
|
| 292 |
+
|
| 293 |
+
It does so by cumulating the input histogram.
|
| 294 |
+
Histogram bins are assumed to be represented by the last array dimension.
|
| 295 |
+
|
| 296 |
+
Parameters
|
| 297 |
+
----------
|
| 298 |
+
hist : ndarray
|
| 299 |
+
Clipped histogram.
|
| 300 |
+
min_val : int
|
| 301 |
+
Minimum value for mapping.
|
| 302 |
+
max_val : int
|
| 303 |
+
Maximum value for mapping.
|
| 304 |
+
n_pixels : int
|
| 305 |
+
Number of pixels in the region.
|
| 306 |
+
|
| 307 |
+
Returns
|
| 308 |
+
-------
|
| 309 |
+
out : ndarray
|
| 310 |
+
Mapped intensity LUT.
|
| 311 |
+
"""
|
| 312 |
+
out = np.cumsum(hist, axis=-1).astype(float)
|
| 313 |
+
out *= (max_val - min_val) / n_pixels
|
| 314 |
+
out += min_val
|
| 315 |
+
np.clip(out, a_min=None, a_max=max_val, out=out)
|
| 316 |
+
|
| 317 |
+
return out.astype(int)
|
envs/kitoverlay/skimage/exposure/exposure.py
ADDED
|
@@ -0,0 +1,851 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
from ..util.dtype import dtype_range, dtype_limits
|
| 4 |
+
from .._shared import utils
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
__all__ = [
|
| 8 |
+
'histogram',
|
| 9 |
+
'cumulative_distribution',
|
| 10 |
+
'equalize_hist',
|
| 11 |
+
'rescale_intensity',
|
| 12 |
+
'adjust_gamma',
|
| 13 |
+
'adjust_log',
|
| 14 |
+
'adjust_sigmoid',
|
| 15 |
+
]
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
DTYPE_RANGE = dtype_range.copy()
|
| 19 |
+
DTYPE_RANGE.update((d.__name__, limits) for d, limits in dtype_range.items())
|
| 20 |
+
DTYPE_RANGE.update(
|
| 21 |
+
{
|
| 22 |
+
'uint10': (0, 2**10 - 1),
|
| 23 |
+
'uint12': (0, 2**12 - 1),
|
| 24 |
+
'uint14': (0, 2**14 - 1),
|
| 25 |
+
'bool': dtype_range[bool],
|
| 26 |
+
'float': dtype_range[np.float64],
|
| 27 |
+
}
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def _offset_array(arr, low_boundary, high_boundary):
|
| 32 |
+
"""Offset the array to get the lowest value at 0 if negative."""
|
| 33 |
+
if low_boundary < 0:
|
| 34 |
+
offset = low_boundary
|
| 35 |
+
dyn_range = high_boundary - low_boundary
|
| 36 |
+
# get smallest dtype that can hold both minimum and offset maximum
|
| 37 |
+
offset_dtype = np.promote_types(
|
| 38 |
+
np.min_scalar_type(dyn_range), np.min_scalar_type(low_boundary)
|
| 39 |
+
)
|
| 40 |
+
if arr.dtype != offset_dtype:
|
| 41 |
+
# prevent overflow errors when offsetting
|
| 42 |
+
arr = arr.astype(offset_dtype)
|
| 43 |
+
arr = arr - offset
|
| 44 |
+
return arr
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def _bincount_histogram_centers(image, source_range):
|
| 48 |
+
"""Compute bin centers for bincount-based histogram."""
|
| 49 |
+
if source_range not in ['image', 'dtype']:
|
| 50 |
+
raise ValueError(f'Incorrect value for `source_range` argument: {source_range}')
|
| 51 |
+
if source_range == 'image':
|
| 52 |
+
image_min = int(image.min().astype(np.int64))
|
| 53 |
+
image_max = int(image.max().astype(np.int64))
|
| 54 |
+
elif source_range == 'dtype':
|
| 55 |
+
image_min, image_max = dtype_limits(image, clip_negative=False)
|
| 56 |
+
bin_centers = np.arange(image_min, image_max + 1)
|
| 57 |
+
return bin_centers
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def _bincount_histogram(image, source_range, bin_centers=None):
|
| 61 |
+
"""
|
| 62 |
+
Efficient histogram calculation for an image of integers.
|
| 63 |
+
|
| 64 |
+
This function is significantly more efficient than np.histogram but
|
| 65 |
+
works only on images of integers. It is based on np.bincount.
|
| 66 |
+
|
| 67 |
+
Parameters
|
| 68 |
+
----------
|
| 69 |
+
image : array
|
| 70 |
+
Input image.
|
| 71 |
+
source_range : {'image', 'dtype'}
|
| 72 |
+
'image' determines the range from the input image.
|
| 73 |
+
'dtype' determines the range from the expected range of the images
|
| 74 |
+
of that data type.
|
| 75 |
+
|
| 76 |
+
Returns
|
| 77 |
+
-------
|
| 78 |
+
hist : array
|
| 79 |
+
The values of the histogram.
|
| 80 |
+
bin_centers : array
|
| 81 |
+
The values at the center of the bins.
|
| 82 |
+
"""
|
| 83 |
+
if bin_centers is None:
|
| 84 |
+
bin_centers = _bincount_histogram_centers(image, source_range)
|
| 85 |
+
image_min, image_max = bin_centers[0], bin_centers[-1]
|
| 86 |
+
image = _offset_array(image, image_min, image_max)
|
| 87 |
+
hist = np.bincount(image.ravel(), minlength=image_max - min(image_min, 0) + 1)
|
| 88 |
+
if source_range == 'image':
|
| 89 |
+
idx = max(image_min, 0)
|
| 90 |
+
hist = hist[idx:]
|
| 91 |
+
return hist, bin_centers
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def _get_outer_edges(image, hist_range):
|
| 95 |
+
"""Determine the outer bin edges to use for `numpy.histogram`.
|
| 96 |
+
|
| 97 |
+
These are obtained from either the image or hist_range.
|
| 98 |
+
|
| 99 |
+
Parameters
|
| 100 |
+
----------
|
| 101 |
+
image : ndarray
|
| 102 |
+
Image for which the histogram is to be computed.
|
| 103 |
+
hist_range : 2-tuple of int or None
|
| 104 |
+
Range of values covered by the histogram bins. If None, the minimum
|
| 105 |
+
and maximum values of `image` are used.
|
| 106 |
+
|
| 107 |
+
Returns
|
| 108 |
+
-------
|
| 109 |
+
first_edge, last_edge : int
|
| 110 |
+
The range spanned by the histogram bins.
|
| 111 |
+
|
| 112 |
+
Notes
|
| 113 |
+
-----
|
| 114 |
+
This function is adapted from ``np.lib.histograms._get_outer_edges``.
|
| 115 |
+
"""
|
| 116 |
+
if hist_range is not None:
|
| 117 |
+
first_edge, last_edge = hist_range
|
| 118 |
+
if first_edge > last_edge:
|
| 119 |
+
raise ValueError("max must be larger than min in hist_range parameter.")
|
| 120 |
+
if not (np.isfinite(first_edge) and np.isfinite(last_edge)):
|
| 121 |
+
raise ValueError(
|
| 122 |
+
f'supplied hist_range of [{first_edge}, {last_edge}] is ' f'not finite'
|
| 123 |
+
)
|
| 124 |
+
elif image.size == 0:
|
| 125 |
+
# handle empty arrays. Can't determine hist_range, so use 0-1.
|
| 126 |
+
first_edge, last_edge = 0, 1
|
| 127 |
+
else:
|
| 128 |
+
first_edge, last_edge = image.min(), image.max()
|
| 129 |
+
if not (np.isfinite(first_edge) and np.isfinite(last_edge)):
|
| 130 |
+
raise ValueError(
|
| 131 |
+
f'autodetected hist_range of [{first_edge}, {last_edge}] is '
|
| 132 |
+
f'not finite'
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
# expand empty hist_range to avoid divide by zero
|
| 136 |
+
if first_edge == last_edge:
|
| 137 |
+
first_edge = first_edge - 0.5
|
| 138 |
+
last_edge = last_edge + 0.5
|
| 139 |
+
|
| 140 |
+
return first_edge, last_edge
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
def _get_bin_edges(image, nbins, hist_range):
|
| 144 |
+
"""Computes histogram bins for use with `numpy.histogram`.
|
| 145 |
+
|
| 146 |
+
Parameters
|
| 147 |
+
----------
|
| 148 |
+
image : ndarray
|
| 149 |
+
Image for which the histogram is to be computed.
|
| 150 |
+
nbins : int
|
| 151 |
+
The number of bins.
|
| 152 |
+
hist_range : 2-tuple of int
|
| 153 |
+
Range of values covered by the histogram bins.
|
| 154 |
+
|
| 155 |
+
Returns
|
| 156 |
+
-------
|
| 157 |
+
bin_edges : ndarray
|
| 158 |
+
The histogram bin edges.
|
| 159 |
+
|
| 160 |
+
Notes
|
| 161 |
+
-----
|
| 162 |
+
This function is a simplified version of
|
| 163 |
+
``np.lib.histograms._get_bin_edges`` that only supports uniform bins.
|
| 164 |
+
"""
|
| 165 |
+
first_edge, last_edge = _get_outer_edges(image, hist_range)
|
| 166 |
+
# numpy/gh-10322 means that type resolution rules are dependent on array
|
| 167 |
+
# shapes. To avoid this causing problems, we pick a type now and stick
|
| 168 |
+
# with it throughout.
|
| 169 |
+
bin_type = np.result_type(first_edge, last_edge, image)
|
| 170 |
+
if np.issubdtype(bin_type, np.integer):
|
| 171 |
+
bin_type = np.result_type(bin_type, float)
|
| 172 |
+
|
| 173 |
+
# compute bin edges
|
| 174 |
+
bin_edges = np.linspace(
|
| 175 |
+
first_edge, last_edge, nbins + 1, endpoint=True, dtype=bin_type
|
| 176 |
+
)
|
| 177 |
+
return bin_edges
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
def _get_numpy_hist_range(image, source_range):
|
| 181 |
+
if source_range == 'image':
|
| 182 |
+
hist_range = None
|
| 183 |
+
elif source_range == 'dtype':
|
| 184 |
+
hist_range = dtype_limits(image, clip_negative=False)
|
| 185 |
+
else:
|
| 186 |
+
raise ValueError(f'Incorrect value for `source_range` argument: {source_range}')
|
| 187 |
+
return hist_range
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
@utils.channel_as_last_axis(multichannel_output=False)
|
| 191 |
+
def histogram(
|
| 192 |
+
image, nbins=256, source_range='image', normalize=False, *, channel_axis=None
|
| 193 |
+
):
|
| 194 |
+
"""Return histogram of image.
|
| 195 |
+
|
| 196 |
+
Unlike `numpy.histogram`, this function returns the centers of bins and
|
| 197 |
+
does not rebin integer arrays. For integer arrays, each integer value has
|
| 198 |
+
its own bin, which improves speed and intensity-resolution.
|
| 199 |
+
|
| 200 |
+
If `channel_axis` is not set, the histogram is computed on the flattened
|
| 201 |
+
image. For color or multichannel images, set ``channel_axis`` to use a
|
| 202 |
+
common binning for all channels. Alternatively, one may apply the function
|
| 203 |
+
separately on each channel to obtain a histogram for each color channel
|
| 204 |
+
with separate binning.
|
| 205 |
+
|
| 206 |
+
Parameters
|
| 207 |
+
----------
|
| 208 |
+
image : array
|
| 209 |
+
Input image.
|
| 210 |
+
nbins : int, optional
|
| 211 |
+
Number of bins used to calculate histogram. This value is ignored for
|
| 212 |
+
integer arrays.
|
| 213 |
+
source_range : {'image', 'dtype'}, optional
|
| 214 |
+
'image' (default) determines the range from the input image.
|
| 215 |
+
'dtype' determines the range from the expected range of the images
|
| 216 |
+
of that data type.
|
| 217 |
+
normalize : bool, optional
|
| 218 |
+
If True, normalize the histogram by the sum of its values.
|
| 219 |
+
channel_axis : int or None, optional
|
| 220 |
+
If None, the image is assumed to be a grayscale (single channel) image.
|
| 221 |
+
Otherwise, this parameter indicates which axis of the array corresponds
|
| 222 |
+
to channels.
|
| 223 |
+
|
| 224 |
+
Returns
|
| 225 |
+
-------
|
| 226 |
+
hist : array
|
| 227 |
+
The values of the histogram. When ``channel_axis`` is not None, hist
|
| 228 |
+
will be a 2D array where the first axis corresponds to channels.
|
| 229 |
+
bin_centers : array
|
| 230 |
+
The values at the center of the bins.
|
| 231 |
+
|
| 232 |
+
See Also
|
| 233 |
+
--------
|
| 234 |
+
cumulative_distribution
|
| 235 |
+
|
| 236 |
+
Examples
|
| 237 |
+
--------
|
| 238 |
+
>>> from skimage import data, exposure, img_as_float
|
| 239 |
+
>>> image = img_as_float(data.camera())
|
| 240 |
+
>>> np.histogram(image, bins=2)
|
| 241 |
+
(array([ 93585, 168559]), array([0. , 0.5, 1. ]))
|
| 242 |
+
>>> exposure.histogram(image, nbins=2)
|
| 243 |
+
(array([ 93585, 168559]), array([0.25, 0.75]))
|
| 244 |
+
"""
|
| 245 |
+
sh = image.shape
|
| 246 |
+
if len(sh) == 3 and sh[-1] < 4 and channel_axis is None:
|
| 247 |
+
utils.warn(
|
| 248 |
+
'This might be a color image. The histogram will be '
|
| 249 |
+
'computed on the flattened image. You can instead '
|
| 250 |
+
'apply this function to each color channel, or set '
|
| 251 |
+
'channel_axis.'
|
| 252 |
+
)
|
| 253 |
+
|
| 254 |
+
if channel_axis is not None:
|
| 255 |
+
channels = sh[-1]
|
| 256 |
+
hist = []
|
| 257 |
+
|
| 258 |
+
# compute bins based on the raveled array
|
| 259 |
+
if np.issubdtype(image.dtype, np.integer):
|
| 260 |
+
# here bins corresponds to the bin centers
|
| 261 |
+
bins = _bincount_histogram_centers(image, source_range)
|
| 262 |
+
else:
|
| 263 |
+
# determine the bin edges for np.histogram
|
| 264 |
+
hist_range = _get_numpy_hist_range(image, source_range)
|
| 265 |
+
bins = _get_bin_edges(image, nbins, hist_range)
|
| 266 |
+
|
| 267 |
+
for chan in range(channels):
|
| 268 |
+
h, bc = _histogram(image[..., chan], bins, source_range, normalize)
|
| 269 |
+
hist.append(h)
|
| 270 |
+
# Convert to numpy arrays
|
| 271 |
+
bin_centers = np.asarray(bc)
|
| 272 |
+
hist = np.stack(hist, axis=0)
|
| 273 |
+
else:
|
| 274 |
+
hist, bin_centers = _histogram(image, nbins, source_range, normalize)
|
| 275 |
+
|
| 276 |
+
return hist, bin_centers
|
| 277 |
+
|
| 278 |
+
|
| 279 |
+
def _histogram(image, bins, source_range, normalize):
|
| 280 |
+
"""
|
| 281 |
+
|
| 282 |
+
Parameters
|
| 283 |
+
----------
|
| 284 |
+
image : ndarray
|
| 285 |
+
Image for which the histogram is to be computed.
|
| 286 |
+
bins : int or ndarray
|
| 287 |
+
The number of histogram bins. For images with integer dtype, an array
|
| 288 |
+
containing the bin centers can also be provided. For images with
|
| 289 |
+
floating point dtype, this can be an array of bin_edges for use by
|
| 290 |
+
``np.histogram``.
|
| 291 |
+
source_range : {'image', 'dtype'}, optional
|
| 292 |
+
'image' (default) determines the range from the input image.
|
| 293 |
+
'dtype' determines the range from the expected range of the images
|
| 294 |
+
of that data type.
|
| 295 |
+
normalize : bool, optional
|
| 296 |
+
If True, normalize the histogram by the sum of its values.
|
| 297 |
+
"""
|
| 298 |
+
|
| 299 |
+
image = image.flatten()
|
| 300 |
+
# For integer types, histogramming with bincount is more efficient.
|
| 301 |
+
if np.issubdtype(image.dtype, np.integer):
|
| 302 |
+
bin_centers = bins if isinstance(bins, np.ndarray) else None
|
| 303 |
+
hist, bin_centers = _bincount_histogram(image, source_range, bin_centers)
|
| 304 |
+
else:
|
| 305 |
+
hist_range = _get_numpy_hist_range(image, source_range)
|
| 306 |
+
hist, bin_edges = np.histogram(image, bins=bins, range=hist_range)
|
| 307 |
+
bin_centers = (bin_edges[:-1] + bin_edges[1:]) / 2.0
|
| 308 |
+
|
| 309 |
+
if normalize:
|
| 310 |
+
hist = hist / np.sum(hist)
|
| 311 |
+
return hist, bin_centers
|
| 312 |
+
|
| 313 |
+
|
| 314 |
+
def cumulative_distribution(image, nbins=256):
|
| 315 |
+
"""Return cumulative distribution function (cdf) for the given image.
|
| 316 |
+
|
| 317 |
+
Parameters
|
| 318 |
+
----------
|
| 319 |
+
image : array
|
| 320 |
+
Image array.
|
| 321 |
+
nbins : int, optional
|
| 322 |
+
Number of bins for image histogram.
|
| 323 |
+
|
| 324 |
+
Returns
|
| 325 |
+
-------
|
| 326 |
+
img_cdf : array
|
| 327 |
+
Values of cumulative distribution function.
|
| 328 |
+
bin_centers : array
|
| 329 |
+
Centers of bins.
|
| 330 |
+
|
| 331 |
+
See Also
|
| 332 |
+
--------
|
| 333 |
+
histogram
|
| 334 |
+
|
| 335 |
+
References
|
| 336 |
+
----------
|
| 337 |
+
.. [1] https://en.wikipedia.org/wiki/Cumulative_distribution_function
|
| 338 |
+
|
| 339 |
+
Examples
|
| 340 |
+
--------
|
| 341 |
+
>>> from skimage import data, exposure, img_as_float
|
| 342 |
+
>>> image = img_as_float(data.camera())
|
| 343 |
+
>>> hi = exposure.histogram(image)
|
| 344 |
+
>>> cdf = exposure.cumulative_distribution(image)
|
| 345 |
+
>>> all(cdf[0] == np.cumsum(hi[0])/float(image.size))
|
| 346 |
+
True
|
| 347 |
+
"""
|
| 348 |
+
hist, bin_centers = histogram(image, nbins)
|
| 349 |
+
img_cdf = hist.cumsum()
|
| 350 |
+
img_cdf = img_cdf / float(img_cdf[-1])
|
| 351 |
+
|
| 352 |
+
# cast img_cdf to single precision for float32 or float16 inputs
|
| 353 |
+
cdf_dtype = utils._supported_float_type(image.dtype)
|
| 354 |
+
img_cdf = img_cdf.astype(cdf_dtype, copy=False)
|
| 355 |
+
|
| 356 |
+
return img_cdf, bin_centers
|
| 357 |
+
|
| 358 |
+
|
| 359 |
+
def equalize_hist(image, nbins=256, mask=None):
|
| 360 |
+
"""Return image after histogram equalization.
|
| 361 |
+
|
| 362 |
+
Parameters
|
| 363 |
+
----------
|
| 364 |
+
image : array
|
| 365 |
+
Image array.
|
| 366 |
+
nbins : int, optional
|
| 367 |
+
Number of bins for image histogram. Note: this argument is
|
| 368 |
+
ignored for integer images, for which each integer is its own
|
| 369 |
+
bin.
|
| 370 |
+
mask : ndarray of bools or 0s and 1s, optional
|
| 371 |
+
Array of same shape as `image`. Only points at which mask == True
|
| 372 |
+
are used for the equalization, which is applied to the whole image.
|
| 373 |
+
|
| 374 |
+
Returns
|
| 375 |
+
-------
|
| 376 |
+
out : float array
|
| 377 |
+
Image array after histogram equalization.
|
| 378 |
+
|
| 379 |
+
Notes
|
| 380 |
+
-----
|
| 381 |
+
This function is adapted from [1]_ with the author's permission.
|
| 382 |
+
|
| 383 |
+
References
|
| 384 |
+
----------
|
| 385 |
+
.. [1] http://www.janeriksolem.net/histogram-equalization-with-python-and.html
|
| 386 |
+
.. [2] https://en.wikipedia.org/wiki/Histogram_equalization
|
| 387 |
+
|
| 388 |
+
"""
|
| 389 |
+
if mask is not None:
|
| 390 |
+
mask = np.array(mask, dtype=bool)
|
| 391 |
+
cdf, bin_centers = cumulative_distribution(image[mask], nbins)
|
| 392 |
+
else:
|
| 393 |
+
cdf, bin_centers = cumulative_distribution(image, nbins)
|
| 394 |
+
out = np.interp(image.flat, bin_centers, cdf)
|
| 395 |
+
out = out.reshape(image.shape)
|
| 396 |
+
# Unfortunately, np.interp currently always promotes to float64, so we
|
| 397 |
+
# have to cast back to single precision when float32 output is desired
|
| 398 |
+
return out.astype(utils._supported_float_type(image.dtype), copy=False)
|
| 399 |
+
|
| 400 |
+
|
| 401 |
+
def intensity_range(image, range_values='image', clip_negative=False):
|
| 402 |
+
"""Return image intensity range (min, max) based on desired value type.
|
| 403 |
+
|
| 404 |
+
Parameters
|
| 405 |
+
----------
|
| 406 |
+
image : array
|
| 407 |
+
Input image.
|
| 408 |
+
range_values : str or 2-tuple, optional
|
| 409 |
+
The image intensity range is configured by this parameter.
|
| 410 |
+
The possible values for this parameter are enumerated below.
|
| 411 |
+
|
| 412 |
+
'image'
|
| 413 |
+
Return image min/max as the range.
|
| 414 |
+
'dtype'
|
| 415 |
+
Return min/max of the image's dtype as the range.
|
| 416 |
+
dtype-name
|
| 417 |
+
Return intensity range based on desired `dtype`. Must be valid key
|
| 418 |
+
in `DTYPE_RANGE`. Note: `image` is ignored for this range type.
|
| 419 |
+
2-tuple
|
| 420 |
+
Return `range_values` as min/max intensities. Note that there's no
|
| 421 |
+
reason to use this function if you just want to specify the
|
| 422 |
+
intensity range explicitly. This option is included for functions
|
| 423 |
+
that use `intensity_range` to support all desired range types.
|
| 424 |
+
|
| 425 |
+
clip_negative : bool, optional
|
| 426 |
+
If True, clip the negative range (i.e. return 0 for min intensity)
|
| 427 |
+
even if the image dtype allows negative values.
|
| 428 |
+
"""
|
| 429 |
+
if range_values == 'dtype':
|
| 430 |
+
range_values = image.dtype.type
|
| 431 |
+
|
| 432 |
+
if range_values == 'image':
|
| 433 |
+
i_min = np.min(image)
|
| 434 |
+
i_max = np.max(image)
|
| 435 |
+
elif range_values in DTYPE_RANGE:
|
| 436 |
+
i_min, i_max = DTYPE_RANGE[range_values]
|
| 437 |
+
if clip_negative:
|
| 438 |
+
i_min = 0
|
| 439 |
+
else:
|
| 440 |
+
i_min, i_max = range_values
|
| 441 |
+
return i_min, i_max
|
| 442 |
+
|
| 443 |
+
|
| 444 |
+
def _output_dtype(dtype_or_range, image_dtype):
|
| 445 |
+
"""Determine the output dtype for rescale_intensity.
|
| 446 |
+
|
| 447 |
+
The dtype is determined according to the following rules:
|
| 448 |
+
- if ``dtype_or_range`` is a dtype, that is the output dtype.
|
| 449 |
+
- if ``dtype_or_range`` is a dtype string, that is the dtype used, unless
|
| 450 |
+
it is not a NumPy data type (e.g. 'uint12' for 12-bit unsigned integers),
|
| 451 |
+
in which case the data type that can contain it will be used
|
| 452 |
+
(e.g. uint16 in this case).
|
| 453 |
+
- if ``dtype_or_range`` is a pair of values, the output data type will be
|
| 454 |
+
``_supported_float_type(image_dtype)``. This preserves float32 output for
|
| 455 |
+
float32 inputs.
|
| 456 |
+
|
| 457 |
+
Parameters
|
| 458 |
+
----------
|
| 459 |
+
dtype_or_range : type, string, or 2-tuple of int/float
|
| 460 |
+
The desired range for the output, expressed as either a NumPy dtype or
|
| 461 |
+
as a (min, max) pair of numbers.
|
| 462 |
+
image_dtype : np.dtype
|
| 463 |
+
The input image dtype.
|
| 464 |
+
|
| 465 |
+
Returns
|
| 466 |
+
-------
|
| 467 |
+
out_dtype : type
|
| 468 |
+
The data type appropriate for the desired output.
|
| 469 |
+
"""
|
| 470 |
+
if type(dtype_or_range) in [list, tuple, np.ndarray]:
|
| 471 |
+
# pair of values: always return float.
|
| 472 |
+
return utils._supported_float_type(image_dtype)
|
| 473 |
+
if type(dtype_or_range) == type:
|
| 474 |
+
# already a type: return it
|
| 475 |
+
return dtype_or_range
|
| 476 |
+
if dtype_or_range in DTYPE_RANGE:
|
| 477 |
+
# string key in DTYPE_RANGE dictionary
|
| 478 |
+
try:
|
| 479 |
+
# if it's a canonical numpy dtype, convert
|
| 480 |
+
return np.dtype(dtype_or_range).type
|
| 481 |
+
except TypeError: # uint10, uint12, uint14
|
| 482 |
+
# otherwise, return uint16
|
| 483 |
+
return np.uint16
|
| 484 |
+
else:
|
| 485 |
+
raise ValueError(
|
| 486 |
+
'Incorrect value for out_range, should be a valid image data '
|
| 487 |
+
f'type or a pair of values, got {dtype_or_range}.'
|
| 488 |
+
)
|
| 489 |
+
|
| 490 |
+
|
| 491 |
+
def rescale_intensity(image, in_range='image', out_range='dtype'):
|
| 492 |
+
"""Return image after stretching or shrinking its intensity levels.
|
| 493 |
+
|
| 494 |
+
The desired intensity range of the input and output, `in_range` and
|
| 495 |
+
`out_range` respectively, are used to stretch or shrink the intensity range
|
| 496 |
+
of the input image. See examples below.
|
| 497 |
+
|
| 498 |
+
Parameters
|
| 499 |
+
----------
|
| 500 |
+
image : array
|
| 501 |
+
Image array.
|
| 502 |
+
in_range, out_range : str or 2-tuple, optional
|
| 503 |
+
Min and max intensity values of input and output image.
|
| 504 |
+
The possible values for this parameter are enumerated below.
|
| 505 |
+
|
| 506 |
+
'image'
|
| 507 |
+
Use image min/max as the intensity range.
|
| 508 |
+
'dtype'
|
| 509 |
+
Use min/max of the image's dtype as the intensity range.
|
| 510 |
+
dtype-name
|
| 511 |
+
Use intensity range based on desired `dtype`. Must be valid key
|
| 512 |
+
in `DTYPE_RANGE`.
|
| 513 |
+
2-tuple
|
| 514 |
+
Use `range_values` as explicit min/max intensities.
|
| 515 |
+
|
| 516 |
+
Returns
|
| 517 |
+
-------
|
| 518 |
+
out : array
|
| 519 |
+
Image array after rescaling its intensity. This image is the same dtype
|
| 520 |
+
as the input image.
|
| 521 |
+
|
| 522 |
+
Notes
|
| 523 |
+
-----
|
| 524 |
+
.. versionchanged:: 0.17
|
| 525 |
+
The dtype of the output array has changed to match the input dtype, or
|
| 526 |
+
float if the output range is specified by a pair of values.
|
| 527 |
+
|
| 528 |
+
See Also
|
| 529 |
+
--------
|
| 530 |
+
equalize_hist
|
| 531 |
+
|
| 532 |
+
Examples
|
| 533 |
+
--------
|
| 534 |
+
By default, the min/max intensities of the input image are stretched to
|
| 535 |
+
the limits allowed by the image's dtype, since `in_range` defaults to
|
| 536 |
+
'image' and `out_range` defaults to 'dtype':
|
| 537 |
+
|
| 538 |
+
>>> image = np.array([51, 102, 153], dtype=np.uint8)
|
| 539 |
+
>>> rescale_intensity(image)
|
| 540 |
+
array([ 0, 127, 255], dtype=uint8)
|
| 541 |
+
|
| 542 |
+
It's easy to accidentally convert an image dtype from uint8 to float:
|
| 543 |
+
|
| 544 |
+
>>> 1.0 * image
|
| 545 |
+
array([ 51., 102., 153.])
|
| 546 |
+
|
| 547 |
+
Use `rescale_intensity` to rescale to the proper range for float dtypes:
|
| 548 |
+
|
| 549 |
+
>>> image_float = 1.0 * image
|
| 550 |
+
>>> rescale_intensity(image_float)
|
| 551 |
+
array([0. , 0.5, 1. ])
|
| 552 |
+
|
| 553 |
+
To maintain the low contrast of the original, use the `in_range` parameter:
|
| 554 |
+
|
| 555 |
+
>>> rescale_intensity(image_float, in_range=(0, 255))
|
| 556 |
+
array([0.2, 0.4, 0.6])
|
| 557 |
+
|
| 558 |
+
If the min/max value of `in_range` is more/less than the min/max image
|
| 559 |
+
intensity, then the intensity levels are clipped:
|
| 560 |
+
|
| 561 |
+
>>> rescale_intensity(image_float, in_range=(0, 102))
|
| 562 |
+
array([0.5, 1. , 1. ])
|
| 563 |
+
|
| 564 |
+
If you have an image with signed integers but want to rescale the image to
|
| 565 |
+
just the positive range, use the `out_range` parameter. In that case, the
|
| 566 |
+
output dtype will be float:
|
| 567 |
+
|
| 568 |
+
>>> image = np.array([-10, 0, 10], dtype=np.int8)
|
| 569 |
+
>>> rescale_intensity(image, out_range=(0, 127))
|
| 570 |
+
array([ 0. , 63.5, 127. ])
|
| 571 |
+
|
| 572 |
+
To get the desired range with a specific dtype, use ``.astype()``:
|
| 573 |
+
|
| 574 |
+
>>> rescale_intensity(image, out_range=(0, 127)).astype(np.int8)
|
| 575 |
+
array([ 0, 63, 127], dtype=int8)
|
| 576 |
+
|
| 577 |
+
If the input image is constant, the output will be clipped directly to the
|
| 578 |
+
output range:
|
| 579 |
+
>>> image = np.array([130, 130, 130], dtype=np.int32)
|
| 580 |
+
>>> rescale_intensity(image, out_range=(0, 127)).astype(np.int32)
|
| 581 |
+
array([127, 127, 127], dtype=int32)
|
| 582 |
+
"""
|
| 583 |
+
if out_range in ['dtype', 'image']:
|
| 584 |
+
out_dtype = _output_dtype(image.dtype.type, image.dtype)
|
| 585 |
+
else:
|
| 586 |
+
out_dtype = _output_dtype(out_range, image.dtype)
|
| 587 |
+
|
| 588 |
+
imin, imax = map(float, intensity_range(image, in_range))
|
| 589 |
+
omin, omax = map(
|
| 590 |
+
float, intensity_range(image, out_range, clip_negative=(imin >= 0))
|
| 591 |
+
)
|
| 592 |
+
|
| 593 |
+
if np.any(np.isnan([imin, imax, omin, omax])):
|
| 594 |
+
utils.warn(
|
| 595 |
+
"One or more intensity levels are NaN. Rescaling will broadcast "
|
| 596 |
+
"NaN to the full image. Provide intensity levels yourself to "
|
| 597 |
+
"avoid this. E.g. with np.nanmin(image), np.nanmax(image).",
|
| 598 |
+
stacklevel=2,
|
| 599 |
+
)
|
| 600 |
+
|
| 601 |
+
image = np.clip(image, imin, imax)
|
| 602 |
+
|
| 603 |
+
if imin != imax:
|
| 604 |
+
image = (image - imin) / (imax - imin)
|
| 605 |
+
return (image * (omax - omin) + omin).astype(out_dtype)
|
| 606 |
+
else:
|
| 607 |
+
return np.clip(image, omin, omax).astype(out_dtype)
|
| 608 |
+
|
| 609 |
+
|
| 610 |
+
def _assert_non_negative(image):
|
| 611 |
+
if np.any(image < 0):
|
| 612 |
+
raise ValueError(
|
| 613 |
+
'Image Correction methods work correctly only on '
|
| 614 |
+
'images with non-negative values. Use '
|
| 615 |
+
'skimage.exposure.rescale_intensity.'
|
| 616 |
+
)
|
| 617 |
+
|
| 618 |
+
|
| 619 |
+
def _adjust_gamma_u8(image, gamma, gain):
|
| 620 |
+
"""LUT based implementation of gamma adjustment."""
|
| 621 |
+
lut = 255 * gain * (np.linspace(0, 1, 256) ** gamma)
|
| 622 |
+
lut = np.minimum(np.rint(lut), 255).astype('uint8')
|
| 623 |
+
return lut[image]
|
| 624 |
+
|
| 625 |
+
|
| 626 |
+
def adjust_gamma(image, gamma=1, gain=1):
|
| 627 |
+
"""Perform gamma correction on the input image.
|
| 628 |
+
|
| 629 |
+
Gamma correction is a power-law transform [1]_. This function
|
| 630 |
+
transforms the input `image` pixel-wise according to the power law
|
| 631 |
+
``image**gamma`` after scaling each pixel to the range 0 to 1. Then
|
| 632 |
+
it is rescaled to its original range and muliplied by `gain`.
|
| 633 |
+
|
| 634 |
+
Parameters
|
| 635 |
+
----------
|
| 636 |
+
image : ndarray
|
| 637 |
+
Input image.
|
| 638 |
+
gamma : float, optional
|
| 639 |
+
Non negative real number. Default value is 1.
|
| 640 |
+
gain : float, optional
|
| 641 |
+
The constant multiplier. Default value is 1.
|
| 642 |
+
|
| 643 |
+
Returns
|
| 644 |
+
-------
|
| 645 |
+
out : ndarray
|
| 646 |
+
Gamma corrected output image.
|
| 647 |
+
|
| 648 |
+
See Also
|
| 649 |
+
--------
|
| 650 |
+
adjust_log
|
| 651 |
+
|
| 652 |
+
Notes
|
| 653 |
+
-----
|
| 654 |
+
For gamma greater than 1, the histogram will shift towards left and
|
| 655 |
+
the output image will be darker than the input image.
|
| 656 |
+
|
| 657 |
+
For gamma less than 1, the histogram will shift towards right and
|
| 658 |
+
the output image will be brighter than the input image.
|
| 659 |
+
|
| 660 |
+
References
|
| 661 |
+
----------
|
| 662 |
+
.. [1] https://en.wikipedia.org/wiki/Gamma_correction
|
| 663 |
+
|
| 664 |
+
Examples
|
| 665 |
+
--------
|
| 666 |
+
>>> import skimage as ski
|
| 667 |
+
>>> image = ski.util.img_as_float(ski.data.moon())
|
| 668 |
+
>>> gamma_corrected = ski.exposure.adjust_gamma(image, 2)
|
| 669 |
+
>>> # Output is darker for gamma > 1
|
| 670 |
+
>>> image.mean() > gamma_corrected.mean()
|
| 671 |
+
True
|
| 672 |
+
"""
|
| 673 |
+
if gamma < 0:
|
| 674 |
+
raise ValueError("Gamma should be a non-negative real number.")
|
| 675 |
+
|
| 676 |
+
dtype = image.dtype.type
|
| 677 |
+
|
| 678 |
+
if dtype is np.uint8:
|
| 679 |
+
out = _adjust_gamma_u8(image, gamma, gain)
|
| 680 |
+
else:
|
| 681 |
+
_assert_non_negative(image)
|
| 682 |
+
|
| 683 |
+
limits = dtype_limits(image, clip_negative=True)
|
| 684 |
+
scale = float(limits[1] - limits[0])
|
| 685 |
+
|
| 686 |
+
out = (((image / scale) ** gamma) * scale * gain).astype(dtype)
|
| 687 |
+
|
| 688 |
+
return out
|
| 689 |
+
|
| 690 |
+
|
| 691 |
+
def adjust_log(image, gain=1, inv=False):
|
| 692 |
+
"""Performs Logarithmic correction on the input image.
|
| 693 |
+
|
| 694 |
+
This function transforms the input image pixelwise according to the
|
| 695 |
+
equation ``O = gain*log(1 + I)`` after scaling each pixel to the range
|
| 696 |
+
0 to 1. For inverse logarithmic correction, the equation is
|
| 697 |
+
``O = gain*(2**I - 1)``.
|
| 698 |
+
|
| 699 |
+
Parameters
|
| 700 |
+
----------
|
| 701 |
+
image : ndarray
|
| 702 |
+
Input image.
|
| 703 |
+
gain : float, optional
|
| 704 |
+
The constant multiplier. Default value is 1.
|
| 705 |
+
inv : float, optional
|
| 706 |
+
If True, it performs inverse logarithmic correction,
|
| 707 |
+
else correction will be logarithmic. Defaults to False.
|
| 708 |
+
|
| 709 |
+
Returns
|
| 710 |
+
-------
|
| 711 |
+
out : ndarray
|
| 712 |
+
Logarithm corrected output image.
|
| 713 |
+
|
| 714 |
+
See Also
|
| 715 |
+
--------
|
| 716 |
+
adjust_gamma
|
| 717 |
+
|
| 718 |
+
References
|
| 719 |
+
----------
|
| 720 |
+
.. [1] http://www.ece.ucsb.edu/Faculty/Manjunath/courses/ece178W03/EnhancePart1.pdf
|
| 721 |
+
|
| 722 |
+
"""
|
| 723 |
+
_assert_non_negative(image)
|
| 724 |
+
dtype = image.dtype.type
|
| 725 |
+
scale = float(dtype_limits(image, True)[1] - dtype_limits(image, True)[0])
|
| 726 |
+
|
| 727 |
+
if inv:
|
| 728 |
+
out = (2 ** (image / scale) - 1) * scale * gain
|
| 729 |
+
return dtype(out)
|
| 730 |
+
|
| 731 |
+
out = np.log2(1 + image / scale) * scale * gain
|
| 732 |
+
return out.astype(dtype)
|
| 733 |
+
|
| 734 |
+
|
| 735 |
+
def adjust_sigmoid(image, cutoff=0.5, gain=10, inv=False):
|
| 736 |
+
"""Performs Sigmoid Correction on the input image.
|
| 737 |
+
|
| 738 |
+
Also known as Contrast Adjustment.
|
| 739 |
+
This function transforms the input image pixelwise according to the
|
| 740 |
+
equation ``O = 1/(1 + exp*(gain*(cutoff - I)))`` after scaling each pixel
|
| 741 |
+
to the range 0 to 1.
|
| 742 |
+
|
| 743 |
+
Parameters
|
| 744 |
+
----------
|
| 745 |
+
image : ndarray
|
| 746 |
+
Input image.
|
| 747 |
+
cutoff : float, optional
|
| 748 |
+
Cutoff of the sigmoid function that shifts the characteristic curve
|
| 749 |
+
in horizontal direction. Default value is 0.5.
|
| 750 |
+
gain : float, optional
|
| 751 |
+
The constant multiplier in exponential's power of sigmoid function.
|
| 752 |
+
Default value is 10.
|
| 753 |
+
inv : bool, optional
|
| 754 |
+
If True, returns the negative sigmoid correction. Defaults to False.
|
| 755 |
+
|
| 756 |
+
Returns
|
| 757 |
+
-------
|
| 758 |
+
out : ndarray
|
| 759 |
+
Sigmoid corrected output image.
|
| 760 |
+
|
| 761 |
+
See Also
|
| 762 |
+
--------
|
| 763 |
+
adjust_gamma
|
| 764 |
+
|
| 765 |
+
References
|
| 766 |
+
----------
|
| 767 |
+
.. [1] Gustav J. Braun, "Image Lightness Rescaling Using Sigmoidal Contrast
|
| 768 |
+
Enhancement Functions",
|
| 769 |
+
http://markfairchild.org/PDFs/PAP07.pdf
|
| 770 |
+
|
| 771 |
+
"""
|
| 772 |
+
_assert_non_negative(image)
|
| 773 |
+
dtype = image.dtype.type
|
| 774 |
+
scale = float(dtype_limits(image, True)[1] - dtype_limits(image, True)[0])
|
| 775 |
+
|
| 776 |
+
if inv:
|
| 777 |
+
out = (1 - 1 / (1 + np.exp(gain * (cutoff - image / scale)))) * scale
|
| 778 |
+
return dtype(out)
|
| 779 |
+
|
| 780 |
+
out = (1 / (1 + np.exp(gain * (cutoff - image / scale)))) * scale
|
| 781 |
+
return out.astype(dtype)
|
| 782 |
+
|
| 783 |
+
|
| 784 |
+
def is_low_contrast(
|
| 785 |
+
image,
|
| 786 |
+
fraction_threshold=0.05,
|
| 787 |
+
lower_percentile=1,
|
| 788 |
+
upper_percentile=99,
|
| 789 |
+
method='linear',
|
| 790 |
+
):
|
| 791 |
+
"""Determine if an image is low contrast.
|
| 792 |
+
|
| 793 |
+
Parameters
|
| 794 |
+
----------
|
| 795 |
+
image : array-like
|
| 796 |
+
The image under test.
|
| 797 |
+
fraction_threshold : float, optional
|
| 798 |
+
The low contrast fraction threshold. An image is considered low-
|
| 799 |
+
contrast when its range of brightness spans less than this
|
| 800 |
+
fraction of its data type's full range. [1]_
|
| 801 |
+
lower_percentile : float, optional
|
| 802 |
+
Disregard values below this percentile when computing image contrast.
|
| 803 |
+
upper_percentile : float, optional
|
| 804 |
+
Disregard values above this percentile when computing image contrast.
|
| 805 |
+
method : str, optional
|
| 806 |
+
The contrast determination method. Right now the only available
|
| 807 |
+
option is "linear".
|
| 808 |
+
|
| 809 |
+
Returns
|
| 810 |
+
-------
|
| 811 |
+
out : bool
|
| 812 |
+
True when the image is determined to be low contrast.
|
| 813 |
+
|
| 814 |
+
Notes
|
| 815 |
+
-----
|
| 816 |
+
For boolean images, this function returns False only if all values are
|
| 817 |
+
the same (the method, threshold, and percentile arguments are ignored).
|
| 818 |
+
|
| 819 |
+
References
|
| 820 |
+
----------
|
| 821 |
+
.. [1] https://scikit-image.org/docs/dev/user_guide/data_types.html
|
| 822 |
+
|
| 823 |
+
Examples
|
| 824 |
+
--------
|
| 825 |
+
>>> image = np.linspace(0, 0.04, 100)
|
| 826 |
+
>>> is_low_contrast(image)
|
| 827 |
+
True
|
| 828 |
+
>>> image[-1] = 1
|
| 829 |
+
>>> is_low_contrast(image)
|
| 830 |
+
True
|
| 831 |
+
>>> is_low_contrast(image, upper_percentile=100)
|
| 832 |
+
False
|
| 833 |
+
"""
|
| 834 |
+
image = np.asanyarray(image)
|
| 835 |
+
|
| 836 |
+
if image.dtype == bool:
|
| 837 |
+
return not ((image.max() == 1) and (image.min() == 0))
|
| 838 |
+
|
| 839 |
+
if image.ndim == 3:
|
| 840 |
+
from ..color import rgb2gray, rgba2rgb # avoid circular import
|
| 841 |
+
|
| 842 |
+
if image.shape[2] == 4:
|
| 843 |
+
image = rgba2rgb(image)
|
| 844 |
+
if image.shape[2] == 3:
|
| 845 |
+
image = rgb2gray(image)
|
| 846 |
+
|
| 847 |
+
dlimits = dtype_limits(image, clip_negative=False)
|
| 848 |
+
limits = np.percentile(image, [lower_percentile, upper_percentile])
|
| 849 |
+
ratio = (limits[1] - limits[0]) / (dlimits[1] - dlimits[0])
|
| 850 |
+
|
| 851 |
+
return ratio < fraction_threshold
|
envs/kitoverlay/skimage/exposure/histogram_matching.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
from .._shared import utils
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def _match_cumulative_cdf(source, template):
|
| 7 |
+
"""
|
| 8 |
+
Return modified source array so that the cumulative density function of
|
| 9 |
+
its values matches the cumulative density function of the template.
|
| 10 |
+
"""
|
| 11 |
+
if source.dtype.kind == 'u':
|
| 12 |
+
src_lookup = source.reshape(-1)
|
| 13 |
+
src_counts = np.bincount(src_lookup)
|
| 14 |
+
tmpl_counts = np.bincount(template.reshape(-1))
|
| 15 |
+
|
| 16 |
+
# omit values where the count was 0
|
| 17 |
+
tmpl_values = np.nonzero(tmpl_counts)[0]
|
| 18 |
+
tmpl_counts = tmpl_counts[tmpl_values]
|
| 19 |
+
else:
|
| 20 |
+
src_values, src_lookup, src_counts = np.unique(
|
| 21 |
+
source.reshape(-1), return_inverse=True, return_counts=True
|
| 22 |
+
)
|
| 23 |
+
tmpl_values, tmpl_counts = np.unique(template.reshape(-1), return_counts=True)
|
| 24 |
+
|
| 25 |
+
# calculate normalized quantiles for each array
|
| 26 |
+
src_quantiles = np.cumsum(src_counts) / source.size
|
| 27 |
+
tmpl_quantiles = np.cumsum(tmpl_counts) / template.size
|
| 28 |
+
|
| 29 |
+
interp_a_values = np.interp(src_quantiles, tmpl_quantiles, tmpl_values)
|
| 30 |
+
return interp_a_values[src_lookup].reshape(source.shape)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
@utils.channel_as_last_axis(channel_arg_positions=(0, 1))
|
| 34 |
+
def match_histograms(image, reference, *, channel_axis=None):
|
| 35 |
+
"""Adjust an image so that its cumulative histogram matches that of another.
|
| 36 |
+
|
| 37 |
+
The adjustment is applied separately for each channel.
|
| 38 |
+
|
| 39 |
+
Parameters
|
| 40 |
+
----------
|
| 41 |
+
image : ndarray
|
| 42 |
+
Input image. Can be gray-scale or in color.
|
| 43 |
+
reference : ndarray
|
| 44 |
+
Image to match histogram of. Must have the same number of channels as
|
| 45 |
+
image.
|
| 46 |
+
channel_axis : int or None, optional
|
| 47 |
+
If None, the image is assumed to be a grayscale (single channel) image.
|
| 48 |
+
Otherwise, this parameter indicates which axis of the array corresponds
|
| 49 |
+
to channels.
|
| 50 |
+
|
| 51 |
+
Returns
|
| 52 |
+
-------
|
| 53 |
+
matched : ndarray
|
| 54 |
+
Transformed input image.
|
| 55 |
+
|
| 56 |
+
Raises
|
| 57 |
+
------
|
| 58 |
+
ValueError
|
| 59 |
+
Thrown when the number of channels in the input image and the reference
|
| 60 |
+
differ.
|
| 61 |
+
|
| 62 |
+
References
|
| 63 |
+
----------
|
| 64 |
+
.. [1] http://paulbourke.net/miscellaneous/equalisation/
|
| 65 |
+
|
| 66 |
+
"""
|
| 67 |
+
if image.ndim != reference.ndim:
|
| 68 |
+
raise ValueError(
|
| 69 |
+
'Image and reference must have the same number ' 'of channels.'
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
if channel_axis is not None:
|
| 73 |
+
if image.shape[-1] != reference.shape[-1]:
|
| 74 |
+
raise ValueError(
|
| 75 |
+
'Number of channels in the input image and '
|
| 76 |
+
'reference image must match!'
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
matched = np.empty(image.shape, dtype=image.dtype)
|
| 80 |
+
for channel in range(image.shape[-1]):
|
| 81 |
+
matched_channel = _match_cumulative_cdf(
|
| 82 |
+
image[..., channel], reference[..., channel]
|
| 83 |
+
)
|
| 84 |
+
matched[..., channel] = matched_channel
|
| 85 |
+
else:
|
| 86 |
+
# _match_cumulative_cdf will always return float64 due to np.interp
|
| 87 |
+
matched = _match_cumulative_cdf(image, reference)
|
| 88 |
+
|
| 89 |
+
if matched.dtype.kind == 'f':
|
| 90 |
+
# output a float32 result when the input is float16 or float32
|
| 91 |
+
out_dtype = utils._supported_float_type(image.dtype)
|
| 92 |
+
matched = matched.astype(out_dtype, copy=False)
|
| 93 |
+
return matched
|
envs/kitoverlay/skimage/filters/__init__.py
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Sharpening, edge finding, rank filters, thresholding, etc."""
|
| 2 |
+
|
| 3 |
+
import lazy_loader as _lazy
|
| 4 |
+
|
| 5 |
+
__getattr__, __dir__, __all__ = _lazy.attach_stub(__name__, __file__)
|
envs/kitoverlay/skimage/filters/__init__.pyi
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Explicitly setting `__all__` is necessary for type inference engines
|
| 2 |
+
# to know which symbols are exported. See
|
| 3 |
+
# https://peps.python.org/pep-0484/#stub-files
|
| 4 |
+
|
| 5 |
+
__all__ = [
|
| 6 |
+
"LPIFilter2D",
|
| 7 |
+
"apply_hysteresis_threshold",
|
| 8 |
+
"butterworth",
|
| 9 |
+
"correlate_sparse",
|
| 10 |
+
"difference_of_gaussians",
|
| 11 |
+
"farid",
|
| 12 |
+
"farid_h",
|
| 13 |
+
"farid_v",
|
| 14 |
+
"filter_inverse",
|
| 15 |
+
"filter_forward",
|
| 16 |
+
"frangi",
|
| 17 |
+
"gabor",
|
| 18 |
+
"gabor_kernel",
|
| 19 |
+
"gaussian",
|
| 20 |
+
"hessian",
|
| 21 |
+
"laplace",
|
| 22 |
+
"median",
|
| 23 |
+
"meijering",
|
| 24 |
+
"prewitt",
|
| 25 |
+
"prewitt_h",
|
| 26 |
+
"prewitt_v",
|
| 27 |
+
"rank",
|
| 28 |
+
"rank_order",
|
| 29 |
+
"roberts",
|
| 30 |
+
"roberts_neg_diag",
|
| 31 |
+
"roberts_pos_diag",
|
| 32 |
+
"sato",
|
| 33 |
+
"scharr",
|
| 34 |
+
"scharr_h",
|
| 35 |
+
"scharr_v",
|
| 36 |
+
"sobel",
|
| 37 |
+
"sobel_h",
|
| 38 |
+
"sobel_v",
|
| 39 |
+
"threshold_isodata",
|
| 40 |
+
"threshold_li",
|
| 41 |
+
"threshold_local",
|
| 42 |
+
"threshold_mean",
|
| 43 |
+
"threshold_minimum",
|
| 44 |
+
"threshold_multiotsu",
|
| 45 |
+
"threshold_niblack",
|
| 46 |
+
"threshold_otsu",
|
| 47 |
+
"threshold_sauvola",
|
| 48 |
+
"threshold_triangle",
|
| 49 |
+
"threshold_yen",
|
| 50 |
+
"try_all_threshold",
|
| 51 |
+
"unsharp_mask",
|
| 52 |
+
"wiener",
|
| 53 |
+
"window",
|
| 54 |
+
]
|
| 55 |
+
|
| 56 |
+
from . import rank
|
| 57 |
+
from ._fft_based import butterworth
|
| 58 |
+
from ._gabor import gabor, gabor_kernel
|
| 59 |
+
from ._gaussian import difference_of_gaussians, gaussian
|
| 60 |
+
from ._median import median
|
| 61 |
+
from ._rank_order import rank_order
|
| 62 |
+
from ._sparse import correlate_sparse
|
| 63 |
+
from ._unsharp_mask import unsharp_mask
|
| 64 |
+
from ._window import window
|
| 65 |
+
from .edges import (
|
| 66 |
+
farid,
|
| 67 |
+
farid_h,
|
| 68 |
+
farid_v,
|
| 69 |
+
laplace,
|
| 70 |
+
prewitt,
|
| 71 |
+
prewitt_h,
|
| 72 |
+
prewitt_v,
|
| 73 |
+
roberts,
|
| 74 |
+
roberts_neg_diag,
|
| 75 |
+
roberts_pos_diag,
|
| 76 |
+
scharr,
|
| 77 |
+
scharr_h,
|
| 78 |
+
scharr_v,
|
| 79 |
+
sobel,
|
| 80 |
+
sobel_h,
|
| 81 |
+
sobel_v,
|
| 82 |
+
)
|
| 83 |
+
from .lpi_filter import (
|
| 84 |
+
LPIFilter2D,
|
| 85 |
+
filter_inverse,
|
| 86 |
+
filter_forward,
|
| 87 |
+
wiener,
|
| 88 |
+
)
|
| 89 |
+
from .ridges import (
|
| 90 |
+
frangi,
|
| 91 |
+
hessian,
|
| 92 |
+
meijering,
|
| 93 |
+
sato,
|
| 94 |
+
)
|
| 95 |
+
from .thresholding import (
|
| 96 |
+
apply_hysteresis_threshold,
|
| 97 |
+
threshold_isodata,
|
| 98 |
+
threshold_li,
|
| 99 |
+
threshold_local,
|
| 100 |
+
threshold_mean,
|
| 101 |
+
threshold_minimum,
|
| 102 |
+
threshold_multiotsu,
|
| 103 |
+
threshold_niblack,
|
| 104 |
+
threshold_otsu,
|
| 105 |
+
threshold_sauvola,
|
| 106 |
+
threshold_triangle,
|
| 107 |
+
threshold_yen,
|
| 108 |
+
try_all_threshold,
|
| 109 |
+
)
|
envs/kitoverlay/skimage/filters/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (420 Bytes). View file
|
|
|
envs/kitoverlay/skimage/filters/__pycache__/_fft_based.cpython-311.pyc
ADDED
|
Binary file (8.08 kB). View file
|
|
|
envs/kitoverlay/skimage/filters/__pycache__/_gabor.cpython-311.pyc
ADDED
|
Binary file (9.45 kB). View file
|
|
|
envs/kitoverlay/skimage/filters/__pycache__/_gaussian.cpython-311.pyc
ADDED
|
Binary file (6.39 kB). View file
|
|
|
envs/kitoverlay/skimage/filters/__pycache__/_median.cpython-311.pyc
ADDED
|
Binary file (3.45 kB). View file
|
|
|
envs/kitoverlay/skimage/filters/__pycache__/_rank_order.cpython-311.pyc
ADDED
|
Binary file (2.81 kB). View file
|
|
|
envs/kitoverlay/skimage/filters/__pycache__/_sparse.cpython-311.pyc
ADDED
|
Binary file (5.98 kB). View file
|
|
|
envs/kitoverlay/skimage/filters/__pycache__/_unsharp_mask.cpython-311.pyc
ADDED
|
Binary file (6.22 kB). View file
|
|
|
envs/kitoverlay/skimage/filters/__pycache__/_window.cpython-311.pyc
ADDED
|
Binary file (5.68 kB). View file
|
|
|
envs/kitoverlay/skimage/filters/__pycache__/edges.cpython-311.pyc
ADDED
|
Binary file (29.4 kB). View file
|
|
|
envs/kitoverlay/skimage/filters/__pycache__/lpi_filter.cpython-311.pyc
ADDED
|
Binary file (11.7 kB). View file
|
|
|
envs/kitoverlay/skimage/filters/__pycache__/ridges.cpython-311.pyc
ADDED
|
Binary file (14.9 kB). View file
|
|
|
envs/kitoverlay/skimage/filters/__pycache__/thresholding.cpython-311.pyc
ADDED
|
Binary file (51.8 kB). View file
|
|
|
envs/kitoverlay/skimage/filters/_fft_based.py
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import functools
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
import scipy.fft as fft
|
| 5 |
+
|
| 6 |
+
from .._shared.utils import _supported_float_type
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def _get_nd_butterworth_filter(
|
| 10 |
+
shape, factor, order, high_pass, real, dtype=np.float64, squared_butterworth=True
|
| 11 |
+
):
|
| 12 |
+
"""Create a N-dimensional Butterworth mask for an FFT
|
| 13 |
+
|
| 14 |
+
Parameters
|
| 15 |
+
----------
|
| 16 |
+
shape : tuple of int
|
| 17 |
+
Shape of the n-dimensional FFT and mask.
|
| 18 |
+
factor : float
|
| 19 |
+
Fraction of mask dimensions where the cutoff should be.
|
| 20 |
+
order : float
|
| 21 |
+
Controls the slope in the cutoff region.
|
| 22 |
+
high_pass : bool
|
| 23 |
+
Whether the filter is high pass (low frequencies attenuated) or
|
| 24 |
+
low pass (high frequencies are attenuated).
|
| 25 |
+
real : bool
|
| 26 |
+
Whether the FFT is of a real (True) or complex (False) image
|
| 27 |
+
squared_butterworth : bool, optional
|
| 28 |
+
When True, the square of the Butterworth filter is used.
|
| 29 |
+
|
| 30 |
+
Returns
|
| 31 |
+
-------
|
| 32 |
+
wfilt : ndarray
|
| 33 |
+
The FFT mask.
|
| 34 |
+
|
| 35 |
+
"""
|
| 36 |
+
ranges = []
|
| 37 |
+
for i, d in enumerate(shape):
|
| 38 |
+
# start and stop ensures center of mask aligns with center of FFT
|
| 39 |
+
axis = np.arange(-(d - 1) // 2, (d - 1) // 2 + 1) / (d * factor)
|
| 40 |
+
ranges.append(fft.ifftshift(axis**2))
|
| 41 |
+
# for real image FFT, halve the last axis
|
| 42 |
+
if real:
|
| 43 |
+
limit = d // 2 + 1
|
| 44 |
+
ranges[-1] = ranges[-1][:limit]
|
| 45 |
+
# q2 = squared Euclidean distance grid
|
| 46 |
+
q2 = functools.reduce(np.add, np.meshgrid(*ranges, indexing="ij", sparse=True))
|
| 47 |
+
q2 = q2.astype(dtype)
|
| 48 |
+
q2 = np.power(q2, order)
|
| 49 |
+
wfilt = 1 / (1 + q2)
|
| 50 |
+
if high_pass:
|
| 51 |
+
wfilt *= q2
|
| 52 |
+
if not squared_butterworth:
|
| 53 |
+
np.sqrt(wfilt, out=wfilt)
|
| 54 |
+
return wfilt
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def butterworth(
|
| 58 |
+
image,
|
| 59 |
+
cutoff_frequency_ratio=0.005,
|
| 60 |
+
high_pass=True,
|
| 61 |
+
order=2.0,
|
| 62 |
+
channel_axis=None,
|
| 63 |
+
*,
|
| 64 |
+
squared_butterworth=True,
|
| 65 |
+
npad=0,
|
| 66 |
+
):
|
| 67 |
+
"""Apply a Butterworth filter to enhance high or low frequency features.
|
| 68 |
+
|
| 69 |
+
This filter is defined in the Fourier domain.
|
| 70 |
+
|
| 71 |
+
Parameters
|
| 72 |
+
----------
|
| 73 |
+
image : (M[, N[, ..., P]][, C]) ndarray
|
| 74 |
+
Input image.
|
| 75 |
+
cutoff_frequency_ratio : float, optional
|
| 76 |
+
Determines the position of the cut-off relative to the shape of the
|
| 77 |
+
FFT. Receives a value between [0, 0.5].
|
| 78 |
+
high_pass : bool, optional
|
| 79 |
+
Whether to perform a high pass filter. If False, a low pass filter is
|
| 80 |
+
performed.
|
| 81 |
+
order : float, optional
|
| 82 |
+
Order of the filter which affects the slope near the cut-off. Higher
|
| 83 |
+
order means steeper slope in frequency space.
|
| 84 |
+
channel_axis : int, optional
|
| 85 |
+
If there is a channel dimension, provide the index here. If None
|
| 86 |
+
(default) then all axes are assumed to be spatial dimensions.
|
| 87 |
+
squared_butterworth : bool, optional
|
| 88 |
+
When True, the square of a Butterworth filter is used. See notes below
|
| 89 |
+
for more details.
|
| 90 |
+
npad : int, optional
|
| 91 |
+
Pad each edge of the image by `npad` pixels using `numpy.pad`'s
|
| 92 |
+
``mode='edge'`` extension.
|
| 93 |
+
|
| 94 |
+
Returns
|
| 95 |
+
-------
|
| 96 |
+
result : ndarray
|
| 97 |
+
The Butterworth-filtered image.
|
| 98 |
+
|
| 99 |
+
Notes
|
| 100 |
+
-----
|
| 101 |
+
A band-pass filter can be achieved by combining a high-pass and low-pass
|
| 102 |
+
filter. The user can increase `npad` if boundary artifacts are apparent.
|
| 103 |
+
|
| 104 |
+
The "Butterworth filter" used in image processing textbooks (e.g. [1]_,
|
| 105 |
+
[2]_) is often the square of the traditional Butterworth filters as
|
| 106 |
+
described by [3]_, [4]_. The squared version will be used here if
|
| 107 |
+
`squared_butterworth` is set to ``True``. The lowpass, squared Butterworth
|
| 108 |
+
filter is given by the following expression for the lowpass case:
|
| 109 |
+
|
| 110 |
+
.. math::
|
| 111 |
+
H_{low}(f) = \\frac{1}{1 + \\left(\\frac{f}{c f_s}\\right)^{2n}}
|
| 112 |
+
|
| 113 |
+
with the highpass case given by
|
| 114 |
+
|
| 115 |
+
.. math::
|
| 116 |
+
H_{hi}(f) = 1 - H_{low}(f)
|
| 117 |
+
|
| 118 |
+
where :math:`f=\\sqrt{\\sum_{d=0}^{\\mathrm{ndim}} f_{d}^{2}}` is the
|
| 119 |
+
absolute value of the spatial frequency, :math:`f_s` is the sampling
|
| 120 |
+
frequency, :math:`c` the ``cutoff_frequency_ratio``, and :math:`n` is the
|
| 121 |
+
filter `order` [1]_. When ``squared_butterworth=False``, the square root of
|
| 122 |
+
the above expressions are used instead.
|
| 123 |
+
|
| 124 |
+
Note that ``cutoff_frequency_ratio`` is defined in terms of the sampling
|
| 125 |
+
frequency, :math:`f_s`. The FFT spectrum covers the Nyquist range
|
| 126 |
+
(:math:`[-f_s/2, f_s/2]`) so ``cutoff_frequency_ratio`` should have a value
|
| 127 |
+
between 0 and 0.5. The frequency response (gain) at the cutoff is 0.5 when
|
| 128 |
+
``squared_butterworth`` is true and :math:`1/\\sqrt{2}` when it is false.
|
| 129 |
+
|
| 130 |
+
Examples
|
| 131 |
+
--------
|
| 132 |
+
Apply a high-pass and low-pass Butterworth filter to a grayscale and
|
| 133 |
+
color image respectively:
|
| 134 |
+
|
| 135 |
+
>>> from skimage.data import camera, astronaut
|
| 136 |
+
>>> from skimage.filters import butterworth
|
| 137 |
+
>>> high_pass = butterworth(camera(), 0.07, True, 8)
|
| 138 |
+
>>> low_pass = butterworth(astronaut(), 0.01, False, 4, channel_axis=-1)
|
| 139 |
+
|
| 140 |
+
References
|
| 141 |
+
----------
|
| 142 |
+
.. [1] Russ, John C., et al. The Image Processing Handbook, 3rd. Ed.
|
| 143 |
+
1999, CRC Press, LLC.
|
| 144 |
+
.. [2] Birchfield, Stan. Image Processing and Analysis. 2018. Cengage
|
| 145 |
+
Learning.
|
| 146 |
+
.. [3] Butterworth, Stephen. "On the theory of filter amplifiers."
|
| 147 |
+
Wireless Engineer 7.6 (1930): 536-541.
|
| 148 |
+
.. [4] https://en.wikipedia.org/wiki/Butterworth_filter
|
| 149 |
+
|
| 150 |
+
"""
|
| 151 |
+
if npad < 0:
|
| 152 |
+
raise ValueError("npad must be >= 0")
|
| 153 |
+
elif npad > 0:
|
| 154 |
+
center_slice = tuple(slice(npad, s + npad) for s in image.shape)
|
| 155 |
+
image = np.pad(image, npad, mode='edge')
|
| 156 |
+
fft_shape = (
|
| 157 |
+
image.shape if channel_axis is None else np.delete(image.shape, channel_axis)
|
| 158 |
+
)
|
| 159 |
+
is_real = np.isrealobj(image)
|
| 160 |
+
float_dtype = _supported_float_type(image.dtype, allow_complex=True)
|
| 161 |
+
if cutoff_frequency_ratio < 0 or cutoff_frequency_ratio > 0.5:
|
| 162 |
+
raise ValueError("cutoff_frequency_ratio should be in the range [0, 0.5]")
|
| 163 |
+
wfilt = _get_nd_butterworth_filter(
|
| 164 |
+
fft_shape,
|
| 165 |
+
cutoff_frequency_ratio,
|
| 166 |
+
order,
|
| 167 |
+
high_pass,
|
| 168 |
+
is_real,
|
| 169 |
+
float_dtype,
|
| 170 |
+
squared_butterworth,
|
| 171 |
+
)
|
| 172 |
+
axes = np.arange(image.ndim)
|
| 173 |
+
if channel_axis is not None:
|
| 174 |
+
axes = np.delete(axes, channel_axis)
|
| 175 |
+
abs_channel = channel_axis % image.ndim
|
| 176 |
+
post = image.ndim - abs_channel - 1
|
| 177 |
+
sl = (slice(None),) * abs_channel + (np.newaxis,) + (slice(None),) * post
|
| 178 |
+
wfilt = wfilt[sl]
|
| 179 |
+
if is_real:
|
| 180 |
+
butterfilt = fft.irfftn(
|
| 181 |
+
wfilt * fft.rfftn(image, axes=axes), s=fft_shape, axes=axes
|
| 182 |
+
)
|
| 183 |
+
else:
|
| 184 |
+
butterfilt = fft.ifftn(
|
| 185 |
+
wfilt * fft.fftn(image, axes=axes), s=fft_shape, axes=axes
|
| 186 |
+
)
|
| 187 |
+
if npad > 0:
|
| 188 |
+
butterfilt = butterfilt[center_slice]
|
| 189 |
+
return butterfilt
|
envs/kitoverlay/skimage/filters/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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import math
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
from scipy import ndimage as ndi
|
| 5 |
+
|
| 6 |
+
from .._shared.utils import _supported_float_type, check_nD
|
| 7 |
+
|
| 8 |
+
__all__ = ['gabor_kernel', 'gabor']
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def _sigma_prefactor(bandwidth):
|
| 12 |
+
b = bandwidth
|
| 13 |
+
# See http://www.cs.rug.nl/~imaging/simplecell.html
|
| 14 |
+
return 1.0 / np.pi * math.sqrt(math.log(2) / 2.0) * (2.0**b + 1) / (2.0**b - 1)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def gabor_kernel(
|
| 18 |
+
frequency,
|
| 19 |
+
theta=0,
|
| 20 |
+
bandwidth=1,
|
| 21 |
+
sigma_x=None,
|
| 22 |
+
sigma_y=None,
|
| 23 |
+
n_stds=3,
|
| 24 |
+
offset=0,
|
| 25 |
+
dtype=np.complex128,
|
| 26 |
+
):
|
| 27 |
+
"""Return complex 2D Gabor filter kernel.
|
| 28 |
+
|
| 29 |
+
Gabor kernel is a Gaussian kernel modulated by a complex harmonic function.
|
| 30 |
+
Harmonic function consists of an imaginary sine function and a real
|
| 31 |
+
cosine function. Spatial frequency is inversely proportional to the
|
| 32 |
+
wavelength of the harmonic and to the standard deviation of a Gaussian
|
| 33 |
+
kernel. The bandwidth is also inversely proportional to the standard
|
| 34 |
+
deviation.
|
| 35 |
+
|
| 36 |
+
Parameters
|
| 37 |
+
----------
|
| 38 |
+
frequency : float
|
| 39 |
+
Spatial frequency of the harmonic function. Specified in pixels.
|
| 40 |
+
theta : float, optional
|
| 41 |
+
Orientation in radians. If 0, the harmonic is in the x-direction.
|
| 42 |
+
bandwidth : float, optional
|
| 43 |
+
The bandwidth captured by the filter. For fixed bandwidth, ``sigma_x``
|
| 44 |
+
and ``sigma_y`` will decrease with increasing frequency. This value is
|
| 45 |
+
ignored if ``sigma_x`` and ``sigma_y`` are set by the user.
|
| 46 |
+
sigma_x, sigma_y : float, optional
|
| 47 |
+
Standard deviation in x- and y-directions. These directions apply to
|
| 48 |
+
the kernel *before* rotation. If `theta = pi/2`, then the kernel is
|
| 49 |
+
rotated 90 degrees so that ``sigma_x`` controls the *vertical*
|
| 50 |
+
direction.
|
| 51 |
+
n_stds : scalar, optional
|
| 52 |
+
The linear size of the kernel is n_stds (3 by default) standard
|
| 53 |
+
deviations
|
| 54 |
+
offset : float, optional
|
| 55 |
+
Phase offset of harmonic function in radians.
|
| 56 |
+
dtype : {np.complex64, np.complex128}
|
| 57 |
+
Specifies if the filter is single or double precision complex.
|
| 58 |
+
|
| 59 |
+
Returns
|
| 60 |
+
-------
|
| 61 |
+
g : complex array
|
| 62 |
+
Complex filter kernel.
|
| 63 |
+
|
| 64 |
+
References
|
| 65 |
+
----------
|
| 66 |
+
.. [1] https://en.wikipedia.org/wiki/Gabor_filter
|
| 67 |
+
.. [2] https://web.archive.org/web/20180127125930/http://mplab.ucsd.edu/tutorials/gabor.pdf
|
| 68 |
+
|
| 69 |
+
Examples
|
| 70 |
+
--------
|
| 71 |
+
>>> from skimage.filters import gabor_kernel
|
| 72 |
+
>>> from matplotlib import pyplot as plt # doctest: +SKIP
|
| 73 |
+
|
| 74 |
+
>>> gk = gabor_kernel(frequency=0.2)
|
| 75 |
+
>>> fig, ax = plt.subplots() # doctest: +SKIP
|
| 76 |
+
>>> ax.imshow(gk.real) # doctest: +SKIP
|
| 77 |
+
>>> plt.show() # doctest: +SKIP
|
| 78 |
+
|
| 79 |
+
>>> # more ripples (equivalent to increasing the size of the
|
| 80 |
+
>>> # Gaussian spread)
|
| 81 |
+
>>> gk = gabor_kernel(frequency=0.2, bandwidth=0.1)
|
| 82 |
+
>>> fig, ax = plt.suplots() # doctest: +SKIP
|
| 83 |
+
>>> ax.imshow(gk.real) # doctest: +SKIP
|
| 84 |
+
>>> plt.show() # doctest: +SKIP
|
| 85 |
+
"""
|
| 86 |
+
if sigma_x is None:
|
| 87 |
+
sigma_x = _sigma_prefactor(bandwidth) / frequency
|
| 88 |
+
if sigma_y is None:
|
| 89 |
+
sigma_y = _sigma_prefactor(bandwidth) / frequency
|
| 90 |
+
|
| 91 |
+
if np.dtype(dtype).kind != 'c':
|
| 92 |
+
raise ValueError("dtype must be complex")
|
| 93 |
+
|
| 94 |
+
ct = math.cos(theta)
|
| 95 |
+
st = math.sin(theta)
|
| 96 |
+
x0 = math.ceil(max(abs(n_stds * sigma_x * ct), abs(n_stds * sigma_y * st), 1))
|
| 97 |
+
y0 = math.ceil(max(abs(n_stds * sigma_y * ct), abs(n_stds * sigma_x * st), 1))
|
| 98 |
+
y, x = np.meshgrid(
|
| 99 |
+
np.arange(-y0, y0 + 1), np.arange(-x0, x0 + 1), indexing='ij', sparse=True
|
| 100 |
+
)
|
| 101 |
+
rotx = x * ct + y * st
|
| 102 |
+
roty = -x * st + y * ct
|
| 103 |
+
|
| 104 |
+
g = np.empty(roty.shape, dtype=dtype)
|
| 105 |
+
np.exp(
|
| 106 |
+
-0.5 * (rotx**2 / sigma_x**2 + roty**2 / sigma_y**2)
|
| 107 |
+
+ 1j * (2 * np.pi * frequency * rotx + offset),
|
| 108 |
+
out=g,
|
| 109 |
+
)
|
| 110 |
+
g *= 1 / (2 * np.pi * sigma_x * sigma_y)
|
| 111 |
+
|
| 112 |
+
return g
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def gabor(
|
| 116 |
+
image,
|
| 117 |
+
frequency,
|
| 118 |
+
theta=0,
|
| 119 |
+
bandwidth=1,
|
| 120 |
+
sigma_x=None,
|
| 121 |
+
sigma_y=None,
|
| 122 |
+
n_stds=3,
|
| 123 |
+
offset=0,
|
| 124 |
+
mode='reflect',
|
| 125 |
+
cval=0,
|
| 126 |
+
):
|
| 127 |
+
"""Return real and imaginary responses to Gabor filter.
|
| 128 |
+
|
| 129 |
+
The real and imaginary parts of the Gabor filter kernel are applied to the
|
| 130 |
+
image and the response is returned as a pair of arrays.
|
| 131 |
+
|
| 132 |
+
Gabor filter is a linear filter with a Gaussian kernel which is modulated
|
| 133 |
+
by a sinusoidal plane wave. Frequency and orientation representations of
|
| 134 |
+
the Gabor filter are similar to those of the human visual system.
|
| 135 |
+
Gabor filter banks are commonly used in computer vision and image
|
| 136 |
+
processing. They are especially suitable for edge detection and texture
|
| 137 |
+
classification.
|
| 138 |
+
|
| 139 |
+
Parameters
|
| 140 |
+
----------
|
| 141 |
+
image : 2-D array
|
| 142 |
+
Input image.
|
| 143 |
+
frequency : float
|
| 144 |
+
Spatial frequency of the harmonic function. Specified in pixels.
|
| 145 |
+
theta : float, optional
|
| 146 |
+
Orientation in radians. If 0, the harmonic is in the x-direction.
|
| 147 |
+
bandwidth : float, optional
|
| 148 |
+
The bandwidth captured by the filter. For fixed bandwidth, ``sigma_x``
|
| 149 |
+
and ``sigma_y`` will decrease with increasing frequency. This value is
|
| 150 |
+
ignored if ``sigma_x`` and ``sigma_y`` are set by the user.
|
| 151 |
+
sigma_x, sigma_y : float, optional
|
| 152 |
+
Standard deviation in x- and y-directions. These directions apply to
|
| 153 |
+
the kernel *before* rotation. If `theta = pi/2`, then the kernel is
|
| 154 |
+
rotated 90 degrees so that ``sigma_x`` controls the *vertical*
|
| 155 |
+
direction.
|
| 156 |
+
n_stds : scalar, optional
|
| 157 |
+
The linear size of the kernel is n_stds (3 by default) standard
|
| 158 |
+
deviations.
|
| 159 |
+
offset : float, optional
|
| 160 |
+
Phase offset of harmonic function in radians.
|
| 161 |
+
mode : {'constant', 'nearest', 'reflect', 'mirror', 'wrap'}, optional
|
| 162 |
+
Mode used to convolve image with a kernel, passed to `ndi.convolve`
|
| 163 |
+
cval : scalar, optional
|
| 164 |
+
Value to fill past edges of input if ``mode`` of convolution is
|
| 165 |
+
'constant'. The parameter is passed to `ndi.convolve`.
|
| 166 |
+
|
| 167 |
+
Returns
|
| 168 |
+
-------
|
| 169 |
+
real, imag : arrays
|
| 170 |
+
Filtered images using the real and imaginary parts of the Gabor filter
|
| 171 |
+
kernel. Images are of the same dimensions as the input one.
|
| 172 |
+
|
| 173 |
+
References
|
| 174 |
+
----------
|
| 175 |
+
.. [1] https://en.wikipedia.org/wiki/Gabor_filter
|
| 176 |
+
.. [2] https://web.archive.org/web/20180127125930/http://mplab.ucsd.edu/tutorials/gabor.pdf
|
| 177 |
+
|
| 178 |
+
Examples
|
| 179 |
+
--------
|
| 180 |
+
>>> from skimage.filters import gabor
|
| 181 |
+
>>> from skimage import data
|
| 182 |
+
>>> from matplotlib import pyplot as plt # doctest: +SKIP
|
| 183 |
+
|
| 184 |
+
>>> image = data.coins()
|
| 185 |
+
>>> # detecting edges in a coin image
|
| 186 |
+
>>> filt_real, filt_imag = gabor(image, frequency=0.6)
|
| 187 |
+
>>> fix, ax = plt.subplots() # doctest: +SKIP
|
| 188 |
+
>>> ax.imshow(filt_real) # doctest: +SKIP
|
| 189 |
+
>>> plt.show() # doctest: +SKIP
|
| 190 |
+
|
| 191 |
+
>>> # less sensitivity to finer details with the lower frequency kernel
|
| 192 |
+
>>> filt_real, filt_imag = gabor(image, frequency=0.1)
|
| 193 |
+
>>> fig, ax = plt.subplots() # doctest: +SKIP
|
| 194 |
+
>>> ax.imshow(filt_real) # doctest: +SKIP
|
| 195 |
+
>>> plt.show() # doctest: +SKIP
|
| 196 |
+
"""
|
| 197 |
+
check_nD(image, 2)
|
| 198 |
+
# do not cast integer types to float!
|
| 199 |
+
if image.dtype.kind == 'f':
|
| 200 |
+
float_dtype = _supported_float_type(image.dtype)
|
| 201 |
+
image = image.astype(float_dtype, copy=False)
|
| 202 |
+
kernel_dtype = np.promote_types(image.dtype, np.complex64)
|
| 203 |
+
else:
|
| 204 |
+
kernel_dtype = np.complex128
|
| 205 |
+
|
| 206 |
+
g = gabor_kernel(
|
| 207 |
+
frequency,
|
| 208 |
+
theta,
|
| 209 |
+
bandwidth,
|
| 210 |
+
sigma_x,
|
| 211 |
+
sigma_y,
|
| 212 |
+
n_stds,
|
| 213 |
+
offset,
|
| 214 |
+
dtype=kernel_dtype,
|
| 215 |
+
)
|
| 216 |
+
|
| 217 |
+
filtered_real = ndi.convolve(image, np.real(g), mode=mode, cval=cval)
|
| 218 |
+
filtered_imag = ndi.convolve(image, np.imag(g), mode=mode, cval=cval)
|
| 219 |
+
|
| 220 |
+
return filtered_real, filtered_imag
|