![]() |
OpenCV 5.0.0-pre
Open Source Computer Vision
|
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:
Linearization is typically an element-wise function. The following symbols are used:
\(C\): Any color channel ( \(R, G\), or \(B\)) \(R, G, B\): Respective color channels \(G\): Grayscale \(s, sl\): Represents the detected data and its linearized value, the former is the input and the latter is the output \(d, dl\): Reference data and its linearized value
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:
\[ C_{sl}=C_s \]
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.
Formulas:
\[ \begin{aligned} C_{sl}=C_s^{\gamma},\qquad C_s\ge0\\ C_{sl}=-(-C_s)^{\gamma},\qquad C_s<0\\\\ \end{aligned} \]
Linearization using polynomial fitting.
Polynomial form:
\[ f(x)=a_nx^n+a_{n-1}x^{n-1}+... +a_0 \]
Then:
\[ C_{sl}=f(C_s) \]
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.
Use three polynomials, \(r(x), g(x), b(x)\), to linearize each channel of the RGB color space[1-3]:
\[ \begin{aligned} R_{sl}=r(R_s)\\ G_{sl}=g(G_s)\\ B_{sl}=b(B_s)\\ \end{aligned} \]
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:
\[ \begin{aligned} f(R_{s1})=R_{dl1}\\ f(R_{s2})=R_{dl2}\\ ... \end{aligned} \]
With a polynomial, the equations become:
\[ \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} \]
This can be expressed in matrix form as:
\[ AX=B \]
Coefficient calculation:
\[ 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.
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.
Takes the logarithm of gamma correction:
\[ 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]:
\[ \begin{aligned} ln(C_{sl})=f(ln(C_s)), \qquad C_s>0\\ C_{sl}=0, \qquad C_s=0 \end{aligned} \]
Because \(exp(ln(0))\to\infty \), the channel component that is zero is directly mapped to zero in this formula.
Fitted using polyfit on logarithmic values:
\[ \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} \]
Note: The parameter of \(ln(*) \) cannot be zero. Therefore, we need to delete all channel values that are 0 from \(R_s \) and \(R_{dl} \), \(G_s\) and \(G_{dl}\), \(B_s\) and \(B_{dl}\).
The final fitting equations become:
\[ \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} \]
For grayscale polynomials, there are also:
\[ f=polyfit(ln(G_{sl}),ln(G_{dl})) \]
and:
\[ \begin{aligned} ln(C_{sl})=f(ln(C_s)), \qquad C_s>0\\ C_sl=0, \qquad C_s=0 \end{aligned} \]
The functionalities are included in:
This documentation is part of the OpenCV photo module.