Metadata-Version: 2.5
Name: ColorCorrectionPipeline
Version: 1.5.0
Summary: A step-wise, color correction pipeline for digital images combining flat-field correction, gamma correction, white-balance, and color-correction
Project-URL: Homepage, https://github.com/collinswakholi/ColorCorrectionPackage
Project-URL: Repository, https://github.com/collinswakholi/ColorCorrectionPackage
Project-URL: Bug Tracker, https://github.com/collinswakholi/ColorCorrectionPackage/issues
Project-URL: Documentation, https://github.com/collinswakholi/ColorCorrectionPackage
Author: Collins Wakholi, Devin A. Rippner
License: MIT for source code; bundled detector models declare AGPL-3.0. See included model notices.
License-File: ColorCorrectionPipeline/detection/models/LICENSE-AGPL-3.0.txt
License-File: ColorCorrectionPipeline/detection/models/UPSTREAM-NOTICE.md
License-File: LICENSE
Keywords: color-correction,computer-vision,flat-field,gamma-correction,image-processing,photography,scientific-imaging,white-balance
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Image Processing
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.8
Requires-Dist: colour-science>=0.4.0
Requires-Dist: matplotlib>=3.4.0
Requires-Dist: numpy>=1.21.0
Requires-Dist: onnxruntime-ep-webgpu>=0.4; python_version >= '3.11' and ((sys_platform == 'win32' and (platform_machine == 'AMD64' or platform_machine == 'ARM64')) or (sys_platform == 'linux' and (platform_machine == 'x86_64' or platform_machine == 'aarch64')) or (sys_platform == 'darwin' and platform_machine == 'arm64'))
Requires-Dist: onnxruntime>=1.16; python_version < '3.11'
Requires-Dist: onnxruntime>=1.31; python_version >= '3.11'
Requires-Dist: opencv-contrib-python>=4.5.0
Requires-Dist: pandas>=1.3.0
Requires-Dist: pillow>=9.2
Requires-Dist: plotly>=5.0.0
Requires-Dist: scikit-image>=0.19.0
Requires-Dist: scikit-learn>=1.0.0
Requires-Dist: scipy>=1.7.0
Requires-Dist: seaborn>=0.11.0
Requires-Dist: tifffile>=2021.11.2
Requires-Dist: torch>=1.10.0
Provides-Extra: dev
Requires-Dist: black>=23.0; extra == 'dev'
Requires-Dist: hypothesis>=6.0; extra == 'dev'
Requires-Dist: mypy>=1.0; extra == 'dev'
Requires-Dist: pytest-cov>=4.0; extra == 'dev'
Requires-Dist: pytest>=7.0; extra == 'dev'
Requires-Dist: ruff>=0.1.0; extra == 'dev'
Provides-Extra: docs
Requires-Dist: sphinx-rtd-theme>=1.0; extra == 'docs'
Requires-Dist: sphinx>=5.0; extra == 'docs'
Provides-Extra: performance
Requires-Dist: numba>=0.55.0; extra == 'performance'
Provides-Extra: segmentation
Requires-Dist: colour-checker-detection>=0.1.0; extra == 'segmentation'
Description-Content-Type: text/markdown

# ColorCorrectionPipeline

A stepwise color correction package for scientific images. Fit flat-field
correction (FFC), gamma correction (GC), white balance (WB) and color correction
(CC) from a chart photograph, then apply the calibration to images captured
under the same conditions.

