1
2
3
4
5
6import argparse
7import sys
8import cv2 as cv
9
10USAGE = """\
11Chromatic Aberration Correction Sample
12Usage:
13 chromatic_aberration_correction.py <input_image> <calibration_file> [--bayer <code>] [--output <path>]
14
15Arguments:
16 input_image Path to the input image. Can be:
17 • a 3-channel BGR image, or
18 • a 1-channel raw Bayer image (see bayer_pattern)
19 calibration_file OpenCV YAML/XML file with chromatic aberration calibration:
20 image_width, image_height, red_channel/coeffs_x, coeffs_y,
21 blue_channel/coeffs_x, coeffs_y.
22 output (optional) Path to save the corrected image. Default: corrected.png
23 bayer (optional) integer code for demosaicing a 1-channel raw image
24 If omitted or <0, input is assumed 3-channel BGR.
25
26Example:
27 python chromatic_aberration_correction.py input.png calib.yaml --bayer 46 --output corrected.png
28"""
29
31 parser = argparse.ArgumentParser(
32 description="Chromatic Aberration Correction Sample",
33 formatter_class=argparse.RawDescriptionHelpFormatter,
34 epilog=USAGE
35 )
36 parser.add_argument("input", help="Input image (BGR or Bayer)")
37 parser.add_argument("calibration", help="Calibration file (YAML/XML)")
38 parser.add_argument("--output", default="corrected.png", help="Output image file")
39 parser.add_argument("--bayer", type=int, default=-1, help="Bayer pattern code for demosaic")
40 parser.add_argument("--no-gui", action="store_true", help="Do not open image windows")
41
42 args = parser.parse_args(argv)
43
44 img =
cv.imread(args.input, cv.IMREAD_UNCHANGED)
45 if img is None:
46 print(f"ERROR: Could not load input image: {args.input}", file=sys.stderr)
47 return 1
48
50 if not fs.isOpened():
51 print(f"Could not calibration coefficients from {args.calibration}")
52 return 1
53
54 try:
57
58 if corrected is None:
59 print("ERROR: cv.correctChromaticAberration returned None", file=sys.stderr)
60 return 1
61
62 if not args.no_gui:
67 print("Press any key to continue...")
70
72 print(f"WARNING: Could not write output image: {args.output}", file=sys.stderr)
73 else:
74 print(f"Saved corrected image to: {args.output}")
75
76 except cv.error as e:
77 print(f"OpenCV error: {e}", file=sys.stderr)
78 return 1
79
80 return 0
81
82if __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