Calibrates the color of an image using a set of known color references (e.g., from a color checker) by implementing multiple calibration models including industry-standard CCM (Color Correction Matrix 3x3), Affine (4x3), White Balance, Cubic (9-term polynomial), and Root-Polynomial (20-term).
Usage
image_correction(
img,
card_colors = NULL,
known_colors = NULL,
k_mat = NULL,
model = c("ccm", "affine", "white_balance", "cubic", "root_polynomial"),
reference = "colorchecker24",
lambda = 1e-04,
plot = FALSE,
verbose = TRUE
)Arguments
- img
An
imageobject to be corrected.- card_colors
A data frame of observed colors from the color checker (typically output directly from
get_card_colors()). Must containR, G, B(orR_obs, G_obs, B_obs) columns. IfNULL,k_matmust be provided.- known_colors
A data frame of known reference colors. Must contain
R, G, B(orref_R, ref_G, ref_B) columns. IfNULLandcard_colorscontains reference data orreference = "colorchecker24", the built-in reference chart is used automatically.- k_mat
A pre-calculated transformation matrix (\(\mathbf{K}\)). If provided,
card_colorsandknown_colorsare optional, and the correction is applied directly toimg. Useful for batch calibration of image series.- model
The calibration model to use:
"ccm"(default): 3x3 linear Color Correction Matrix (robust, standard in computer vision)."affine": 4x3 matrix with intercept."white_balance": Channel gains derived from neutral white patch."cubic": 9-term 3rd-order polynomial model."root_polynomial": 20-term root-polynomial model.
- reference
Reference chart standard to use when
known_colors = NULL(default"colorchecker24").- lambda
Ridge regularization factor for matrix inversion (default
1e-4).- plot
Logical. If
TRUE(defaultFALSE), displays a side-by-side comparison of the original and calibrated image.- verbose
Logical. If
TRUE(default), prints calibration diagnostics.
Value
If card_colors or known_colors are provided, returns a list containing:
img: Animageobject with the color correction applied.k: The calculated transformation matrix (\(\mathbf{K}\)).model: The model used.avg_delta_raw: Mean Euclidean color error (\(\Delta E\)) before calibration.avg_delta_calib: Mean \(\Delta E\) after calibration.wb_gains: White balance channel gains.df: Comparison data frame with observed, calibrated, and reference colors.
If only img and a pre-calculated k_mat are supplied, returns the corrected image directly.
Details
Color calibration finds an optimal transformation matrix \(\mathbf{K}\) mapping observed colors \(\mathbf{S}\) to reference target colors \(\mathbf{T}\):
"ccm"(default): Linear 3x3 Color Correction Matrix computed via Ridge-regularized least squares: $$\mathbf{K} = (\mathbf{S}^T \mathbf{S} + \lambda \mathbf{I})^{-1} \mathbf{S}^T \mathbf{T}$$"affine": 4x3 linear transformation with translation offset (intercept) \([1, R, G, B]\)."white_balance": Channel-wise gain correction derived from neutral/white patches."cubic": 9-term polynomial mapping (\(R, G, B, R^2, G^2, B^2, R^3, G^3, B^3\))."root_polynomial": 20-term root-polynomial mapping with cross-channel interactions.
Examples
if (FALSE) { # interactive()
library(pliman)
img <- image_pliman("colorcheck.jpg")
# 1. Extract card colors
card <- get_card_colors(img)
# 2. Calibrate image using default CCM 3x3 model
calib <- image_correction(img, card, model = "ccm", plot = TRUE)
# 3. Or using polynomial models
calib_poly <- image_correction(img, card, model = "root_polynomial")
}
