darsia.PatchwiseIlluminationCorrection#

class PatchwiseIlluminationCorrection(baseline_images=None, labels=None, nw=100, eps=1e-06, show_images=True)[source]#

Bases: BaseCorrection

Class for performing patchwise illumination correction on images.

Initialize the PatchwiseIlluminationCorrection class.

Parameters:
baseline_imageslist[str] | list[darsia.Image]

List of baseline images for correction.

labelsdarsia.Image

Label image defining regions for per-label reference color computation within each patch. All unique labels in this image are used for calibration.

nwint

Number of patches in width direction for patchwise illumination correction. Default is 100.

epsfloat

Small constant to avoid division by zero in patchwise illumination correction. Default is 1e-6.

show_imagesbool

Flag to control whether to display the calibrated image.

Methods

compute_correction(coefficient_list, ...)

Calculate correction coefficients based on baseline images.

correct_array(img)

Apply patchwise illumination correction to the input image.

correct_metadata([metadata])

Correction routine on metadata level.

extract_color_values_patches(image, labels)

Extract RGB values from image patches, averaged per-label within each patch.

load(path)

Load correction coefficients from a file.

save(path)

Save correction coefficients to a file.

compute_correction(coefficient_list, coefficient_mean_list)[source]#

Calculate correction coefficients based on baseline images.

Parameters:
coefficient_listlist[np.ndarray]

List of coefficient matrices for each baseline image.

coefficient_mean_listlist[np.ndarray]

List of mean coefficient values for each baseline image.

Returns:
np.ndarray

Array of correction coefficients.

correct_array(img)[source]#

Apply patchwise illumination correction to the input image.

Parameters:
imgnp.ndarray

Input image to be corrected.

Returns:
np.ndarray

Corrected image after applying patchwise illumination correction.

extract_color_values_patches(image, labels)[source]#

Extract RGB values from image patches, averaged per-label within each patch.

For each patch, computes the mean color per unique label present in that patch, then averages those per-label means (unweighted). This ensures heterogeneous patches (straddling multiple materials) contribute equally per material, regardless of pixel-count distribution.

Parameters:
imagenp.ndarray

Input image (RGB, same shape as labels).

labelsdarsia.Image

Label image defining regions for per-label reference color computation within each patch. All unique labels in this image are used for calibration.

Returns:
Tuple containing R, G, B matrices (shape nh x nw).
load(path)[source]#

Load correction coefficients from a file.

Parameters:
pathPath

Path to the file from which coefficients will be loaded.

save(path)[source]#

Save correction coefficients to a file.

Parameters:
pathPath

Path to the file where coefficients will be saved.