Linearization Transformation For Color Correction {#tutorial_ccm_linearization_transformation}
============================

Overview
------------

The first step in color correction is to linearize the detected colors. Since the input color space may not be calibrated, empirical methods are used for linearization. The most common methods include:

1. Identical Transformation
2. Gamma Correction
3. Polynomial Fitting

Linearization is typically an element-wise function. The following symbols are used:

\f$C\f$: Any color channel (\f$R, G\f$, or \f$B\f$)
\f$R, G, B\f$: Respective color channels
\f$G\f$: Grayscale
\f$s, sl\f$: Represents the detected data and its linearized value, the former is the input and the latter is the output
\f$d, dl\f$: 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.

**Formula:**
\f[
C_{sl}=C_s
\f]

---

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 \f$gamma\f$ is usually set to 2.2.
You can also customize the value.

**Formulas:**
\f[
\begin{aligned}
C_{sl}=C_s^{\gamma},\qquad C_s\ge0\\
C_{sl}=-(-C_s)^{\gamma},\qquad C_s<0\\\\
\end{aligned}
\f]

---

Polynomial Fitting
------------

Linearization using polynomial fitting.

**Polynomial form:**
\f[
f(x)=a_nx^n+a_{n-1}x^{n-1}+... +a_0
\f]
Then:
\f[
C_{sl}=f(C_s)
\f]

*Usually n ≤ 3 to avoid overfitting.*\
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, \f$r(x), g(x), b(x)\f$, to linearize each channel of the RGB color space[1-3]:
\f[
\begin{aligned}
R_{sl}=r(R_s)\\
G_{sl}=g(G_s)\\
B_{sl}=b(B_s)\\
\end{aligned}
\f]
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:

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

It's equivalent to finding the least square regression for below equations:
\f[
\begin{aligned}
f(R_{s1})=R_{dl1}\\
f(R_{s2})=R_{dl2}\\
...
\end{aligned}
\f]

With a polynomial, the equations become:
\f[
\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}
\f]
This can be expressed in matrix form as:
\f[
AX=B
\f]
**Coefficient calculation:**
\f[
X=(A^TA)^{-1}A^TB
\f]
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:
\f[
R=polyfit(R_S, R_{dl})
\f]
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].
\f[
G_{s}=0.2126R_{s}+0.7152G_{s}+0.0722B_{s}
\f]
Then the polynomial parameters can be obtained by using the polyfit:
\f[
f=polyfit(G_{s}, G_{dl})
\f]
After \f$f\f$ is obtained, linearization can be performed.

### Logarithmic Polynomial Fitting
Takes the logarithm of gamma correction:
\f[
ln(C_{sl})={\gamma}ln(C_s),\qquad C_s\ge0\
\f]
It can be seen that there is a linear relationship between \f$ln(C_s)\f$ and \f$ln(C_{sl})\f$. It can be considered that the formula is an approximation of a polynomial relationship, that is, there exists a polynomial \f$f\f$, which makes[2]:
\f[
\begin{aligned}
ln(C_{sl})=f(ln(C_s)), \qquad C_s>0\\
C_{sl}=0, \qquad C_s=0
\end{aligned}
\f]

Because \f$exp(ln(0))\to\infty \f$, the channel component that is zero is directly mapped to zero in this formula.

**Fitted using polyfit on logarithmic values:**
\f[
\begin{aligned}
r=polyfit(ln(R_s),ln(R_{dl}))\\
g=polyfit(ln(G_s),ln(G_{dl}))\\
b=polyfit(ln(B_s),ln(B_{dl}))\\
\end{aligned}
\f]

Note: The parameter of \f$ln(*) \f$ cannot be zero. Therefore, we need to delete all channel values that are 0 from \f$R_s \f$ and \f$R_{dl} \f$, \f$G_s\f$ and \f$G_{dl}\f$, \f$B_s\f$ and \f$B_{dl}\f$.

The final fitting equations become:
\f[
\begin{aligned}
\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
\end{aligned}
\f]

For grayscale polynomials, there are also:
\f[
f=polyfit(ln(G_{sl}),ln(G_{dl}))
\f]
and:
\f[
\begin{aligned}
ln(C_{sl})=f(ln(C_s)), \qquad C_s>0\\
C_sl=0, \qquad C_s=0
\end{aligned}
\f]
---
The functionalities are included in:
@code{.cpp}
#include <opencv2/photo/ccm.hpp>
@endcode

Enum Definition
------------

```cpp
enum LINEAR_TYPE
{
    LINEARIZATION_IDENTITY,            // No change
    LINEARIZATION_GAMMA,               // Gamma correction; requires gamma value
    LINEARIZATION_COLORPOLYFIT,        // Polynomial fitting for each channel; requires degree
    LINEARIZATION_COLORLOGPOLYFIT,     // Logarithmic polynomial fitting; requires degree
    LINEARIZATION_GRAYPOLYFIT,         // Grayscale polynomial fitting; requires degree and dst_whites
    LINEARIZATION_GRAYLOGPOLYFIT       // Grayscale logarithmic polynomial fitting; requires degree and dst_whites
};
```

---

## References
- [1-3] Refer to polynomial fitting methods and empirical studies.
- [4] Describes nonlinear polynomial generation methods.
- [5] sRGB approximation for grayscale calculation.

This documentation is part of the OpenCV photo module.
