OpenCV  4.5.3 Open Source Computer Vision
Color Correction Model

## Classes

class  cv::ccm::ColorCorrectionModel
Core class of ccm model. More...

## Enumerations

enum  cv::ccm::CCM_TYPE {
cv::ccm::CCM_3x3,
cv::ccm::CCM_4x3
}
Enum of the possible types of ccm. More...

enum  cv::ccm::COLOR_SPACE {
cv::ccm::COLOR_SPACE_sRGB,
cv::ccm::COLOR_SPACE_sRGBL,
cv::ccm::COLOR_SPACE_WideGamutRGB,
cv::ccm::COLOR_SPACE_WideGamutRGBL,
cv::ccm::COLOR_SPACE_ProPhotoRGB,
cv::ccm::COLOR_SPACE_ProPhotoRGBL,
cv::ccm::COLOR_SPACE_DCI_P3_RGB,
cv::ccm::COLOR_SPACE_DCI_P3_RGBL,
cv::ccm::COLOR_SPACE_AppleRGB,
cv::ccm::COLOR_SPACE_AppleRGBL,
cv::ccm::COLOR_SPACE_REC_709_RGB,
cv::ccm::COLOR_SPACE_REC_709_RGBL,
cv::ccm::COLOR_SPACE_REC_2020_RGB,
cv::ccm::COLOR_SPACE_REC_2020_RGBL,
cv::ccm::COLOR_SPACE_XYZ_D65_2,
cv::ccm::COLOR_SPACE_XYZ_D65_10,
cv::ccm::COLOR_SPACE_XYZ_D50_2,
cv::ccm::COLOR_SPACE_XYZ_D50_10,
cv::ccm::COLOR_SPACE_XYZ_A_2,
cv::ccm::COLOR_SPACE_XYZ_A_10,
cv::ccm::COLOR_SPACE_XYZ_D55_2,
cv::ccm::COLOR_SPACE_XYZ_D55_10,
cv::ccm::COLOR_SPACE_XYZ_D75_2,
cv::ccm::COLOR_SPACE_XYZ_D75_10,
cv::ccm::COLOR_SPACE_XYZ_E_2,
cv::ccm::COLOR_SPACE_XYZ_E_10,
cv::ccm::COLOR_SPACE_Lab_D65_2,
cv::ccm::COLOR_SPACE_Lab_D65_10,
cv::ccm::COLOR_SPACE_Lab_D50_2,
cv::ccm::COLOR_SPACE_Lab_D50_10,
cv::ccm::COLOR_SPACE_Lab_A_2,
cv::ccm::COLOR_SPACE_Lab_A_10,
cv::ccm::COLOR_SPACE_Lab_D55_2,
cv::ccm::COLOR_SPACE_Lab_D55_10,
cv::ccm::COLOR_SPACE_Lab_D75_2,
cv::ccm::COLOR_SPACE_Lab_D75_10,
cv::ccm::COLOR_SPACE_Lab_E_2,
cv::ccm::COLOR_SPACE_Lab_E_10
}

enum  cv::ccm::CONST_COLOR {
cv::ccm::COLORCHECKER_Macbeth,
cv::ccm::COLORCHECKER_Vinyl,
cv::ccm::COLORCHECKER_DigitalSG
}
Macbeth and Vinyl ColorChecker with 2deg D50. More...

enum  cv::ccm::DISTANCE_TYPE {
cv::ccm::DISTANCE_CIE76,
cv::ccm::DISTANCE_CIE94_GRAPHIC_ARTS,
cv::ccm::DISTANCE_CIE94_TEXTILES,
cv::ccm::DISTANCE_CIE2000,
cv::ccm::DISTANCE_CMC_1TO1,
cv::ccm::DISTANCE_CMC_2TO1,
cv::ccm::DISTANCE_RGB,
cv::ccm::DISTANCE_RGBL
}
Enum of possible functions to calculate the distance between colors. More...

enum  cv::ccm::INITIAL_METHOD_TYPE {
cv::ccm::INITIAL_METHOD_WHITE_BALANCE,
cv::ccm::INITIAL_METHOD_LEAST_SQUARE
}
Enum of the possible types of initial method. More...

enum  cv::ccm::LINEAR_TYPE {
cv::ccm::LINEARIZATION_IDENTITY,
cv::ccm::LINEARIZATION_GAMMA,
cv::ccm::LINEARIZATION_COLORPOLYFIT,
cv::ccm::LINEARIZATION_COLORLOGPOLYFIT,
cv::ccm::LINEARIZATION_GRAYPOLYFIT,
cv::ccm::LINEARIZATION_GRAYLOGPOLYFIT
}
Linearization transformation type. More...

