OpenCV
5.0.0-pre
Open Source Computer Vision
|
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.
Classes | |
class | cv::ccm::ColorCorrectionModel |
Core class of ccm model. More... | |
enum cv::ccm::CCM_TYPE |
#include <opencv2/mcc/ccm.hpp>
Enum of the possible types of ccm.
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 |
COLOR_SPACE_AdobeRGB Python: cv.ccm.COLOR_SPACE_AdobeRGB | https://en.wikipedia.org/wiki/Adobe_RGB_color_space , RGB color space |
COLOR_SPACE_AdobeRGBL Python: cv.ccm.COLOR_SPACE_AdobeRGBL | 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 |
enum cv::ccm::CONST_COLOR |
#include <opencv2/mcc/ccm.hpp>
Macbeth and Vinyl ColorChecker with 2deg D50.
#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
#include <opencv2/mcc/ccm.hpp>
Enum of the possible types of initial method.
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
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 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 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.
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.
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.
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 \]