Debian Codegraph @

planetary_system_stacker/Test_programs/RGB_Alignment.py

raw symbols SCIP
1 from cv2 import cvtColor, imread, COLOR_BGR2RGB, imshow, destroyAllWindows, \
2 waitKey, COLOR_RGB2BGR
3 from os.path import splitext
4
5 from miscellaneous import Miscellaneous
6 from frames import Frames
7 from exceptions import Error
8
9 def test_detailed(input_file_name, shift_red, shift_blue, interpolation_factor, reduction_factor):
10 # Change colors to standard RGB
11 input_image = cvtColor(imread(input_file_name, -1), COLOR_BGR2RGB)
12 image_original = Miscellaneous.shift_colors(input_image, (0, 0), (0, 0),
13 interpolate_input=interpolation_factor,
14 reduce_output=reduction_factor)
15 imshow('Original image', cvtColor(image_original, COLOR_RGB2BGR))
16 waitKey()
17 destroyAllWindows()
18
19 print("Input shifts:")
20 print("Red channel shift: (" + str(round(shift_red[0], 1)) + ", " +
21 str(round(shift_red[1], 1)) + ")")
22 print("Blue channel shift: (" + str(round(shift_blue[0], 1)) + ", " +
23 str(round(shift_blue[1], 1)) + ")")
24
25 image_shifted = Miscellaneous.shift_colors(input_image, shift_red, shift_blue,
26 interpolate_input=interpolation_factor)
27
28 imshow('RGB shifted image', cvtColor(image_shifted, COLOR_RGB2BGR))
29 waitKey()
30 destroyAllWindows()
31
32 max_shift = 5 * interpolation_factor
33 blurr_strength = 7
34 reference_channel = 1
35
36 print("\nMeasured shifts:")
37 channel_red = 0
38 try:
39 shift_red = Miscellaneous.measure_rgb_shift(image_shifted, channel_red, reference_channel,
40 max_shift, blur_strength=blurr_strength)
41 print("Red channel shift: (" + str(round(shift_red[0] / interpolation_factor, 1)) + ", " +
42 str(round(shift_red[1] / interpolation_factor, 1)) + ")")
43 except Error as e:
44 print("Error in measuring red shift: " + e.message)
45 channel_blue = 2
46 try:
47 shift_blue = Miscellaneous.measure_rgb_shift(image_shifted, channel_blue, reference_channel,
48 max_shift, blur_strength=blurr_strength)
49 print("Blue channel shift: (" + str(round(shift_blue[0] / interpolation_factor, 1)) + ", " +
50 str(round(shift_blue[1] / interpolation_factor, 1)) + ")")
51 except Error as e:
52 print("Error in measuring blue shift: " + e.message)
53
54 image_corrected = Miscellaneous.shift_colors(image_shifted, (-shift_red[0], -shift_red[1]),
55 (-shift_blue[0], -shift_blue[1]))
56
57 imshow('RGB shifted image', cvtColor(image_corrected, COLOR_RGB2BGR))
58 waitKey()
59 destroyAllWindows()
60
61 def create_shifted_image(input_file_name, shift_red, shift_blue, interpolation_factor):
62 # Change colors to standard RGB
63 input_image = cvtColor(imread(input_file_name, -1), COLOR_BGR2RGB)
64 shifted_image = Miscellaneous.shift_colors(input_image, shift_red, shift_blue,
65 interpolate_input=interpolation_factor,
66 reduce_output=interpolation_factor)
67
68 output_file_name= splitext(input_file_name)[0] + '_rgb-shifted.png'
69 Frames.save_image(output_file_name, shifted_image,
70 color=(len(shifted_image.shape) == 3), avoid_overwriting=False,
71 header="PlanetarySystemStacker")
72
73 def test_auto_rgbg_align(input_file_name, interpolation_factor, blur_strength):
74 # Change colors to standard RGB
75 shifted_image = cvtColor(imread(input_file_name, -1), COLOR_BGR2RGB)
76
77 imshow('RGB-aligned image', cvtColor(shifted_image, COLOR_RGB2BGR))
78 waitKey()
79 destroyAllWindows()
80
81 max_shift = 5
82 corrected_image, shift_red, shift_blue = Miscellaneous.auto_rgb_align(shifted_image, max_shift,
83 interpolation_factor=interpolation_factor, blur_strenght=blur_strength)
84
85 output_file_name = splitext(input_file_name)[0] + '_corrected.png'
86 Frames.save_image(output_file_name, corrected_image,
87 color=(len(corrected_image.shape) == 3), avoid_overwriting=False,
88 header="PlanetarySystemStacker")
89
90 print("Corrections applied:")
91 print("Red channel shift: (" + str(round(shift_red[0], 1)) + ", " +
92 str(round(shift_red[1], 1)) + ")")
93 print("Blue channel shift: (" + str(round(shift_blue[0], 1)) + ", " +
94 str(round(shift_blue[1], 1)) + ")")
95
96 imshow('RGB-aligned image', cvtColor(corrected_image, COLOR_RGB2BGR))
97 waitKey()
98 destroyAllWindows()
99
100 input_file_name = "D:\SW-Development\Python\PlanetarySystemStacker\Examples\Jupiter_Richard\\2020-07-29-2145_3-L-Jupiter_ALTAIRGP224C_pss_p70_b48.png"
101 input_file_name_shifted = "D:\SW-Development\Python\PlanetarySystemStacker\Examples\Jupiter_Richard\\2020-07-29-2145_3-L-Jupiter_ALTAIRGP224C_pss_p70_b48_rgb-shifted.png"
102
103 # input_file_name = "D:\SW-Development\Python\PlanetarySystemStacker\Examples\Jupiter_Richard\\2020-07-29-2145_3-L-Jupiter_ALTAIRGP224C_pss_p70_b48.png"
104 # input_file_name_shifted = "D:\SW-Development\Python\PlanetarySystemStacker\Examples\Jupiter_Richard\\2020-07-29-2145_3-L-Jupiter_ALTAIRGP224C_pss_p70_b48_rgb-shifted.png"
105
106 interpolation_factor = 4
107 reduction_factor = 1
108 blur_strength = 1
109 shift_red = (1.3, -3.2)
110 shift_blue = (-1.8, 1.1)
111
112 # test_detailed(input_file_name, shift_red, shift_blue, interpolation_factor, reduction_factor)
113 create_shifted_image(input_file_name, shift_red, shift_blue, interpolation_factor)
114 test_auto_rgbg_align(input_file_name_shifted, interpolation_factor, blur_strength)