## Introduction

The purpose of color correction is to adjust the color response of input and output devices to a known state. The device being calibrated is sometimes called the calibration source; the color space used as the standard is sometimes called the calibration target. Color calibration has been used in many industries, such as television production, games, photography, engineering, chemistry, medicine, etc. Due to the manufacturing process of the input and output equipment, the channel response has nonlinear distortion. In order to correct the picture output of the equipment, it is nessary to calibrate the captured color and the actual color.

## ◆ CCM_TYPE

 enum cv::ccm::CCM_TYPE

#include <opencv2/mcc/ccm.hpp>

Enum of the possible types of ccm.

Enumerator
CCM_3x3
Python: cv.ccm.CCM_3x3

The CCM with the shape $$3\times3$$ performs linear transformation on color values.

CCM_4x3
Python: cv.ccm.CCM_4x3

The CCM with the shape $$4\times3$$ performs affine transformation.

## ◆ COLOR_SPACE

 enum cv::ccm::COLOR_SPACE

#include <opencv2/mcc/ccm.hpp>

Enumerator
COLOR_SPACE_sRGB
Python: cv.ccm.COLOR_SPACE_sRGB

https://en.wikipedia.org/wiki/SRGB , RGB color space

COLOR_SPACE_sRGBL
Python: cv.ccm.COLOR_SPACE_sRGBL

https://en.wikipedia.org/wiki/SRGB , linear RGB color space

https://en.wikipedia.org/wiki/Adobe_RGB_color_space , linear RGB color space

COLOR_SPACE_WideGamutRGB
Python: cv.ccm.COLOR_SPACE_WideGamutRGB

https://en.wikipedia.org/wiki/Wide-gamut_RGB_color_space , RGB color space

COLOR_SPACE_WideGamutRGBL
Python: cv.ccm.COLOR_SPACE_WideGamutRGBL

https://en.wikipedia.org/wiki/Wide-gamut_RGB_color_space , linear RGB color space

COLOR_SPACE_ProPhotoRGB
Python: cv.ccm.COLOR_SPACE_ProPhotoRGB

https://en.wikipedia.org/wiki/ProPhoto_RGB_color_space , RGB color space

COLOR_SPACE_ProPhotoRGBL
Python: cv.ccm.COLOR_SPACE_ProPhotoRGBL

https://en.wikipedia.org/wiki/ProPhoto_RGB_color_space , linear RGB color space

COLOR_SPACE_DCI_P3_RGB
Python: cv.ccm.COLOR_SPACE_DCI_P3_RGB

https://en.wikipedia.org/wiki/DCI-P3 , RGB color space

COLOR_SPACE_DCI_P3_RGBL
Python: cv.ccm.COLOR_SPACE_DCI_P3_RGBL

https://en.wikipedia.org/wiki/DCI-P3 , linear RGB color space

COLOR_SPACE_AppleRGB
Python: cv.ccm.COLOR_SPACE_AppleRGB

https://en.wikipedia.org/wiki/RGB_color_space , RGB color space

COLOR_SPACE_AppleRGBL
Python: cv.ccm.COLOR_SPACE_AppleRGBL

https://en.wikipedia.org/wiki/RGB_color_space , linear RGB color space

COLOR_SPACE_REC_709_RGB
Python: cv.ccm.COLOR_SPACE_REC_709_RGB

https://en.wikipedia.org/wiki/Rec._709 , RGB color space

COLOR_SPACE_REC_709_RGBL
Python: cv.ccm.COLOR_SPACE_REC_709_RGBL

https://en.wikipedia.org/wiki/Rec._709 , linear RGB color space

COLOR_SPACE_REC_2020_RGB
Python: cv.ccm.COLOR_SPACE_REC_2020_RGB

https://en.wikipedia.org/wiki/Rec._2020 , RGB color space

COLOR_SPACE_REC_2020_RGBL
Python: cv.ccm.COLOR_SPACE_REC_2020_RGBL

https://en.wikipedia.org/wiki/Rec._2020 , linear RGB color space

COLOR_SPACE_XYZ_D65_2
Python: cv.ccm.COLOR_SPACE_XYZ_D65_2

https://en.wikipedia.org/wiki/CIE_1931_color_space , non-RGB color space

COLOR_SPACE_XYZ_D65_10
Python: cv.ccm.COLOR_SPACE_XYZ_D65_10

non-RGB color space

COLOR_SPACE_XYZ_D50_2
Python: cv.ccm.COLOR_SPACE_XYZ_D50_2

non-RGB color space

