1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-31 08:13:04 +04:00

Merge pull request #27491 from MykhailoTrushch:ca_cpp

Chromatic aberration correction #27491

Merge with https://github.com/opencv/opencv_extra/pull/1266

### 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

Parent issue: https://github.com/opencv/opencv/issues/27206
Related PR: [#27490](https://github.com/opencv/opencv/pull/27490)

This PR adds chromatic aberration correction in C++ based on calibration data from the python app.

This code adds a function for chromatic aberration correction based on the calibration file (Mat correctChromaticAberration(InputArray image, const String& calibration_file)), and a class ChromaticAberrationCorrector which can be used to correct images of the same camera under the same settings (so for the same calibration data that is initialized in the beginning).

Also, basic functionality and performance tests are added.
This commit is contained in:
Mykhailo Trushch
2025-12-19 13:02:57 +01:00
committed by GitHub
parent 2ea31d5075
commit 71c2b944a1
14 changed files with 1667 additions and 0 deletions
+63
View File
@@ -895,6 +895,69 @@ CV_EXPORTS_W void stylization(InputArray src, OutputArray dst, float sigma_s = 6
//! @} photo_render
//! @addtogroup photo_ca_correction Chromatic Aberration Correction
//! @{
/** @example samples/cpp/snippets/chromatic_aberration_correction.cpp
An example correcting chromatic aberration with C++
*/
/** @example samples/python/snippets/chromatic_aberration_correction.py
* An example correcting chromatic aberration with Python
*/
/** @brief Corrects lateral chromatic aberration in an image using polynomial distortion model.
This function loads polynomial calibration data from the specified file and applies
a channelspecific warp to remove chromatic aberration.
If @p input_image has one channel, it is assumed to be a raw Bayer image and is
first demosaiced using @p bayer_pattern. If it has three channels, it is treated
as a BGR image and @p bayer_pattern is ignored.
Firstly, calibration needs to be done using apps/chromatic-aberration-calibration/ca_calibration.py on a photo of
a pattern of black discs on white background, included in opencv_extra/testdata/cv/cameracalibration/chromatic_aberration/chromatic_aberration_pattern_a3.png
Calibration and correction are based on the algorithm described in @cite rudakova2013precise.
The chromatic aberration is modeled as a polynomial of some degree in red and blue channels compared to green.
In calibration, a photo of many black discs on white background is used, and the displacements
between the centres of discs in red and blue channels compared to green are minimized. The coefficients
are then saved in a yaml file which can be used with this function to correct lateral chromatic aberration.
@param input_image Input BGR image to correct
@param coefficients Coefficient model
@param output_image Corrected BGR image
@param image_size Size of images for the calibration coefficient model
@param calib_degree Degree of the calibration coefficient model
@param bayer_pattern Bayer pattern code (e.g. cv::COLOR_BayerBG2BGR) used for
demosaicing when @p input_image has one channel; ignored otherwise.
@sa loadChromaticAberrationParams, demosaicing
*/
CV_EXPORTS_W void correctChromaticAberration(InputArray input_image, InputArray coefficients, OutputArray output_image,
const Size& image_size, int calib_degree, int bayer_pattern = -1);
/** @brief Load chromatic-aberration calibration parameters from opened FileStorage.
*
R e*ads the red and blue polynomial coefficients from the specified file and
packs them into a 4×N CV_32F matrix:
row 0 = blue dx coefficients
row 1 = blue dy coefficients
row 2 = red dx coefficients
row 3 = red dy coefficients
@param FileNode Node of opened cv::FileStorage object.
@param coeffMat Output 4xN coefficient matrix (CV_32F).
@param degree Polynomial degree inferred from N.
@param calib_size Calibration image size read from file.
@sa correctChromaticAberration
*/
CV_EXPORTS_W void loadChromaticAberrationParams(
const FileNode& node,
OutputArray coeffMat,
CV_OUT Size& calib_size,
CV_OUT int& degree);
//! @} photo_ca_correction
//! @} photo
} // cv