Metadata-Version: 2.5
Name: ColorCorrectionPipeline
Version: 2.2.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: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
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

Calibrate **flat-field correction (FFC) → gamma correction (GC) → white balance
(WB) → color correction (CC)**, then apply the fitted calibration to images
captured under the same conditions.

**Version 2.x defaults to the UI-native RPCC2 workflow.** Legacy methodology is
retired as the default, along with `ours`/`conv` naming in the primary API.
For a linear-light 3×3 CCM, use `Config(model="3x3")` when fitting the chart.
Historical `conv` configurations retain their encoded-sRGB fit, and saved
calibrations retain their original predictions. See the [conventional CCM migration note](https://github.com/collinswakholi/ColorCorrectionPackage/blob/v2.2.0/docs/migration-2.md#migrating-a-conventional-ccm-trained-on-jpegs).

## Install

```shell
pip install --upgrade ColorCorrectionPipeline
```

To install the version used in the example below, run `pip install ColorCorrectionPipeline==2.2.0`.
The distribution and Python import are both named `ColorCorrectionPipeline`;
the repository is named ColorCorrectionPackage.

Python **3.8–3.14** is qualified across the CI matrix. Prefer maintained CPython
3.13 or 3.14 for a new environment; see [Python compatibility](https://github.com/collinswakholi/ColorCorrectionPackage/blob/v2.2.0/docs/python-compatibility.md).
Optional CPU acceleration is available with
`pip install --upgrade "ColorCorrectionPipeline[performance]"`. Chart detection separately
automatically tries supported GPU providers and falls back to CPU if none completes a verified model forward. No device setting is needed. Color fitting and exact bundle replay use the qualified CPU kernels; model loading does not require the GPU used for detection. See [device selection and diagnostics](docs/gpu-detection.md).

## Run the sample photographs

```shell
python -m ColorCorrectionPipeline.examples.quickstart --output demo
```

The quickstart fits the included chart and white field, explicitly saves
`demo/calibration.ccpb`, reloads it and writes `demo/corrected.png` and
`demo/results.json`. To reuse that calibration on your own image in a fresh process:

```shell
python -m ColorCorrectionPipeline.examples.apply_bundle --bundle demo/calibration.ccpb --image scene.jpg --output replay
```

## One complete test example

Save this as `test_calibration.py` outside the source checkout and run
`python test_calibration.py` so it imports the installed package.
It uses the installed sample photographs, fits all four steps, saves and reloads
a safe calibration bundle, checks exact replay, and writes a verified PNG.
Choose a new output directory for each run.

```python
from contextlib import ExitStack
from importlib import resources
from pathlib import Path

import numpy as np

from ColorCorrectionPipeline import (
    ColorCorrection, Config, Steps, FFCOptions, GCOptions, WBOptions, CCOptions,
)
from ColorCorrectionPipeline.io import write_image_data

output = Path("sample-results")
config = Config(
    steps=Steps(
        ffc=FFCOptions(enabled=True, fit={"method": "linear", "degree": 1}),
        gc=GCOptions(enabled=True, quality={"min_neutrals": 3}),
        wb=WBOptions(enabled=True, fit={"headroom": 0.95}),
        cc=CCOptions(
            enabled=True,
            fit={"model": "rpcc2"},
            validation={"enabled": False},
        ),
    ),
    input={"frame_index": 0},
    chart={"device": "auto", "allow_cpu_fallback": True},
    color={"input_encoding": "srgb", "output_encoding": "srgb"},
    numerics={"precision": "auto"},
    observations={"saturation": "report"},
    persistence={"enabled": True, "directory": str(output), "format": "bundle"},
)

output.mkdir(exist_ok=False)

with ExitStack() as stack:
    def sample(name):
        # Keep packaged files alive until calibration and prediction finish.
        package = "ColorCorrectionPipeline.examples.data"
        if hasattr(resources, "files"):
            source = resources.as_file(resources.files(package).joinpath(name))
        else:  # Python 3.8
            source = resources.path(package, name)
        return stack.enter_context(source)

    chart, white, scene = map(sample, ("Image_1.JPG", "white.JPG", "Sample_1.JPG"))
    pipeline = ColorCorrection()
    fit = pipeline.fit_or_raise(chart, white, name_="chart", config=config)

    bundle = output / "chart_models.ccpb"
    restored = ColorCorrection().load_bundle(bundle)
    corrected = restored.predict_image(scene, stages="final")["CC"]
    direct = pipeline.predict_image(scene, stages="final")["CC"]
    np.testing.assert_array_equal(corrected, direct)

    record = write_image_data(
        output / "corrected.png", corrected,
        dtype="uint8", input_encoding="srgb", allow_precision_loss=True,
        png_profile="fast-lossless",
    )
    assert record.maximum_codec_error == 0

    print("Training-chart mean Delta E00:", fit.metrics["chart_CC"]["delta_e00_after_mean"])
    print("Bundle replay: exact; PNG codec error:", record.maximum_codec_error)
    print("Outputs:", output.resolve())
```

With automatic persistence, set the format explicitly to `bundle` and
use the fit name to locate the file. Here it is `sample-results/chart_models.ccpb`.
The result's `artifacts` lists the paths written by that fit. With saving enabled,
omitting the format selects the historical pickle format. With saving disabled,
call `pipeline.save_bundle(path)` after a successful fit to choose the filename,
as the quickstart does.

`fit_or_raise()` raises `CalibrationError` for a failed stage and propagates
configuration and filesystem exceptions. After a failed refit, the pipeline still
holds the last successful calibration. Existing callers can continue using
`metrics, images, failed = pipeline.run(...)`.

The PNG is an explicitly quantized preview; zero codec error means its stored
samples survived readback unchanged. Use a suitable TIFF export when retaining
floating-point precision. Chart ΔE00 measures the calibration chart, not
independent scene accuracy.

For your own captures, replace the three sample paths with your chart, matching
white-field image and scene. Enabled FFC requires compatible image dimensions.
Without a matching white image, disable `steps.ffc.enabled` and pass `None`
as the white-image argument.

## Configuration breakdown

| Category | Purpose in the example |
| --- | --- |
| `steps.ffc` | Enables white-field correction and selects a linear illumination surface. |
| `steps.gc` | Fits the neutral-lightness curve; requires at least three usable neutral patches. |
| `steps.wb` | Fits neutral white balance with 0.95 headroom. |
| `steps.cc` | Fits RPCC2. `fit`, `apply`/LUT options and CC-only `validation` have separate subgroups; validation is disabled for this smoke test. |
| `input` | Selects frame zero explicitly for the bundled JPEGs. |
| `chart` | Selects chart detection, independently of CPU color fitting. `auto` allows GPU detection with CPU fallback. |
| `reference` | Omitted here to use the built-in UI-native Classic24 reference. Supply a `ReferenceProfile` for measured targets. |
| `color` | Declares input and output encoding; the example uses sRGB. |
| `numerics` | Resolves byte or high-precision execution from calibration inputs. Use `high` if a byte chart will later serve uint16/float scenes. |
| `observations` | Reports saturation evidence without synthesizing replacement measurements. |
| `persistence` | Saves models and metrics during calibration, using a safe bundle in the output directory. Image export is a separate writer call. |

CC supports `3x3`/`ccm`, `rpcc2`, `rpcc3`, `linear`, `pls` and `nn`.
Linear, PLS and NN fits support polynomial, root or hybrid features through
degree four. See the [grouped configuration reference](https://github.com/collinswakholi/ColorCorrectionPackage/blob/v2.2.0/docs/grouped-configuration.md)
for solver-specific controls, defaults, constraints and JSON.

Grouped options are immutable snapshots over the existing flat configuration.
Old constructor arguments and kwargs remain supported; contradictory aliases
fail explicitly. Unversioned flat dictionaries retain their historical API-1
interpretation. A loaded calibration owns its recorded numerical recipe.

### Reproducible random chart sampling

```python
Config(model="3x3", chart={"samples_per_patch": 20, "sampling_seed": 42})
```

For a UI-native fit, omit both settings for the default 9 × 9 grid (81 points per patch). Set `samples_per_patch` (1–4096) for an exact count inside the inner 70% of each detected patch. The unsigned 32-bit seed defaults to zero. The same positions are reused across stages; the fitter still receives one mean RGB per patch. Do not combine a grid size with a random count. These settings affect calibration, not application of a saved model.

## Further usage

- [Fully customizable example](https://github.com/collinswakholi/ColorCorrectionPackage/blob/v2.2.0/ColorCorrectionPipeline/examples/customizable.py):
  editable grouped JSON, custom references, multiple scenes, TIFF/PNG exports and replay checks.
- [Migration and model selection](https://github.com/collinswakholi/ColorCorrectionPackage/blob/v2.2.0/docs/migration-2.md): retired defaults, historical training and feature bases.
- [Images and exports](https://github.com/collinswakholi/ColorCorrectionPackage/blob/v2.2.0/docs/image-io-policies.md): encoding, range, precision consent and strict readback.
- [Calibration bundles](https://github.com/collinswakholi/ColorCorrectionPackage/blob/v2.2.0/docs/calibration-bundles.md): portable validated inference parameters.
- [Numerical policies](https://github.com/collinswakholi/ColorCorrectionPackage/blob/v2.2.0/docs/numerical-policies-and-validation.md): fit objectives, quality guards and validation scope.
- [Batch processing](https://github.com/collinswakholi/ColorCorrectionPackage/blob/v2.2.0/docs/batch-and-reporting.md) and [prepared execution](https://github.com/collinswakholi/ColorCorrectionPackage/blob/v2.2.0/docs/acceleration.md).
- [Architecture](https://github.com/collinswakholi/ColorCorrectionPackage/blob/v2.2.0/docs/architecture.md) and [distribution qualification](https://github.com/collinswakholi/ColorCorrectionPackage/blob/v2.2.0/docs/distribution-qualification.md): source ownership and installed-package checks.

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

## License and citation

Python source is [MIT licensed](https://github.com/collinswakholi/ColorCorrectionPackage/blob/v2.2.0/LICENSE). Bundled detector models declare
AGPL-3.0; their [notices and provenance](https://github.com/collinswakholi/ColorCorrectionPackage/blob/v2.2.0/ColorCorrectionPipeline/detection/models/UPSTREAM-NOTICE.md)
are retained, including the recorded model-ancestry and training-data rights limitations.

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).

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