
Extract ColorChecker Palettes using Bilinear Homography and Negative Buffering
Source:R/utils_imagem.R
get_card_colors.RdAutomatically detects a ColorChecker chart in an image (or uses user-specified corners), maps each color patch using perspective-aware bilinear homography, and extracts pure nucleus color statistics using a dynamic negative buffer.
Usage
get_card_colors(
img,
reference = "colorchecker24",
nrow = NULL,
ncol = NULL,
buffer = 0.45,
margin_x = 0.05,
margin_y = 0.07,
corners = NULL,
auto_orient = TRUE,
index = "GRAY",
erode = NULL,
fill_hull = TRUE,
stat = c("mean", "median"),
plot = TRUE,
verbose = TRUE,
...
)Arguments
- img
An
imageobject (S3 pliman image or array).- reference
Reference chart standard for comparison. Default
"colorchecker24". Can be"colorchecker24", a customdata.framewith referenceR, G, Bcolumns, orNULLto extract observed colors without reference metrics.- nrow
The number of rows of color patches. If
NULL(default), automatically determined based on card orientation (4 for landscape, 6 for portrait for 24-patch charts).- ncol
The number of columns of color patches. If
NULL(default), automatically determined based on card orientation (6 for landscape, 4 for portrait).- buffer
Negative buffer shrinkage fraction (0 to 1, default
0.45for 45% shrink). If provided as a percentage > 1 (e.g. 45), it is automatically scaled to fraction.- margin_x
Outer horizontal border margin fraction (default
0.05/ 5%).- margin_y
Outer vertical border margin fraction (default
0.07/ 7%).- corners
Optional quadrilateral corners. Can be
NULL(automatic detection), a 4x2 matrix, a list with$TL, $TR, $BR, $BL, or an 8-element numeric vector.- auto_orient
Logical. If
TRUE(default for 24-patch ColorChecker charts), automatically detects the physical chart orientation (0, 90, 180, 270 degrees) by identifying the White (patch 19) and Black (patch 24) corners, ensuring that patch 1 (Dark skin) to patch 24 (Black) are always indexed correctly regardless of camera tilt or card rotation.- index
Segmentation index used for auto-detecting the card (default
"GRAY").- erode
Erosion kernel size for segmentation mask. If
NULL(default), automatically computed based on card coverage.- fill_hull
Logical. Fill holes in the segmentation mask (default
TRUE).- stat
Primary statistic assigned to
R, G, Bcolumns:"mean"(default) or"median".- plot
Logical. If
TRUE(default), displays the image with overlay showing the detected card boundary, cell grid, negative buffer sampling polygons, and patch badges.- verbose
Logical. If
TRUE(default), prints diagnostic summary.- ...
Legacy arguments (
xpix,ypix,filter_order) supported for backward compatibility.
Value
An object of class c("pliman_card_colors", "data.frame") containing:
id: Patch identifier (1 tonrow * ncol).row,col: Grid row and column positions.center_x,center_y: Pixel center coordinates.R,G,B: Sampled primary RGB values (0-255 scale).R_med,G_med,B_med: Median RGB values.R_sd,G_sd,B_sd: Standard deviations per channel.n_pixels: Number of pixels sampled in the nucleus.If reference comparison is active:
code,name,ref_R,ref_G,ref_B,Delta_R,Delta_G,Delta_B,Delta_E, andDelta_E_Calib.
The object also stores attributes:
attr(res, "patches"): List of patch bounding boxes and polygon vertices.attr(res, "corners"): Sorted corner coordinates (TL, TR, BR, BL).attr(res, "wb_gains"): Calculated White Balance gains.attr(res, "ccm"): Calculated 3x3 Color Correction Matrix.attr(res, "avg_delta_raw"): Mean Euclidean color error (\(\Delta E\)) before calibration.attr(res, "avg_delta_calib"): Mean \(\Delta E\) after CCM 3x3 calibration.
Details
The extraction employs a bilinear interpolation mapping:
$$P(u, v) = (1 - u)(1 - v)\mathbf{TL} + u(1 - v)\mathbf{TR} + uv\mathbf{BR} + (1 - u)v\mathbf{BL}$$
Outer card margins (margin_x, margin_y) exclude the surrounding plastic/cardboard
frame. A dynamic negative buffer (buffer, default 45%) shrinks each nominal cell
inwards towards its center, ensuring pixel sampling is strictly restricted to the
homogeneous nucleus of the color chip, immune to edge glare, shadows, and plastic borders.
Examples
if (FALSE) { # interactive()
library(pliman)
img <- image_pliman("colorcheck.jpg")
# Auto-detect card, auto-orient, and extract 24 patches with 45% negative buffer
card <- get_card_colors(img)
head(card)
# View patch overlay
plot(card)
}