mirror of
https://github.com/opencv/opencv.git
synced 2026-07-25 21:33:04 +04:00
71c2b944a1
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.
84 lines
3.2 KiB
Python
Executable File
84 lines
3.2 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# This file is part of OpenCV project.
|
|
# It is subject to the license terms in the LICENSE file found in the top-level directory
|
|
# of this distribution and at http://opencv.org/license.html
|
|
|
|
import argparse
|
|
import sys
|
|
import cv2 as cv
|
|
|
|
USAGE = """\
|
|
Chromatic Aberration Correction Sample
|
|
Usage:
|
|
chromatic_aberration_correction.py <input_image> <calibration_file> [--bayer <code>] [--output <path>]
|
|
|
|
Arguments:
|
|
input_image Path to the input image. Can be:
|
|
• a 3-channel BGR image, or
|
|
• a 1-channel raw Bayer image (see bayer_pattern)
|
|
calibration_file OpenCV YAML/XML file with chromatic aberration calibration:
|
|
image_width, image_height, red_channel/coeffs_x, coeffs_y,
|
|
blue_channel/coeffs_x, coeffs_y.
|
|
output (optional) Path to save the corrected image. Default: corrected.png
|
|
bayer (optional) integer code for demosaicing a 1-channel raw image
|
|
If omitted or <0, input is assumed 3-channel BGR.
|
|
|
|
Example:
|
|
python chromatic_aberration_correction.py input.png calib.yaml --bayer 46 --output corrected.png
|
|
"""
|
|
|
|
def main(argv=None):
|
|
parser = argparse.ArgumentParser(
|
|
description="Chromatic Aberration Correction Sample",
|
|
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
epilog=USAGE
|
|
)
|
|
parser.add_argument("input", help="Input image (BGR or Bayer)")
|
|
parser.add_argument("calibration", help="Calibration file (YAML/XML)")
|
|
parser.add_argument("--output", default="corrected.png", help="Output image file")
|
|
parser.add_argument("--bayer", type=int, default=-1, help="Bayer pattern code for demosaic")
|
|
parser.add_argument("--no-gui", action="store_true", help="Do not open image windows")
|
|
|
|
args = parser.parse_args(argv)
|
|
|
|
img = cv.imread(args.input, cv.IMREAD_UNCHANGED)
|
|
if img is None:
|
|
print(f"ERROR: Could not load input image: {args.input}", file=sys.stderr)
|
|
return 1
|
|
|
|
fs = cv.FileStorage(args.calibration, cv.FileStorage_READ)
|
|
if not fs.isOpened():
|
|
print(f"Could not calibration coefficients from {args.calibration}")
|
|
return 1
|
|
|
|
try:
|
|
coeffMat, size, degree = cv.loadChromaticAberrationParams(fs.root())
|
|
corrected = cv.correctChromaticAberration(img, coeffMat, size, degree, args.bayer)
|
|
|
|
if corrected is None:
|
|
print("ERROR: cv.correctChromaticAberration returned None", file=sys.stderr)
|
|
return 1
|
|
|
|
if not args.no_gui:
|
|
cv.namedWindow("Original", cv.WINDOW_AUTOSIZE)
|
|
cv.namedWindow("Corrected", cv.WINDOW_AUTOSIZE)
|
|
cv.imshow("Original", img)
|
|
cv.imshow("Corrected", corrected)
|
|
print("Press any key to continue...")
|
|
cv.waitKey(0)
|
|
cv.destroyAllWindows()
|
|
|
|
if not cv.imwrite(args.output, corrected):
|
|
print(f"WARNING: Could not write output image: {args.output}", file=sys.stderr)
|
|
else:
|
|
print(f"Saved corrected image to: {args.output}")
|
|
|
|
except cv.error as e:
|
|
print(f"OpenCV error: {e}", file=sys.stderr)
|
|
return 1
|
|
|
|
return 0
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|