mirror of
https://github.com/opencv/opencv.git
synced 2026-07-30 07:43:03 +04:00
Merge pull request #27051 from gursimarsingh:move_ccm_to_photo_module
Adding color correction module to photo module from opencv_contrib #27051 This PR moved color correction module from opencv_contrib to main repo inside photo module. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
@@ -1,4 +0,0 @@
|
||||
Computational photography (photo module) {#tutorial_table_of_content_photo}
|
||||
========================================
|
||||
|
||||
Content has been moved to this page: @ref tutorial_table_of_content_other
|
||||
@@ -1,7 +1,6 @@
|
||||
Other tutorials (photo, stitching, video) {#tutorial_table_of_content_other}
|
||||
Other tutorials (stitching, video) {#tutorial_table_of_content_other}
|
||||
========================================================
|
||||
|
||||
- photo. @subpage tutorial_hdr_imaging
|
||||
- stitching. @subpage tutorial_stitcher
|
||||
- video. @subpage tutorial_background_subtraction
|
||||
- video. @subpage tutorial_meanshift
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
Color Correction Model{#tutorial_ccm_color_correction_model}
|
||||
===========================
|
||||
|
||||
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.
|
||||
|
||||
In this tutorial you will learn how to use the 'Color Correction Model' to do a color correction in a image.
|
||||
|
||||
The color correction functionalities are included in:
|
||||
```cpp
|
||||
#include <opencv2/photo/ccm.hpp>
|
||||
```
|
||||
|
||||
Reference
|
||||
----
|
||||
|
||||
See details of ColorCorrection Algorithm at https://github.com/riskiest/color_calibration/tree/v4/doc/pdf/English/Algorithm
|
||||
|
||||
Source Code of the sample
|
||||
-----------
|
||||
|
||||
The sample has two parts of code, the first is the color checker detector model, see details at tutorial_macbeth_chart_detection, the second part is to make color calibration.
|
||||
|
||||
```
|
||||
Here are the parameters for ColorCorrectionModel
|
||||
src :
|
||||
detected colors of ColorChecker patches;
|
||||
NOTICE: the color type is RGB not BGR, and the color values are in [0, 1];
|
||||
constcolor :
|
||||
the Built-in color card;
|
||||
Supported list:
|
||||
Macbeth: Macbeth ColorChecker ;
|
||||
Vinyl: DKK ColorChecker ;
|
||||
DigitalSG: DigitalSG ColorChecker with 140 squares;
|
||||
Mat colors :
|
||||
the reference color values
|
||||
and corresponding color space
|
||||
NOTICE: the color values are in [0, 1]
|
||||
refColorSpace :
|
||||
the corresponding color space
|
||||
If the color type is some RGB, the format is RGB not BGR;
|
||||
Supported Color Space:
|
||||
Must be one of the members of the ColorSpace enum.
|
||||
@snippet modules/photo/include/opencv2/photo/ccm.hpp ColorSpace
|
||||
For the full, up-to-date list see cv::ccm::ColorSpace in ccm.hpp.
|
||||
```
|
||||
|
||||
|
||||
## Code
|
||||
|
||||
@snippet samples/cpp/color_correction_model.cpp tutorial
|
||||
@@ -0,0 +1,231 @@
|
||||
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.
|
||||
@@ -0,0 +1,6 @@
|
||||
Photo (photo module) {#tutorial_table_of_content_photo}
|
||||
==========================================================
|
||||
|
||||
- @subpage tutorial_hdr_imaging
|
||||
- @subpage tutorial_ccm_color_correction_model
|
||||
- @subpage tutorial_ccm_linearization_transformation
|
||||
@@ -9,9 +9,10 @@ OpenCV Tutorials {#tutorial_root}
|
||||
- @subpage tutorial_table_of_content_objdetect - INSERT OBJDETECT MODULE INFO
|
||||
- @subpage tutorial_table_of_content_features - feature detectors, descriptors and matching framework
|
||||
- @subpage tutorial_table_of_content_dnn - infer neural networks using built-in _dnn_ module
|
||||
- @subpage tutorial_table_of_content_other - other modules (stitching, video, photo)
|
||||
- @subpage tutorial_table_of_content_other - other modules (stitching, video)
|
||||
- @subpage tutorial_table_of_content_ios - running OpenCV on an iDevice
|
||||
- @subpage tutorial_table_of_content_3d - 3d objects processing and visualisation
|
||||
- @subpage tutorial_table_of_content_photo - photo module functions (hdr_image, ccm)
|
||||
@cond CUDA_MODULES
|
||||
- @subpage tutorial_table_of_content_gpu - utilizing power of video card to run CV algorithms
|
||||
@endcond
|
||||
|
||||
Reference in New Issue
Block a user