The package builds on [ML_ColorCorrection_tool](https://github.com/collinswakholi/ML_ColorCorrection_tool).
A desktop interface is available in [ColorCorrectionPackage_UI](https://github.com/collinswakholi/ColorCorrectionPackage_UI).

The opt-in [UI-compatible fitting policy](docs/ui-compatible-fitting.md) adds
linear-light CCM, RPCC2/RPCC3, polynomial, PLS and residual NN algorithms while
retaining the existing fitting defaults. `fitting_policy="ui-native-v1"` adds
the native byte-image and FFC/GC fusion rules for new uint8 sRGB calibrations.
Apply that mode with `predict_image` or `predict_batch`; its
[documented limits](docs/ui-compatible-fitting.md#matching-the-native-ui-image-pipeline)
include prepared execution. Existing calibrations keep their recorded rules.

## Installation

Python 3.8 or later is required. The qualification matrix covers Python
3.8–3.12 on Windows, Linux and macOS; newer interpreters are not in that matrix.

```shell
git clone https://github.com/collinswakholi/ColorCorrectionPackage.git
cd ColorCorrectionPackage
python -m pip install -e ".[performance]"
```

The `performance` extra enables optional Numba CPU kernels. Install with
`python -m pip install -e .` for the NumPy fallback. Scientific, Torch, plotting
and ONNX Runtime dependencies are included. The separately published PyPI
package can be installed with `python -m pip install ColorCorrectionPipeline`;
its features depend on the published version.

Chart detection defaults to the bundled Classic24 ONNX model. It prefers a
supported GPU provider and falls back to CPU. See [GPU configuration](docs/gpu-detection.md)
for CUDA, DirectML, WebGPU and CoreML setup and execution diagnostics.
Chart detection and color-transform acceleration have separate backend policies.

## Calibrate and apply

This example uses the bundled sample photographs, explicitly selects their first
frame, and fits a linear-light CCM with native uint8 sRGB image semantics.
Numerical policies are opt-in; existing callers retain their recorded stage
mathematics and return conventions.

```python
from pathlib import Path
from ColorCorrectionPipeline import ColorCorrection, Config
from ColorCorrectionPipeline.io import write_image_data

output = Path("results")
output.mkdir(exist_ok=True)
pipeline = ColorCorrection()
config = Config(
    do_ffc=True, do_gc=True, do_wb=True, do_cc=True,
    check_saturation=False,
    fitting_policy="ui-native-v1",
    image_options={"frame_index": 0},
    FFC_kwargs={"fit_method": "linear", "bins": 30, "smooth_window": 7},
    CC_kwargs={"model": "ccm"},
    save=True, save_path=str(output), model_format="bundle",
)
metrics, images, failed = pipeline.run(
    "Images/Image_1.JPG", "Images/white.JPG", name_="sample", config=config
)
if failed:
    raise RuntimeError(pipeline.last_run)

# Loading a safe bundle restores the numerical policies and reference context.
restored = ColorCorrection().load_bundle(output / "sample_models.ccpb")
result = restored.predict_image("Images/Sample_1.JPG", stages="final")
record = write_image_data(
    output / "corrected.png", result["CC"],
    dtype="uint8", allow_precision_loss=True,
)
```

Set `do_ffc=False` and omit the white image when no matching white-field
photograph is available. Enabled stages are required: `last_run` records stage
outcomes, errors, detector execution and persistence diagnostics. A failed fit
or save preserves the previous successful calibration.

The existing `reproducible-v1` encoded-sRGB fits remain available. Their linear
solver minimizes RGB squared error against unclipped reference targets; lower
RGB error can increase CIEDE2000, particularly for out-of-gamut references.
The fit report records this objective and affected target cells. See
[fit objectives and perceptual evaluation](docs/numerical-policies-and-validation.md#fit-objective-and-perceptual-evaluation).
Linear-light CCM also optimizes RGB error; evaluate its measured results rather
than assuming every correction reduces perceptual error.

## Working with the package

- [Contracts](docs/contracts.md): immutable images, canonical references and chart observations.
- [Color semantics](docs/color-semantics.md) and [pipeline integration](docs/contracts-and-color-integration.md): declared encoding, illuminants and orientation.
- [Numerical policies](docs/numerical-policies-and-validation.md): clipping evidence, monotonic neutral gamma, guarded FFC, reproducible fitting and grouped evaluation.
- [Chart detection](docs/chart-detection.md): model settings, supported layouts and explicit OpenCV MCC compatibility.
- [Persistence and compatibility](docs/local-reliability.md): transactional state and honest failure behavior.
- [Calibration bundles](docs/calibration-bundles.md): portable validated parameters; legacy pickle loading remains trusted-file-only.
- [Image I/O](docs/image-io-policies.md): precision consent, strict readback verification and atomic publication. Optional `png_profile="fast-lossless"` preserves PNG samples with larger files.
- [Batch processing and reporting](docs/batch-and-reporting.md): bounded work, cancellation, per-file outcomes and stage retention.
- [Prepared execution](docs/acceleration.md): direct CPU execution, explicitly budgeted combined LUTs, cache ownership and cube export.
- [Performance measurements](docs/performance.md) and [distribution qualification](docs/distribution-qualification.md).

Legacy `run`, `predict_image`, `predict_images`, array/image helpers, saved-model
formats and import entry points remain available. New contracts and policies
do not reinterpret older calibrations. Chart model geometry differs from MCC,
so fitting a new calibration can produce different measurements.

The runtime is organized around `pipeline.py` and `config.py`; `contracts.py`,
`colorimetry.py` and `observations.py` define the interpretation boundaries.
`detection/` supplies model geometry and provider policy. `core/`, `modern.py`,
`stage_policies.py` and `fitting.py` implement stage execution and fitting.
`ui_fitting.py` and `ui_nn.py` contain UI numerical fits; `ui_native.py` records
native image semantics and `ui_execution.py` supplies optional CPU execution.
`bundle.py` handles safe inference artifacts; `acceleration.py` prepares execution;
`batch.py`, `prediction.py`, `reporting.py` and `io/` handle application and output.
`key_functions.py` retains historical utility imports for compatibility.

## Example output

The illustrations show an example workflow. Chart residuals and appearance do
not establish physical color accuracy on independent photographs; results
depend on the reference, capture conditions, fit and validation protocol.

| Before | After |
| --- | --- |
| ![Before correction](ReadMe_Images/before.svg) | ![After correction](ReadMe_Images/After.svg) |

## Development and qualification

Install developer tools with `python -m pip install -e ".[dev,performance]"`.
Build and inspect distributions with:

```shell
python -m pip install build twine
python -m build
python -m twine check --strict dist/*
python scripts/verify_distribution.py --output evidence/artifacts.json
```

The release workflow installs the built wheel into isolated base and performance
environments and runs `scripts/verify_installed.py`, including a real-photo fit,
safe-bundle reload, precision I/O and bounded batch processing. See the
[qualification guide](docs/distribution-qualification.md) for neutral-directory
commands. Generated evidence belongs outside the source tree. Submit changes
through a pull request with the relevant numerical and compatibility evidence.

## License

Python source is MIT licensed; see [LICENSE](LICENSE). Bundled detector models
declare AGPL-3.0. Their original license, notices and provenance are retained
[beside the chart model](ColorCorrectionPipeline/detection/models/UPSTREAM-NOTICE.md).
Source licensing does not replace the model terms; the provenance record also
states the unresolved model ancestry and training-data rights information.

## Citation

Wakholi, C., Hardigan, M. A., Lee, J., Lukas, S. B., Feldman, M. J., Altendorf,
K. R., Neyhart, J. L., & Rippner, D. A. (2026). A systematic color correction
pipeline for controlled-environment imaging. *The Plant Phenome Journal*, 9(1),
e70067. [doi:10.1002/ppj2.70067](https://doi.org/10.1002/ppj2.70067).

```bibtex
@software{colorcorrectionpipeline,
  author = {Wakholi, Collins and Rippner, Devin A.},
  title = {ColorCorrectionPipeline: A stepwise color correction pipeline},
  url = {https://github.com/collinswakholi/ColorCorrectionPackage},
  version = {1.5.0},
  year = {2026}
}
```

Thanks to Devin A. Rippner for technical guidance, ORISE for fellowship support,
and USDA-ARS for research support. Report issues and feature requests on
[GitHub](https://github.com/collinswakholi/ColorCorrectionPackage/issues).
