vendor: OpenCV 5.0.0 snapshot at 40738fb16ceddb5fb3fea747585f7ce6abb0605b

This commit is contained in:
Gitea Mirror Bot
2026-08-22 00:10:33 +08:00
commit f7f077da11
6933 changed files with 2335208 additions and 0 deletions
@@ -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.
+204
View File
@@ -0,0 +1,204 @@
High Dynamic Range Imaging {#tutorial_hdr_imaging}
==========================
@tableofcontents
@next_tutorial{tutorial_stitcher}
| | |
| -: | :- |
| Original author | Fedor Morozov |
| Compatibility | OpenCV >= 3.0 |
Introduction
------------
Today most digital images and imaging devices use 8 bits per channel thus limiting the dynamic range
of the device to two orders of magnitude (actually 256 levels), while human eye can adapt to
lighting conditions varying by ten orders of magnitude. When we take photographs of a real world
scene bright regions may be overexposed, while the dark ones may be underexposed, so we cant
capture all details using a single exposure. HDR imaging works with images that use more that 8 bits
per channel (usually 32-bit float values), allowing much wider dynamic range.
There are different ways to obtain HDR images, but the most common one is to use photographs of the
scene taken with different exposure values. To combine this exposures it is useful to know your
cameras response function and there are algorithms to estimate it. After the HDR image has been
blended it has to be converted back to 8-bit to view it on usual displays. This process is called
tonemapping. Additional complexities arise when objects of the scene or camera move between shots,
since images with different exposures should be registered and aligned.
In this tutorial we show how to generate and display HDR image from an exposure sequence. In our
case images are already aligned and there are no moving objects. We also demonstrate an alternative
approach called exposure fusion that produces low dynamic range image. Each step of HDR pipeline can
be implemented using different algorithms so take a look at the reference manual to see them all.
Exposure sequence
-----------------
![](images/memorial.png)
Source Code
-----------
@add_toggle_cpp
This tutorial code's is shown lines below. You can also download it from
[here](https://github.com/opencv/opencv/tree/5.x/samples/cpp/tutorial_code/photo/hdr_imaging/hdr_imaging.cpp)
@include samples/cpp/tutorial_code/photo/hdr_imaging/hdr_imaging.cpp
@end_toggle
@add_toggle_java
This tutorial code's is shown lines below. You can also download it from
[here](https://github.com/opencv/opencv/tree/5.x/samples/java/tutorial_code/photo/hdr_imaging/HDRImagingDemo.java)
@include samples/java/tutorial_code/photo/hdr_imaging/HDRImagingDemo.java
@end_toggle
@add_toggle_python
This tutorial code's is shown lines below. You can also download it from
[here](https://github.com/opencv/opencv/tree/5.x/samples/python/tutorial_code/photo/hdr_imaging/hdr_imaging.py)
@include samples/python/tutorial_code/photo/hdr_imaging/hdr_imaging.py
@end_toggle
Sample images
-------------
Data directory that contains images, exposure times and `list.txt` file can be downloaded from
[here](https://github.com/opencv/opencv_extra/tree/5.x/testdata/cv/hdr/exposures).
Explanation
-----------
- **Load images and exposure times**
@add_toggle_cpp
@snippet samples/cpp/tutorial_code/photo/hdr_imaging/hdr_imaging.cpp Load images and exposure times
@end_toggle
@add_toggle_java
@snippet samples/java/tutorial_code/photo/hdr_imaging/HDRImagingDemo.java Load images and exposure times
@end_toggle
@add_toggle_python
@snippet samples/python/tutorial_code/photo/hdr_imaging/hdr_imaging.py Load images and exposure times
@end_toggle
Firstly we load input images and exposure times from user-defined folder. The folder should
contain images and *list.txt* - file that contains file names and inverse exposure times.
For our image sequence the list is following:
@code{.none}
memorial00.png 0.03125
memorial01.png 0.0625
...
memorial15.png 1024
@endcode
- **Estimate camera response**
@add_toggle_cpp
@snippet samples/cpp/tutorial_code/photo/hdr_imaging/hdr_imaging.cpp Estimate camera response
@end_toggle
@add_toggle_java
@snippet samples/java/tutorial_code/photo/hdr_imaging/HDRImagingDemo.java Estimate camera response
@end_toggle
@add_toggle_python
@snippet samples/python/tutorial_code/photo/hdr_imaging/hdr_imaging.py Estimate camera response
@end_toggle
It is necessary to know camera response function (CRF) for a lot of HDR construction algorithms.
We use one of the calibration algorithms to estimate inverse CRF for all 256 pixel values.
- **Make HDR image**
@add_toggle_cpp
@snippet samples/cpp/tutorial_code/photo/hdr_imaging/hdr_imaging.cpp Make HDR image
@end_toggle
@add_toggle_java
@snippet samples/java/tutorial_code/photo/hdr_imaging/HDRImagingDemo.java Make HDR image
@end_toggle
@add_toggle_python
@snippet samples/python/tutorial_code/photo/hdr_imaging/hdr_imaging.py Make HDR image
@end_toggle
We use Debevec's weighting scheme to construct HDR image using response calculated in the previous
item.
- **Tonemap HDR image**
@add_toggle_cpp
@snippet samples/cpp/tutorial_code/photo/hdr_imaging/hdr_imaging.cpp Tonemap HDR image
@end_toggle
@add_toggle_java
@snippet samples/java/tutorial_code/photo/hdr_imaging/HDRImagingDemo.java Tonemap HDR image
@end_toggle
@add_toggle_python
@snippet samples/python/tutorial_code/photo/hdr_imaging/hdr_imaging.py Tonemap HDR image
@end_toggle
Since we want to see our results on common LDR display we have to map our HDR image to 8-bit range
preserving most details. It is the main goal of tonemapping methods. We use tonemapper with
bilateral filtering and set 2.2 as the value for gamma correction.
- **Perform exposure fusion**
@add_toggle_cpp
@snippet samples/cpp/tutorial_code/photo/hdr_imaging/hdr_imaging.cpp Perform exposure fusion
@end_toggle
@add_toggle_java
@snippet samples/java/tutorial_code/photo/hdr_imaging/HDRImagingDemo.java Perform exposure fusion
@end_toggle
@add_toggle_python
@snippet samples/python/tutorial_code/photo/hdr_imaging/hdr_imaging.py Perform exposure fusion
@end_toggle
There is an alternative way to merge our exposures in case when we don't need HDR image. This
process is called exposure fusion and produces LDR image that doesn't require gamma correction. It
also doesn't use exposure values of the photographs.
- **Write results**
@add_toggle_cpp
@snippet samples/cpp/tutorial_code/photo/hdr_imaging/hdr_imaging.cpp Write results
@end_toggle
@add_toggle_java
@snippet samples/java/tutorial_code/photo/hdr_imaging/HDRImagingDemo.java Write results
@end_toggle
@add_toggle_python
@snippet samples/python/tutorial_code/photo/hdr_imaging/hdr_imaging.py Write results
@end_toggle
Now it's time to look at the results. Note that HDR image can't be stored in one of common image
formats, so we save it to Radiance image (.hdr). Also all HDR imaging functions return results in
[0, 1] range so we should multiply result by 255.
You can try other tonemap algorithms: cv::TonemapDrago, cv::TonemapMantiuk and cv::TonemapReinhard
You can also adjust the parameters in the HDR calibration and tonemap methods for your own photos.
Results
-------
### Tonemapped image
![](images/ldr.png)
### Exposure fusion
![](images/fusion.png)
Additional Resources
--------------------
1. Paul E Debevec and Jitendra Malik. Recovering high dynamic range radiance maps from photographs. In ACM SIGGRAPH 2008 classes, page 31. ACM, 2008. @cite DM97
2. Mark A Robertson, Sean Borman, and Robert L Stevenson. Dynamic range improvement through multiple exposures. In Image Processing, 1999. ICIP 99. Proceedings. 1999 International Conference on, volume 3, pages 159163. IEEE, 1999. @cite RB99
3. Tom Mertens, Jan Kautz, and Frank Van Reeth. Exposure fusion. In Computer Graphics and Applications, 2007. PG'07. 15th Pacific Conference on, pages 382390. IEEE, 2007. @cite MK07
4. [Wikipedia-HDR](https://en.wikipedia.org/wiki/High-dynamic-range_imaging)
5. [Recovering High Dynamic Range Radiance Maps from Photographs (webpage)](http://www.pauldebevec.com/Research/HDR/)
@@ -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