COLOR_SPACE_XYZ_D50_10
Python: cv.ccm.COLOR_SPACE_XYZ_D50_10

non-RGB color space

COLOR_SPACE_XYZ_A_2
Python: cv.ccm.COLOR_SPACE_XYZ_A_2

non-RGB color space

COLOR_SPACE_XYZ_A_10
Python: cv.ccm.COLOR_SPACE_XYZ_A_10

non-RGB color space

COLOR_SPACE_XYZ_D55_2
Python: cv.ccm.COLOR_SPACE_XYZ_D55_2

non-RGB color space

COLOR_SPACE_XYZ_D55_10
Python: cv.ccm.COLOR_SPACE_XYZ_D55_10

non-RGB color space

COLOR_SPACE_XYZ_D75_2
Python: cv.ccm.COLOR_SPACE_XYZ_D75_2

non-RGB color space

COLOR_SPACE_XYZ_D75_10
Python: cv.ccm.COLOR_SPACE_XYZ_D75_10

non-RGB color space

COLOR_SPACE_XYZ_E_2
Python: cv.ccm.COLOR_SPACE_XYZ_E_2

non-RGB color space

COLOR_SPACE_XYZ_E_10
Python: cv.ccm.COLOR_SPACE_XYZ_E_10

non-RGB color space

COLOR_SPACE_Lab_D65_2
Python: cv.ccm.COLOR_SPACE_Lab_D65_2

https://en.wikipedia.org/wiki/CIELAB_color_space , non-RGB color space

COLOR_SPACE_Lab_D65_10
Python: cv.ccm.COLOR_SPACE_Lab_D65_10

non-RGB color space

COLOR_SPACE_Lab_D50_2
Python: cv.ccm.COLOR_SPACE_Lab_D50_2

non-RGB color space

COLOR_SPACE_Lab_D50_10
Python: cv.ccm.COLOR_SPACE_Lab_D50_10

non-RGB color space

COLOR_SPACE_Lab_A_2
Python: cv.ccm.COLOR_SPACE_Lab_A_2

non-RGB color space

COLOR_SPACE_Lab_A_10
Python: cv.ccm.COLOR_SPACE_Lab_A_10

non-RGB color space

COLOR_SPACE_Lab_D55_2
Python: cv.ccm.COLOR_SPACE_Lab_D55_2

non-RGB color space

COLOR_SPACE_Lab_D55_10
Python: cv.ccm.COLOR_SPACE_Lab_D55_10

non-RGB color space

COLOR_SPACE_Lab_D75_2
Python: cv.ccm.COLOR_SPACE_Lab_D75_2

non-RGB color space

COLOR_SPACE_Lab_D75_10
Python: cv.ccm.COLOR_SPACE_Lab_D75_10

non-RGB color space

COLOR_SPACE_Lab_E_2
Python: cv.ccm.COLOR_SPACE_Lab_E_2

non-RGB color space

COLOR_SPACE_Lab_E_10
Python: cv.ccm.COLOR_SPACE_Lab_E_10

non-RGB color space

## ◆ CONST_COLOR

 enum cv::ccm::CONST_COLOR

#include <opencv2/mcc/ccm.hpp>

Macbeth and Vinyl ColorChecker with 2deg D50.

Enumerator
COLORCHECKER_Macbeth
Python: cv.ccm.COLORCHECKER_Macbeth

Macbeth ColorChecker.

COLORCHECKER_Vinyl
Python: cv.ccm.COLORCHECKER_Vinyl

DKK ColorChecker.

COLORCHECKER_DigitalSG
Python: cv.ccm.COLORCHECKER_DigitalSG

DigitalSG ColorChecker with 140 squares.

## ◆ DISTANCE_TYPE

#include <opencv2/mcc/ccm.hpp>

Enum of possible functions to calculate the distance between colors.

See https://en.wikipedia.org/wiki/Color_difference for details

Enumerator
DISTANCE_CIE76
Python: cv.ccm.DISTANCE_CIE76

The 1976 formula is the first formula that related a measured color difference to a known set of CIELAB coordinates.

DISTANCE_CIE94_GRAPHIC_ARTS
Python: cv.ccm.DISTANCE_CIE94_GRAPHIC_ARTS

The 1976 definition was extended to address perceptual non-uniformities.

DISTANCE_CIE94_TEXTILES
Python: cv.ccm.DISTANCE_CIE94_TEXTILES
DISTANCE_CIE2000
Python: cv.ccm.DISTANCE_CIE2000
DISTANCE_CMC_1TO1
Python: cv.ccm.DISTANCE_CMC_1TO1

