"""
Colour Checker Detection - Templated
=====================================
Defines the objects to perform templated (warped perspective) colour checker detection:
- :func:`colour_checker_detection.detect_colour_checkers_templated`
- :func:`colour_checker_detection.segmenter_templated`
- :func:`colour_checker_detection.extractor_templated`
"""
from __future__ import annotations
import typing
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Literal
import cv2
import numpy as np
from colour import read_image
from colour.hints import (
Any,
ArrayLike,
NDArrayFloat,
NDArrayInt,
NDArrayReal,
cast,
)
if TYPE_CHECKING:
from collections.abc import Callable
from colour.io import convert_bit_depth
from colour.models import eotf_inverse_sRGB, eotf_sRGB
from colour.utilities import (
Structure,
optional,
required,
usage_warning,
)
from colour.utilities.documentation import (
DocstringDict,
is_documentation_building,
)
from scipy.optimize import linear_sum_assignment
from scipy.spatial import distance_matrix
from colour_checker_detection.detection.common import (
DTYPE_FLOAT_DEFAULT,
DTYPE_INT_DEFAULT,
DataDetectionColourChecker,
as_float32_array,
as_int32_array,
cluster_swatches,
contour_centroid,
detect_contours,
filter_clusters,
reformat_image,
remove_stacked_contours,
)
from colour_checker_detection.detection.plotting import plot_detection_results
from colour_checker_detection.detection.segmentation import (
SETTINGS_SEGMENTATION_COLORCHECKER_CLASSIC,
DataSegmentationColourCheckers,
)
from colour_checker_detection.detection.templates import (
PATH_TEMPLATE_COLORCHECKER_CLASSIC,
load_template,
)
__author__ = "Colour Developers"
__copyright__ = "Copyright 2018 Colour Developers"
__license__ = "BSD-3-Clause - https://opensource.org/licenses/BSD-3-Clause"
__maintainer__ = "Colour Developers"
__email__ = "colour-developers@colour-science.org"
__status__ = "Production"
__all__ = [
"SETTINGS_TEMPLATED_COLORCHECKER_CLASSIC",
"WarpingData",
"segmenter_templated",
"extractor_templated",
"detect_colour_checkers_templated",
]
SETTINGS_TEMPLATED_COLORCHECKER_CLASSIC: dict = (
SETTINGS_SEGMENTATION_COLORCHECKER_CLASSIC.copy()
)
if is_documentation_building(): # pragma: no cover
SETTINGS_TEMPLATED_COLORCHECKER_CLASSIC = DocstringDict(
SETTINGS_TEMPLATED_COLORCHECKER_CLASSIC
)
SETTINGS_TEMPLATED_COLORCHECKER_CLASSIC.__doc__ = """
Settings for the templated detection of the *X-Rite* *ColorChecker Classic*.
"""
SETTINGS_TEMPLATED_COLORCHECKER_CLASSIC.update(
{
"contour_approximation_factor": 0.1,
"dbscan_eps": 0.5,
"dbscan_min_samples": 5,
"transformation_cost_threshold": 10.0,
"swatches_chromatic_slice": slice(0, 18),
"swatches_achromatic_slice": slice(18, 24),
}
)
TEMPLATE_COLORCHECKER_CLASSIC = load_template(PATH_TEMPLATE_COLORCHECKER_CLASSIC)
"""Default ColorChecker Classic template for templated detection."""
[docs]
@dataclass
class WarpingData:
"""
Data class for storing the results of the correspondence finding.
Parameters
----------
cluster_id
The index of the cluster that was used for the correspondence.
cost
The cost of the transformation, which means the average distance of the
warped point from the reference template point.
transformation
The transformation matrix to warp the cluster to the template.
"""
cluster_id: int = -1
cost: float = np.inf
transformation: np.ndarray | None = field(default=None)
@typing.overload
def segmenter_templated(
image: ArrayLike,
cctf_encoding: Callable = ...,
apply_cctf_encoding: bool = ...,
additional_data: Literal[True] = True,
**kwargs: Any,
) -> DataSegmentationColourCheckers: ...
@typing.overload
def segmenter_templated(
image: ArrayLike,
cctf_encoding: Callable = ...,
apply_cctf_encoding: bool = ...,
*,
additional_data: Literal[False],
**kwargs: Any,
) -> NDArrayInt: ...
@typing.overload
def segmenter_templated(
image: ArrayLike,
cctf_encoding: Callable,
apply_cctf_encoding: bool,
additional_data: Literal[False],
**kwargs: Any,
) -> NDArrayInt: ...
[docs]
@required("scikit-learn") # pyright: ignore
def segmenter_templated(
image: ArrayLike,
cctf_encoding: Callable = eotf_inverse_sRGB,
apply_cctf_encoding: bool = True,
additional_data: bool = False,
**kwargs: Any,
) -> DataSegmentationColourCheckers | NDArrayInt:
"""
Detect the colour checker rectangles, clusters and swatches in specified image
using segmentation with advanced filtering.
The process is as follows:
1. Input image is converted to a grayscale image and normalised to range [0, 1].
2. Image is denoised using multiple bilateral filtering passes.
3. Image is thresholded.
4. Image is eroded and dilated to cleanup remaining noise.
5. Contours are detected.
6. Contours are filtered to only keep squares/swatches above and below defined
surface area, moreover they have to resemble a convex quadrilateral.
Additionally, squareness, area, aspect ratio and orientation are used as
features to remove any remaining outlier contours.
7. Stacked contours are removed.
8. Swatches are clustered to isolate region-of-interest that are potentially
colour checkers: Contours are scaled by a third so that colour checkers
swatches are joined, creating a large rectangular cluster. Rectangles
are fitted to the clusters.
9. Clusters with a number of swatches close to the expected one are kept.
Parameters
----------
image
Image to detect the colour checker rectangles from.
cctf_encoding
Encoding colour component transfer function / opto-electronic
transfer function used when converting the image from float to 8-bit.
apply_cctf_encoding
Apply the encoding colour component transfer function / opto-electronic
transfer function.
additional_data
Whether to output additional data.
Other Parameters
----------------
adaptive_threshold_kwargs
Keyword arguments for :func:`cv2.adaptiveThreshold` definition.
aspect_ratio
Colour checker aspect ratio, e.g. 1.5.
aspect_ratio_minimum
Minimum colour checker aspect ratio for detection: projective geometry
might reduce the colour checker aspect ratio.
aspect_ratio_maximum
Maximum colour checker aspect ratio for detection: projective geometry
might increase the colour checker aspect ratio.
bilateral_filter_iterations
Number of iterations to use for bilateral filtering.
bilateral_filter_kwargs
Keyword arguments for :func:`cv2.bilateralFilter` definition.
contour_approximation_factor
Approximation factor for the Douglas-Peucker polygon approximation algorithm.
It controls how aggressively contours are simplified, expressed as a fraction
of the contour's perimeter. Lower values (e.g., 0.01) preserve more detail,
higher values (e.g., 0.1) simplify more aggressively.
convolution_iterations
Number of iterations to use for the erosion / dilation process.
convolution_kernel
Convolution kernel to use for the erosion / dilation process.
dbscan_eps
DBSCAN epsilon parameter defining the maximum distance between two samples
for them to be considered in the same neighborhood. Lower values create
tighter clusters. Default is 0.5.
dbscan_min_samples
DBSCAN minimum samples parameter defining the number of samples in a
neighborhood for a point to be considered a core point. Default is 5.
transformation_cost_threshold
Cost threshold for early termination of transformation search. If a
transformation achieves an average distance below this threshold, the search
stops immediately. Lower values require better matches. Default is 10.0.
interpolation_method
Interpolation method used when resizing the images, `cv2.INTER_CUBIC`
and `cv2.INTER_LINEAR` methods are recommended.
reference_values
Reference values for the colour checker of interest.
swatch_contour_scale
As the image is filtered, the swatches area will tend to shrink, the
generated contours can thus be scaled.
swatch_minimum_area_factor
Swatch minimum area factor :math:`f` with the minimum area :math:`m_a`
expressed as follows: :math:`m_a = image_w * image_h / s_c / f` where
:math:`image_w`, :math:`image_h` and :math:`s_c` are respectively the
image width, height and the swatches count.
swatches
Colour checker swatches total count.
swatches_achromatic_slice
A `slice` instance defining achromatic swatches used to detect if the
colour checker is upside down.
swatches_chromatic_slice
A `slice` instance defining chromatic swatches used to detect if the
colour checker is upside down.
swatches_count_maximum
Maximum swatches count to be considered for the detection.
swatches_count_minimum
Minimum swatches count to be considered for the detection.
swatches_horizontal
Colour checker swatches horizontal columns count.
swatches_vertical
Colour checker swatches vertical row count.
transform
Transform to apply to the colour checker image post-detection.
working_width
Width the input image is resized to for detection.
working_height
Height the input image is resized to for detection.
Returns
-------
:class:`colour_checker_detection.DataSegmentationColourCheckers`
or :class:`np.ndarray`
Colour checker rectangles and additional data or colour checker rectangles only.
Examples
--------
>>> import os
>>> from colour import read_image
>>> from colour_checker_detection import ROOT_RESOURCES_TESTS, segmenter_templated
>>> path = os.path.join(
... ROOT_RESOURCES_TESTS,
... "colour_checker_detection",
... "detection",
... "IMG_1967.png",
... )
>>> image = read_image(path)
>>> segmenter_templated(image) # doctest: +ELLIPSIS
array([[[ 357, 690],
[ 373, 219],
[1086, 244],
[1069, 715]]], dtype=int32)
"""
from sklearn.cluster import DBSCAN # noqa: PLC0415
settings = Structure(**SETTINGS_TEMPLATED_COLORCHECKER_CLASSIC)
settings.update(**kwargs)
if apply_cctf_encoding:
image = cctf_encoding(image)
image = reformat_image(image, settings.working_width, settings.interpolation_method)
image = cast("NDArrayFloat", image)
contours, image_k = detect_contours(image, True, **settings) # pyright: ignore
# Filter contours using multiple features: area, convexity, squareness,
# aspect ratio, and orientation
width, height = image.shape[1], image.shape[0]
minimum_area = (
width * height / settings.swatches / settings.swatch_minimum_area_factor
)
maximum_area = width * height / settings.swatches
square = np.array([[0, 0], [1, 0], [1, 1], [0, 1]])
squares = []
features = []
for contour in contours:
curve = cv2.approxPolyDP(
as_int32_array(contour),
settings.contour_approximation_factor
* cv2.arcLength(as_int32_array(contour), True),
True,
)
area = cv2.contourArea(curve)
if minimum_area < area < maximum_area and len(curve) == 4:
swatch = curve.reshape(-1, 2)
squares.append(swatch)
squareness = cv2.matchShapes(swatch, square, cv2.CONTOURS_MATCH_I2, 0.0)
bbox = cv2.boundingRect(swatch)
aspect_ratio = float(bbox[2]) / bbox[3]
features.append([squareness, area, aspect_ratio])
if squares:
features_array = np.array(features)
features_std = np.std(features_array, axis=0)
features_std[features_std == 0] = 1.0 # Avoid division by zero
features_normalized = (
features_array - np.mean(features_array, axis=0)
) / features_std
clustering = DBSCAN(
eps=settings.dbscan_eps,
min_samples=settings.dbscan_min_samples,
).fit(features_normalized)
mask = clustering.labels_ != -1
# CRITICAL: If DBSCAN removes everything, keep original swatches
if np.sum(mask) == 0:
mask = np.ones(len(squares), dtype=bool)
squares = np.array(squares)[mask]
squares = (
as_int32_array(squares)
if len(squares) > 0
else np.empty((0, 4, 2), dtype=DTYPE_INT_DEFAULT)
)
swatches = as_int32_array(remove_stacked_contours(squares))
clusters = cluster_swatches(image, swatches, settings.swatch_contour_scale)
rectangles = filter_clusters(
clusters,
swatches,
settings.swatches_count_minimum,
settings.swatches_count_maximum,
)
if additional_data:
return DataSegmentationColourCheckers(
rectangles,
clusters,
swatches,
image_k, # pyright: ignore
)
return rectangles
@typing.overload
def extractor_templated(
image: ArrayLike,
segmentation_data: DataSegmentationColourCheckers,
samples: int = ...,
cctf_decoding: Callable = ...,
apply_cctf_decoding: bool = ...,
additional_data: Literal[True] = True,
**kwargs: Any,
) -> tuple[DataDetectionColourChecker, ...]: ...
@typing.overload
def extractor_templated(
image: ArrayLike,
segmentation_data: DataSegmentationColourCheckers,
samples: int = ...,
cctf_decoding: Callable = ...,
apply_cctf_decoding: bool = ...,
*,
additional_data: Literal[False],
**kwargs: Any,
) -> tuple[NDArrayFloat, ...]: ...
@typing.overload
def extractor_templated(
image: ArrayLike,
segmentation_data: DataSegmentationColourCheckers,
samples: int,
cctf_decoding: Callable,
apply_cctf_decoding: bool,
additional_data: Literal[False],
**kwargs: Any,
) -> tuple[NDArrayFloat, ...]: ...
@typing.overload
def detect_colour_checkers_templated(
image: str | ArrayLike,
samples: int = ...,
cctf_decoding: Callable = ...,
apply_cctf_decoding: bool = ...,
segmenter: Callable = ...,
segmenter_kwargs: dict | None = ...,
extractor: Callable = ...,
extractor_kwargs: dict | None = ...,
show: bool = ...,
additional_data: Literal[True] = True,
**kwargs: Any,
) -> tuple[DataDetectionColourChecker, ...]: ...
@typing.overload
def detect_colour_checkers_templated(
image: str | ArrayLike,
samples: int = ...,
cctf_decoding: Callable = ...,
apply_cctf_decoding: bool = ...,
segmenter: Callable = ...,
segmenter_kwargs: dict | None = ...,
extractor: Callable = ...,
extractor_kwargs: dict | None = ...,
show: bool = ...,
*,
additional_data: Literal[False],
**kwargs: Any,
) -> tuple[NDArrayFloat, ...]: ...
@typing.overload
def detect_colour_checkers_templated(
image: str | ArrayLike,
samples: int,
cctf_decoding: Callable,
apply_cctf_decoding: bool,
segmenter: Callable,
segmenter_kwargs: dict | None,
extractor: Callable,
extractor_kwargs: dict | None,
show: bool,
additional_data: Literal[False],
**kwargs: Any,
) -> tuple[NDArrayFloat, ...]: ...
[docs]
def detect_colour_checkers_templated(
image: str | ArrayLike,
samples: int = 32,
cctf_decoding: Callable = eotf_sRGB,
apply_cctf_decoding: bool = False,
segmenter: Callable = segmenter_templated,
segmenter_kwargs: dict | None = None,
extractor: Callable = extractor_templated,
extractor_kwargs: dict | None = None,
show: bool = False,
additional_data: bool = False,
**kwargs: Any,
) -> tuple[DataDetectionColourChecker, ...] | tuple[NDArrayFloat, ...]:
"""
Detect the colour checkers swatches in specified image using templated methods.
Parameters
----------
image
Image (or image path to read the image from) to detect the colour
checkers swatches from.
samples
Sample count to use to average (mean) the swatches colours. The effective
sample count is :math:`samples^2`.
cctf_decoding
Decoding colour component transfer function / opto-electronic
transfer function used when converting the image from 8-bit to float.
apply_cctf_decoding
Apply the decoding colour component transfer function / opto-electronic
transfer function.
segmenter
Callable responsible to segment the image and extract the colour
checker rectangles.
segmenter_kwargs
Keyword arguments to pass to the ``segmenter``. Can include 'template'
as str (NPZ file path to template) or Template object.
If 'template' not provided, defaults to built-in ColorChecker Classic template.
extractor
Callable responsible to extract the colour checker data from the
segmented rectangles.
extractor_kwargs
Keyword arguments to pass to the ``extractor``.
show
Whether to show various debug images.
additional_data
Whether to output additional data.
Other Parameters
----------------
greedy_heuristic : float, optional
Heuristic threshold for early stopping in transformation search.
Default is 2.0.
validation_threshold : float, optional
Threshold for colour validation.
Default is 0.5.
Returns
-------
:class:`tuple`
Tuple of :class:`DataDetectionColourChecker` class
instances or colour checkers swatches.
Examples
--------
>>> import os
>>> from colour import read_image
>>> from colour_checker_detection import ROOT_RESOURCES_TESTS
>>> path = os.path.join(
... ROOT_RESOURCES_TESTS,
... "colour_checker_detection",
... "detection",
... "IMG_1967.png",
... )
>>> image = read_image(path)
>>> detect_colour_checkers_templated(image, apply_cctf_decoding=True)
... # doctest: +SKIP
(array([[ 1.07537337e-01, 4.11238223e-02, 1.31721459e-02],
[ 3.52024108e-01, 1.29535466e-01, 4.84532639e-02],
[ 9.00324881e-02, 8.14048126e-02, 6.83287457e-02],
[ 7.53633380e-02, 6.11113459e-02, 1.10184597e-02],
[ 1.46142766e-01, 8.32280964e-02, 7.74866268e-02],
[ 1.01110630e-01, 1.63705498e-01, 7.03689680e-02],
[ 4.23571885e-01, 1.02802373e-01, 6.50439737e-03],
[ 6.09256141e-02, 5.20781092e-02, 8.99062678e-02],
[ 3.42755497e-01, 5.93896434e-02, 2.91827880e-02],
[ 7.73139372e-02, 2.73966864e-02, 3.07213869e-02],
[ 2.03338221e-01, 1.79222777e-01, 2.65911571e-03],
[ 3.85695517e-01, 1.34022757e-01, 1.26003276e-03],
[ 3.15370820e-02, 2.88631991e-02, 6.08412772e-02],
[ 6.47268444e-02, 1.22600473e-01, 1.40322614e-02],
[ 2.66343951e-01, 3.76947522e-02, 1.44897113e-02],
[ 4.79563773e-01, 2.28976935e-01, 4.33672598e-04],
[ 2.93841749e-01, 5.43766469e-02, 5.89662455e-02],
[ 2.67328490e-02, 8.21092799e-02, 7.17147887e-02],
[ 5.15012801e-01, 3.33238840e-01, 1.63575962e-01],
[ 3.56554657e-01, 2.31821850e-01, 1.14737533e-01],
[ 2.27919579e-01, 1.48821548e-01, 7.34134316e-02],
[ 1.14748545e-01, 7.51190484e-02, 3.64632085e-02],
[ 5.69365770e-02, 3.84297743e-02, 1.82996020e-02],
[ 2.28971709e-02, 1.62528455e-02, 7.39292800e-03]]...),)
"""
if segmenter_kwargs is None:
segmenter_kwargs = {}
settings = Structure(**SETTINGS_TEMPLATED_COLORCHECKER_CLASSIC)
settings.update(**kwargs)
swatches_h = settings.swatches_horizontal
swatches_v = settings.swatches_vertical
segmenter_kwargs = segmenter_kwargs.copy()
template = load_template(
optional(
segmenter_kwargs.pop("template", None), PATH_TEMPLATE_COLORCHECKER_CLASSIC
)
)
if isinstance(image, str):
image = read_image(image)
else:
image = convert_bit_depth(
image,
DTYPE_FLOAT_DEFAULT.__name__, # pyright: ignore
)
if apply_cctf_decoding:
image = cctf_decoding(image)
image = cast("NDArrayReal", image)
image = reformat_image(image, settings.working_width, settings.interpolation_method)
segmentation_colour_checkers_data = segmenter(
image,
additional_data=True,
**{**segmenter_kwargs, **settings, "template": template},
)
extractor_kwargs = cast("dict[str, Any]", optional(extractor_kwargs, {}))
colour_checkers_data = list(
extractor(
image,
segmentation_colour_checkers_data,
samples=samples,
cctf_decoding=cctf_decoding,
apply_cctf_decoding=False,
additional_data=True,
template=template,
residual_threshold=0.3,
**{**extractor_kwargs, **kwargs},
)
)
if show:
plot_detection_results(
tuple(colour_checkers_data),
swatches_h,
swatches_v,
segmentation_colour_checkers_data,
image,
)
if additional_data:
return tuple(colour_checkers_data)
return tuple(
colour_checker_data.swatch_colours
for colour_checker_data in colour_checkers_data
)