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
"""
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
if not fs.isOpened():
print(f"Could not calibration coefficients from {args.calibration}")
return 1
try:
if corrected is None:
print("ERROR: cv.correctChromaticAberration returned None", file=sys.stderr)
return 1
if not args.no_gui:
print("Press any key to continue...")
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__":
XML/YAML/JSON file storage class that encapsulates all the information necessary for writing or readi...
Definition persistence.hpp:261
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
int waitKey(int delay=0)
Waits for a pressed key.
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
void destroyAllWindows()
Destroys all of the HighGUI windows.
bool imwrite(const String &filename, InputArray img, const std::vector< int > ¶ms=std::vector< int >())
Saves an image to a specified file.
Mat imread(const String &filename, int flags=IMREAD_COLOR_BGR)
Loads an image from a file.
void correctChromaticAberration(InputArray input_image, InputArray coefficients, OutputArray output_image, const Size &image_size, int calib_degree, int bayer_pattern=-1)
Corrects lateral chromatic aberration in an image using polynomial distortion model.
void loadChromaticAberrationParams(const FileNode &node, OutputArray coeffMat, Size &calib_size, int °ree)
Load chromatic-aberration calibration parameters from opened FileStorage.
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3