In 1984, the Colour Measurement Committee of the Society of Dyers and Colourists defined a difference measure, also based on the L*C*h color model.

DISTANCE_CMC_2TO1
Python: cv.ccm.DISTANCE_CMC_2TO1
DISTANCE_RGB
Python: cv.ccm.DISTANCE_RGB

Euclidean distance of rgb color space.

DISTANCE_RGBL
Python: cv.ccm.DISTANCE_RGBL

Euclidean distance of rgbl color space.

## ◆ INITIAL_METHOD_TYPE

#include <opencv2/mcc/ccm.hpp>

Enum of the possible types of initial method.

Enumerator
INITIAL_METHOD_WHITE_BALANCE
Python: cv.ccm.INITIAL_METHOD_WHITE_BALANCE

The white balance method. The initial value is:

INITIAL_METHOD_LEAST_SQUARE
Python: cv.ccm.INITIAL_METHOD_LEAST_SQUARE

the least square method is an optimal solution under the linear RGB distance function

$$M_{CCM}= \begin{bmatrix} k_R & 0 & 0\\ 0 & k_G & 0\\ 0 & 0 & k_B\\ \end{bmatrix}$$
where
$$k_R=mean(R_{li}')/mean(R_{li})\\ k_R=mean(G_{li}')/mean(G_{li})\\ k_R=mean(B_{li}')/mean(B_{li})$$

## ◆ LINEAR_TYPE

 enum cv::ccm::LINEAR_TYPE

#include <opencv2/mcc/ccm.hpp>

Linearization transformation type.

The first step in color correction is to linearize the detected colors. Because the input color space has not been calibrated, we usually use some empirical methods to linearize. There are several common linearization methods. The first is identical transformation, the second is gamma correction, and the third is polynomial fitting.

Linearization is generally an elementwise function. The mathematical symbols are as follows:

$$C$$: any channel of a color, could be $$R, G$$ or $$B$$.

$$R, G, B$$: $$R, G, B$$ channels respectively.

$$G$$: grayscale;

$$s,sl$$: subscript, which represents the detected data and its linearized value, the former is the input and the latter is the output;

$$d,dl$$: subscript, which represents the reference data and its linearized value

### Identical Transformation

No change is made during the Identical transformation linearization, usually because the tristimulus values of the input RGB image is already proportional to the luminance. For example, if the input measurement data is in RAW format, the measurement data is already linear, so no linearization is required.

The identity transformation formula is as follows:

$C_{sl}=C_s$

### Gamma Correction

Gamma correction is a means of performing nonlinearity in RGB space, see the Color Space documentation for details. In the linearization part, the value of $$\gamma$$ is usually set to 2.2. You can also customize the value.

The formula for gamma correction linearization is as follows:

$C_{sl}=C_s^{\gamma},\qquad C_s\ge0\\ C_{sl}=-(-C_s)^{\gamma},\qquad C_s<0\\\\$

### Polynomial Fitting

Polynomial fitting uses polynomials to linearize. Provided the polynomial is:

$f(x)=a_nx^n+a_{n-1}x^{n-1}+... +a_0$

Then:

$C_{sl}=f(C_s)$

In practice, $$n\le3$$ is used to prevent overfitting.

There are many variants of polynomial fitting, the difference lies in the way of generating $$f(x)$$. It is usually necessary to use linearized reference colors and corresponding detected colors to calculate the polynomial parameters. However, not all colors can participate in the calculation. The saturation detected colors needs to be removed. See the algorithm introduction document for details.

#### Fitting Channels Respectively

Use three polynomials, $$r(x), g(x), b(x)$$, to linearize each channel of the RGB color space[1-3]:

$R_{sl}=r(R_s)\\ G_{sl}=g(G_s)\\ B_{sl}=b(B_s)\\$

The polynomial is generated by minimizing the residual sum of squares between the detected data and the linearized reference data. Take the R-channel as an example:

$R=\arg min_{f}(\Sigma(R_{dl}-f(R_S)^2)$

It's equivalent to finding the least square regression for below equations:

$f(R_{s1})=R_{dl1}\\ f(R_{s2})=R_{dl2}\\ ...$

With a polynomial, the above equations becomes:

$\begin{bmatrix} R_{s1}^{n} & R_{s1}^{n-1} & ... & 1\\ R_{s2}^{n} & R_{s2}^{n-1} & ... & 1\\ ... & ... & ... & ... \end{bmatrix} \begin{bmatrix} a_{n}\\ a_{n-1}\\ ... \\ a_0 \end{bmatrix} = \begin{bmatrix} R_{dl1}\\ R_{dl2}\\ ... \end{bmatrix}$

It can be expressed as a system of linear equations:

$AX=B$

When the number of reference colors is not less than the degree of the polynomial, the linear system has a least-squares solution:

$X=(A^TA)^{-1}A^TB$

Once we get the polynomial coefficients, we can get the polynomial r.

This method of finding polynomial coefficients can be implemented by numpy.polyfit in numpy, expressed here as:

$R=polyfit(R_S, R_{dl})$

Note that, in general, the polynomial that we want to obtain is guaranteed to monotonically increase in the interval [0,1] , but this means that nonlinear method is needed to generate the polynomials(see [4] for detail). This would greatly increases the complexity of the program. Considering that the monotonicity does not affect the correct operation of the color correction program, polyfit is still used to implement the program.

Parameters for other channels can also be derived in a similar way.

#### Grayscale Polynomial Fitting

In this method[2], single polynomial is used for all channels. The polynomial is still a polyfit result from the detected colors to the linear reference colors. However, only the gray of the reference colors can participate in the calculation.

Since the detected colors corresponding to the gray of reference colors is not necessarily gray, it needs to be grayed. Grayscale refers to the Y channel of the XYZ color space. The color space of the detected data is not determined and cannot be converted into the XYZ space. Therefore, the sRGB formula is used to approximate[5].

$G_{s}=0.2126R_{s}+0.7152G_{s}+0.0722B_{s}$

Then the polynomial parameters can be obtained by using the polyfit.

$f=polyfit(G_{s}, G_{dl})$

After $$f$$ is obtained, linearization can be performed.

#### Logarithmic Polynomial Fitting

For gamma correction formula, we take the logarithm:

$ln(C_{sl})={\gamma}ln(C_s),\qquad C_s\ge0\$

It can be seen that there is a linear relationship between $$ln(C_s)$$ and $$ln(C_{sl})$$. It can be considered that the formula is an approximation of a polynomial relationship, that is, there exists a polynomial $$f$$, which makes[2]:

$ln(C_{sl})=f(ln(C_s)), \qquad C_s>0\\ C_{sl}=0, \qquad C_s=0$

Because $$exp(ln(0))\to\infty$$, the channel whose component is 0 is directly mapped to 0 in the formula above.

For fitting channels respectively, we have:

$r=polyfit(ln(R_s),ln(R_{dl}))\\ g=polyfit(ln(G_s),ln(G_{dl}))\\ b=polyfit(ln(B_s),ln(B_{dl}))\\$

Note that the parameter of $$ln(*)$$ cannot be 0. Therefore, we need to delete the channels whose values are 0 from $$R_s$$ and $$R_{dl}$$, $$G_s$$ and $$G_{dl}$$, $$B_s$$ and $$B_{dl}$$.

Therefore:

$ln(R_{sl})=r(ln(R_s)), \qquad R_s>0\\ R_{sl}=0, \qquad R_s=0\\ ln(G_{sl})=g(ln(G_s)),\qquad G_s>0\\ G_{sl}=0, \qquad G_s=0\\ ln(B_{sl})=b(ln(B_s)),\qquad B_s>0\\ B_{sl}=0, \qquad B_s=0\\$

For grayscale polynomials, there are also:

$f=polyfit(ln(G_{sl}),ln(G_{dl}))$

and:

$ln(C_{sl})=f(ln(C_s)), \qquad C_s>0\\ C_sl=0, \qquad C_s=0$

Enumerator
LINEARIZATION_IDENTITY
Python: cv.ccm.LINEARIZATION_IDENTITY

LINEARIZATION_GAMMA
Python: cv.ccm.LINEARIZATION_GAMMA

gamma correction; Need assign a value to gamma simultaneously

LINEARIZATION_COLORPOLYFIT
Python: cv.ccm.LINEARIZATION_COLORPOLYFIT

polynomial fitting channels respectively; Need assign a value to deg simultaneously

LINEARIZATION_COLORLOGPOLYFIT
Python: cv.ccm.LINEARIZATION_COLORLOGPOLYFIT

logarithmic polynomial fitting channels respectively; Need assign a value to deg simultaneously

LINEARIZATION_GRAYPOLYFIT
Python: cv.ccm.LINEARIZATION_GRAYPOLYFIT

grayscale polynomial fitting; Need assign a value to deg and dst_whites simultaneously

LINEARIZATION_GRAYLOGPOLYFIT
Python: cv.ccm.LINEARIZATION_GRAYLOGPOLYFIT

grayscale Logarithmic polynomial fitting; Need assign a value to deg and dst_whites simultaneously