| 1 |
|
| 2 |
""" |
| 3 |
Copyright (c) 2019 Rolf Hempel, [email protected] |
| 4 |
|
| 5 |
This file is part of the PlanetarySystemStacker tool (PSS). |
| 6 |
https://github.com/Rolf-Hempel/PlanetarySystemStacker |
| 7 |
|
| 8 |
PSS is free software: you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation, either version 3 of the License, or |
| 11 |
(at your option) any later version. |
| 12 |
|
| 13 |
This program is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
GNU General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with PSS. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
|
| 21 |
""" |
| 22 |
|
| 23 |
import sys |
| 24 |
from configparser import ConfigParser |
| 25 |
from copy import deepcopy |
| 26 |
from os.path import expanduser, join, isfile, dirname |
| 27 |
from os.path import splitext |
| 28 |
|
| 29 |
import psutil |
| 30 |
from numpy import uint8 |
| 31 |
|
| 32 |
from exceptions import ArgumentError |
| 33 |
from miscellaneous import Miscellaneous |
| 34 |
|
| 35 |
|
| 36 |
PSS_Version = "PlanetarySystemStacker 0.9.8" |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
class ConfigurationParameters(object): |
| 41 |
def __init__(self): |
| 42 |
self.hidden_parameters_current_dir = None |
| 43 |
self.hidden_parameters_main_window_x0 = None |
| 44 |
self.hidden_parameters_main_window_y0 = None |
| 45 |
self.hidden_parameters_main_window_width = None |
| 46 |
self.hidden_parameters_main_window_height = None |
| 47 |
self.hidden_parameters_main_window_maximized = None |
| 48 |
self.global_parameters_display_quickstart = None |
| 49 |
self.global_parameters_version = None |
| 50 |
self.global_parameters_protocol_level = None |
| 51 |
self.global_parameters_write_protocol_to_file = None |
| 52 |
self.global_parameters_store_protocol_with_result = None |
| 53 |
self.global_parameters_buffering_level = None |
| 54 |
self.global_parameters_maximum_memory_active = None |
| 55 |
self.global_parameters_maximum_memory_amount = None |
| 56 |
self.global_parameters_include_postprocessing = None |
| 57 |
self.global_parameters_image_format = None |
| 58 |
self.global_parameters_parameters_in_filename = None |
| 59 |
self.global_parameters_stack_number_frames = None |
| 60 |
self.global_parameters_stack_percent_frames = None |
| 61 |
self.global_parameters_ap_box_size = None |
| 62 |
self.global_parameters_ap_number = None |
| 63 |
self.frames_gauss_width = None |
| 64 |
self.frames_debayering_default = None |
| 65 |
self.frames_debayering_method = None |
| 66 |
self.frames_normalization = None |
| 67 |
self.frames_normalization_threshold = None |
| 68 |
self.frames_add_selection_dialog = None |
| 69 |
self.align_frames_fast_changing_object = None |
| 70 |
self.align_frames_mode = None |
| 71 |
self.align_frames_automation = None |
| 72 |
self.align_frames_rectangle_scale_factor = None |
| 73 |
self.align_frames_search_width = None |
| 74 |
self.align_frames_average_frame_percent = None |
| 75 |
self.alignment_points_half_box_width = None |
| 76 |
self.alignment_points_search_width = None |
| 77 |
self.alignment_points_structure_threshold = None |
| 78 |
self.alignment_points_brightness_threshold = None |
| 79 |
self.alignment_points_frame_percent = None |
| 80 |
self.alignment_points_frame_number = None |
| 81 |
self.stack_frames_drizzle_factor_string = None |
| 82 |
|
| 83 |
def set_defaults(self): |
| 84 |
self.hidden_parameters_current_dir = expanduser("~") |
| 85 |
self.hidden_parameters_main_window_x0 = 100 |
| 86 |
self.hidden_parameters_main_window_y0 = 100 |
| 87 |
self.hidden_parameters_main_window_width = 1200 |
| 88 |
self.hidden_parameters_main_window_height = 800 |
| 89 |
self.hidden_parameters_main_window_maximized = False |
| 90 |
self.global_parameters_display_quickstart = True |
| 91 |
self.global_parameters_version = PSS_Version |
| 92 |
self.global_parameters_protocol_level = 1 |
| 93 |
self.global_parameters_write_protocol_to_file = False |
| 94 |
self.global_parameters_store_protocol_with_result = False |
| 95 |
self.global_parameters_buffering_level = -1 |
| 96 |
self.global_parameters_maximum_memory_active = False |
| 97 |
self.global_parameters_maximum_memory_amount = \ |
| 98 |
max(1, int(dict(psutil.virtual_memory()._asdict())['available'] / 1e9)) |
| 99 |
self.global_parameters_include_postprocessing = True |
| 100 |
self.global_parameters_image_format = "png" |
| 101 |
self.global_parameters_parameters_in_filename = False |
| 102 |
self.global_parameters_stack_number_frames = False |
| 103 |
self.global_parameters_stack_percent_frames = False |
| 104 |
self.global_parameters_ap_box_size = False |
| 105 |
self.global_parameters_ap_number = False |
| 106 |
self.frames_gauss_width = 7 |
| 107 |
self.frames_debayering_default = 'Auto detect color' |
| 108 |
self.frames_debayering_method = 'Bilinear' |
| 109 |
self.frames_normalization = True |
| 110 |
self.frames_normalization_threshold = 15 |
| 111 |
self.frames_add_selection_dialog = False |
| 112 |
self.align_frames_fast_changing_object = True |
| 113 |
self.align_frames_mode = 'Surface' |
| 114 |
self.align_frames_automation = True |
| 115 |
self.align_frames_rectangle_scale_factor = 3. |
| 116 |
self.align_frames_search_width = 34 |
| 117 |
self.align_frames_average_frame_percent = 5 |
| 118 |
self.alignment_points_search_width = 14 |
| 119 |
self.alignment_points_frame_percent = 10 |
| 120 |
self.alignment_points_frame_number = -1 |
| 121 |
self.stack_frames_drizzle_factor_string = "Off" |
| 122 |
self.set_defaults_ap_editing() |
| 123 |
|
| 124 |
def set_defaults_ap_editing(self): |
| 125 |
self.alignment_points_half_box_width = 24 |
| 126 |
self.alignment_points_structure_threshold = 0.04 |
| 127 |
self.alignment_points_brightness_threshold = 10 |
| 128 |
|
| 129 |
def copy_from_config_object(self, configuration_object): |
| 130 |
self.hidden_parameters_current_dir = configuration_object.hidden_parameters_current_dir |
| 131 |
self.hidden_parameters_main_window_x0 = \ |
| 132 |
configuration_object.hidden_parameters_main_window_x0 |
| 133 |
self.hidden_parameters_main_window_y0 = \ |
| 134 |
configuration_object.hidden_parameters_main_window_y0 |
| 135 |
self.hidden_parameters_main_window_width = \ |
| 136 |
configuration_object.hidden_parameters_main_window_width |
| 137 |
self.hidden_parameters_main_window_height = \ |
| 138 |
configuration_object.hidden_parameters_main_window_height |
| 139 |
self.hidden_parameters_main_window_maximized = \ |
| 140 |
configuration_object.hidden_parameters_main_window_maximized |
| 141 |
self.global_parameters_display_quickstart = \ |
| 142 |
configuration_object.global_parameters_display_quickstart |
| 143 |
self.global_parameters_version = configuration_object.global_parameters_version |
| 144 |
self.global_parameters_protocol_level = \ |
| 145 |
configuration_object.global_parameters_protocol_level |
| 146 |
self.global_parameters_write_protocol_to_file = \ |
| 147 |
configuration_object.global_parameters_write_protocol_to_file |
| 148 |
self.global_parameters_store_protocol_with_result = \ |
| 149 |
configuration_object.global_parameters_store_protocol_with_result |
| 150 |
self.global_parameters_buffering_level = \ |
| 151 |
configuration_object.global_parameters_buffering_level |
| 152 |
self.global_parameters_maximum_memory_active = \ |
| 153 |
configuration_object.global_parameters_maximum_memory_active |
| 154 |
self.global_parameters_maximum_memory_amount = \ |
| 155 |
configuration_object.global_parameters_maximum_memory_amount |
| 156 |
self.global_parameters_include_postprocessing = \ |
| 157 |
configuration_object.global_parameters_include_postprocessing |
| 158 |
self.global_parameters_image_format = \ |
| 159 |
configuration_object.global_parameters_image_format |
| 160 |
self.global_parameters_parameters_in_filename = \ |
| 161 |
configuration_object.global_parameters_parameters_in_filename |
| 162 |
self.global_parameters_stack_number_frames = \ |
| 163 |
configuration_object.global_parameters_stack_number_frames |
| 164 |
self.global_parameters_stack_percent_frames = \ |
| 165 |
configuration_object.global_parameters_stack_percent_frames |
| 166 |
self.global_parameters_ap_box_size = \ |
| 167 |
configuration_object.global_parameters_ap_box_size |
| 168 |
self.global_parameters_ap_number = \ |
| 169 |
configuration_object.global_parameters_ap_number |
| 170 |
self.frames_gauss_width = configuration_object.frames_gauss_width |
| 171 |
self.frames_debayering_default = configuration_object.frames_debayering_default |
| 172 |
self.frames_debayering_method = configuration_object.frames_debayering_method |
| 173 |
self.frames_normalization = configuration_object.frames_normalization |
| 174 |
self.frames_normalization_threshold = configuration_object.frames_normalization_threshold |
| 175 |
self.frames_add_selection_dialog = configuration_object.frames_add_selection_dialog |
| 176 |
self.align_frames_fast_changing_object = \ |
| 177 |
configuration_object.align_frames_fast_changing_object |
| 178 |
self.align_frames_mode = configuration_object.align_frames_mode |
| 179 |
self.align_frames_automation = configuration_object.align_frames_automation |
| 180 |
self.align_frames_rectangle_scale_factor = \ |
| 181 |
configuration_object.align_frames_rectangle_scale_factor |
| 182 |
self.align_frames_search_width = configuration_object.align_frames_search_width |
| 183 |
self.align_frames_average_frame_percent = \ |
| 184 |
configuration_object.align_frames_average_frame_percent |
| 185 |
self.alignment_points_half_box_width = configuration_object.alignment_points_half_box_width |
| 186 |
self.alignment_points_search_width = configuration_object.alignment_points_search_width |
| 187 |
self.alignment_points_structure_threshold = \ |
| 188 |
configuration_object.alignment_points_structure_threshold |
| 189 |
self.alignment_points_brightness_threshold = \ |
| 190 |
configuration_object.alignment_points_brightness_threshold |
| 191 |
self.alignment_points_frame_percent = configuration_object.alignment_points_frame_percent |
| 192 |
self.alignment_points_frame_number = configuration_object.alignment_points_frame_number |
| 193 |
self.stack_frames_drizzle_factor_string = \ |
| 194 |
configuration_object.stack_frames_drizzle_factor_string |
| 195 |
|
| 196 |
|
| 197 |
class Configuration(object): |
| 198 |
def __init__(self): |
| 199 |
|
| 200 |
self.global_parameters_version = PSS_Version |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
python_dir = dirname(sys.executable) |
| 207 |
icon_locations = ['PSS-Icon-64.png', join('Icons', 'PSS-Icon-64.png'), |
| 208 |
join(python_dir, "Lib", "site-packages", 'planetary_system_stacker', |
| 209 |
'Icons', 'PSS-Icon-64.png')] |
| 210 |
|
| 211 |
self.window_icon = None |
| 212 |
for location in icon_locations: |
| 213 |
if isfile(location): |
| 214 |
self.window_icon = location |
| 215 |
break |
| 216 |
|
| 217 |
self.frames_mono_channel = 'panchromatic' |
| 218 |
self.frames_color_difference_threshold = 0 |
| 219 |
self.frames_bayer_max_noise_diff_green = 2. |
| 220 |
self.frames_bayer_min_distance_from_blue = 99.5 |
| 221 |
|
| 222 |
self.rank_frames_pixel_stride = 2 |
| 223 |
self.rank_frames_method = "Laplace" |
| 224 |
|
| 225 |
self.align_frames_method = "MultiLevelCorrelation" |
| 226 |
self.align_frames_rectangle_black_threshold = 10240 |
| 227 |
self.align_frames_rectangle_min_fraction = 0.7 |
| 228 |
self.align_frames_rectangle_stride = 2 |
| 229 |
self.align_frames_border_width = 10 |
| 230 |
self.align_frames_sampling_stride = 2 |
| 231 |
self.align_frames_min_stabilization_patch_fraction = 0.2 |
| 232 |
self.align_frames_max_stabilization_patch_fraction = 0.7 |
| 233 |
self.align_frames_max_search_width = 150 |
| 234 |
self.align_frames_best_frames_window_extension = 2 |
| 235 |
|
| 236 |
self.alignment_points_min_half_box_width = 10 |
| 237 |
self.alignment_points_contrast_threshold = 0 |
| 238 |
self.alignment_points_dim_fraction_threshold = 0.6 |
| 239 |
self.alignment_points_rank_method = "Laplace" |
| 240 |
self.alignment_points_rank_pixel_stride = 2 |
| 241 |
self.alignment_points_de_warp = True |
| 242 |
self.alignment_points_method = 'MultiLevelCorrelation' |
| 243 |
self.alignment_points_sampling_stride = 2 |
| 244 |
self.alignment_points_local_search_subpixel = False |
| 245 |
self.alignment_points_penalty_factor = 0.00025 |
| 246 |
|
| 247 |
self.stack_frames_suffix = "_pss" |
| 248 |
self.stack_frames_background_blend_threshold = 0.2 |
| 249 |
self.stack_frames_background_fraction = 0.3 |
| 250 |
self.stack_frames_background_patch_size = 100 |
| 251 |
|
| 252 |
self.postproc_suffix = "_gpp" |
| 253 |
self.postproc_max_layers = 10 |
| 254 |
self.postproc_bi_range_standard = 13 |
| 255 |
self.postproc_max_shift = 5 |
| 256 |
self.postproc_max_ram_percentage = 20. |
| 257 |
self.postproc_blinking_period = 1. |
| 258 |
self.postproc_idle_loop_time = 0.2 |
| 259 |
|
| 260 |
|
| 261 |
self.config_parser_object = ConfigParser() |
| 262 |
|
| 263 |
|
| 264 |
self.postproc_data_object = PostprocDataObject(self.postproc_suffix) |
| 265 |
|
| 266 |
def initialize_configuration(self, read_from_file=True): |
| 267 |
""" |
| 268 |
Initialize the configuration with parameters stored during the previous PSS run. This |
| 269 |
initialization is skipped if PSS is executed from the command line. Finally, set derived |
| 270 |
parameters, i.e. parameters which are computed from other parameters. |
| 271 |
|
| 272 |
:param read_from_file: If True, read parameters from a configuration file. |
| 273 |
If False, skip this step. |
| 274 |
:return: - |
| 275 |
""" |
| 276 |
|
| 277 |
if read_from_file: |
| 278 |
|
| 279 |
|
| 280 |
self.home = expanduser("~") |
| 281 |
self.config_filename = join(self.home, ".PlanetarySystemStacker.ini") |
| 282 |
self.protocol_filename = join(self.home, "PlanetarySystemStacker.log") |
| 283 |
|
| 284 |
|
| 285 |
self.config_file_exists = isfile(self.config_filename) |
| 286 |
else: |
| 287 |
self.config_file_exists = False |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
self.configuration_read = False |
| 292 |
if self.config_file_exists: |
| 293 |
try: |
| 294 |
self.config_parser_object = self.read_config() |
| 295 |
self.configuration_read = True |
| 296 |
except: |
| 297 |
self.configuration_read = False |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
if not self.configuration_read: |
| 302 |
configuration_parameters = ConfigurationParameters() |
| 303 |
configuration_parameters.set_defaults() |
| 304 |
|
| 305 |
|
| 306 |
self.import_from_configuration_parameters(configuration_parameters) |
| 307 |
|
| 308 |
|
| 309 |
self.postproc_data_object = PostprocDataObject(self.postproc_suffix) |
| 310 |
|
| 311 |
|
| 312 |
self.set_derived_parameters() |
| 313 |
|
| 314 |
|
| 315 |
self.configuration_changed = False |
| 316 |
self.go_back_to_activity = None |
| 317 |
|
| 318 |
def import_from_configuration_parameters(self, configuration_parameters): |
| 319 |
""" |
| 320 |
Set all current parameters to the corresponding values of a ConfigurationParameters object. |
| 321 |
|
| 322 |
:param configuration_parameters: ConfigurationParameters object with new parameter values. |
| 323 |
:return: - |
| 324 |
""" |
| 325 |
|
| 326 |
self.hidden_parameters_current_dir = configuration_parameters.hidden_parameters_current_dir |
| 327 |
self.hidden_parameters_main_window_x0 = \ |
| 328 |
configuration_parameters.hidden_parameters_main_window_x0 |
| 329 |
self.hidden_parameters_main_window_y0 = \ |
| 330 |
configuration_parameters.hidden_parameters_main_window_y0 |
| 331 |
self.hidden_parameters_main_window_width = \ |
| 332 |
configuration_parameters.hidden_parameters_main_window_width |
| 333 |
self.hidden_parameters_main_window_height = \ |
| 334 |
configuration_parameters.hidden_parameters_main_window_height |
| 335 |
self.hidden_parameters_main_window_maximized = \ |
| 336 |
configuration_parameters.hidden_parameters_main_window_maximized |
| 337 |
self.global_parameters_display_quickstart = \ |
| 338 |
configuration_parameters.global_parameters_display_quickstart |
| 339 |
self.global_parameters_version = configuration_parameters.global_parameters_version |
| 340 |
self.global_parameters_protocol_level = \ |
| 341 |
configuration_parameters.global_parameters_protocol_level |
| 342 |
self.global_parameters_write_protocol_to_file = \ |
| 343 |
configuration_parameters.global_parameters_write_protocol_to_file |
| 344 |
self.global_parameters_store_protocol_with_result = \ |
| 345 |
configuration_parameters.global_parameters_store_protocol_with_result |
| 346 |
self.global_parameters_buffering_level = \ |
| 347 |
configuration_parameters.global_parameters_buffering_level |
| 348 |
self.global_parameters_maximum_memory_active = \ |
| 349 |
configuration_parameters.global_parameters_maximum_memory_active |
| 350 |
self.global_parameters_maximum_memory_amount = \ |
| 351 |
configuration_parameters.global_parameters_maximum_memory_amount |
| 352 |
self.global_parameters_include_postprocessing = \ |
| 353 |
configuration_parameters.global_parameters_include_postprocessing |
| 354 |
self.global_parameters_image_format = \ |
| 355 |
configuration_parameters.global_parameters_image_format |
| 356 |
self.global_parameters_parameters_in_filename = \ |
| 357 |
configuration_parameters.global_parameters_parameters_in_filename |
| 358 |
self.global_parameters_stack_number_frames = \ |
| 359 |
configuration_parameters.global_parameters_stack_number_frames |
| 360 |
self.global_parameters_stack_percent_frames = \ |
| 361 |
configuration_parameters.global_parameters_stack_percent_frames |
| 362 |
self.global_parameters_ap_box_size = \ |
| 363 |
configuration_parameters.global_parameters_ap_box_size |
| 364 |
self.global_parameters_ap_number = \ |
| 365 |
configuration_parameters.global_parameters_ap_number |
| 366 |
self.frames_gauss_width = configuration_parameters.frames_gauss_width |
| 367 |
self.frames_debayering_default = configuration_parameters.frames_debayering_default |
| 368 |
self.frames_debayering_method = configuration_parameters.frames_debayering_method |
| 369 |
self.frames_normalization = configuration_parameters.frames_normalization |
| 370 |
self.frames_normalization_threshold = \ |
| 371 |
configuration_parameters.frames_normalization_threshold |
| 372 |
self.frames_add_selection_dialog = \ |
| 373 |
configuration_parameters.frames_add_selection_dialog |
| 374 |
self.align_frames_fast_changing_object = \ |
| 375 |
configuration_parameters.align_frames_fast_changing_object |
| 376 |
self.align_frames_mode = configuration_parameters.align_frames_mode |
| 377 |
self.align_frames_automation = configuration_parameters.align_frames_automation |
| 378 |
self.align_frames_rectangle_scale_factor = \ |
| 379 |
configuration_parameters.align_frames_rectangle_scale_factor |
| 380 |
self.align_frames_search_width = configuration_parameters.align_frames_search_width |
| 381 |
self.align_frames_average_frame_percent = \ |
| 382 |
configuration_parameters.align_frames_average_frame_percent |
| 383 |
self.alignment_points_half_box_width = \ |
| 384 |
configuration_parameters.alignment_points_half_box_width |
| 385 |
self.alignment_points_search_width = configuration_parameters.alignment_points_search_width |
| 386 |
self.alignment_points_structure_threshold = \ |
| 387 |
configuration_parameters.alignment_points_structure_threshold |
| 388 |
self.alignment_points_brightness_threshold = \ |
| 389 |
configuration_parameters.alignment_points_brightness_threshold |
| 390 |
self.alignment_points_frame_percent = \ |
| 391 |
configuration_parameters.alignment_points_frame_percent |
| 392 |
self.alignment_points_frame_number = \ |
| 393 |
configuration_parameters.alignment_points_frame_number |
| 394 |
self.stack_frames_drizzle_factor_string = \ |
| 395 |
configuration_parameters.stack_frames_drizzle_factor_string |
| 396 |
|
| 397 |
def export_to_configuration_parameters(self, configuration_parameters): |
| 398 |
""" |
| 399 |
Set all values of a ConfigurarionParameters object to the current parameters. |
| 400 |
|
| 401 |
:param configuration_parameters: ConfigurarionParameters object to be updated. |
| 402 |
:return: - |
| 403 |
""" |
| 404 |
|
| 405 |
configuration_parameters.hidden_parameters_current_dir = self.hidden_parameters_current_dir |
| 406 |
configuration_parameters.hidden_parameters_main_window_x0 = \ |
| 407 |
self.hidden_parameters_main_window_x0 |
| 408 |
configuration_parameters.hidden_parameters_main_window_y0 = \ |
| 409 |
self.hidden_parameters_main_window_y0 |
| 410 |
configuration_parameters.hidden_parameters_main_window_width = \ |
| 411 |
self.hidden_parameters_main_window_width |
| 412 |
configuration_parameters.hidden_parameters_main_window_height = \ |
| 413 |
self.hidden_parameters_main_window_height |
| 414 |
configuration_parameters.hidden_parameters_main_window_maximized = \ |
| 415 |
self.hidden_parameters_main_window_maximized |
| 416 |
configuration_parameters.global_parameters_display_quickstart = \ |
| 417 |
self.global_parameters_display_quickstart |
| 418 |
configuration_parameters.global_parameters_version = self.global_parameters_version |
| 419 |
configuration_parameters.global_parameters_protocol_level = \ |
| 420 |
self.global_parameters_protocol_level |
| 421 |
configuration_parameters.global_parameters_write_protocol_to_file = \ |
| 422 |
self.global_parameters_write_protocol_to_file |
| 423 |
configuration_parameters.global_parameters_store_protocol_with_result = \ |
| 424 |
self.global_parameters_store_protocol_with_result |
| 425 |
configuration_parameters.global_parameters_include_postprocessing = \ |
| 426 |
self.global_parameters_include_postprocessing |
| 427 |
configuration_parameters.global_parameters_image_format = \ |
| 428 |
self.global_parameters_image_format |
| 429 |
configuration_parameters.global_parameters_parameters_in_filename = \ |
| 430 |
self.global_parameters_parameters_in_filename |
| 431 |
configuration_parameters.global_parameters_stack_number_frames = \ |
| 432 |
self.global_parameters_stack_number_frames |
| 433 |
configuration_parameters.global_parameters_stack_percent_frames = \ |
| 434 |
self.global_parameters_stack_percent_frames |
| 435 |
configuration_parameters.global_parameters_ap_box_size = \ |
| 436 |
self.global_parameters_ap_box_size |
| 437 |
configuration_parameters.global_parameters_ap_number = \ |
| 438 |
self.global_parameters_ap_number |
| 439 |
|
| 440 |
configuration_parameters.frames_gauss_width = self.frames_gauss_width |
| 441 |
configuration_parameters.frames_debayering_default = self.frames_debayering_default |
| 442 |
configuration_parameters.frames_debayering_method = self.frames_debayering_method |
| 443 |
configuration_parameters.frames_normalization = self.frames_normalization |
| 444 |
configuration_parameters.frames_normalization_threshold = self.frames_normalization_threshold |
| 445 |
configuration_parameters.frames_add_selection_dialog = self.frames_add_selection_dialog |
| 446 |
|
| 447 |
configuration_parameters.align_frames_fast_changing_object = \ |
| 448 |
self.align_frames_fast_changing_object |
| 449 |
configuration_parameters.align_frames_mode = self.align_frames_mode |
| 450 |
configuration_parameters.align_frames_automation = self.align_frames_automation |
| 451 |
configuration_parameters.align_frames_rectangle_scale_factor = \ |
| 452 |
self.align_frames_rectangle_scale_factor |
| 453 |
configuration_parameters.align_frames_search_width = self.align_frames_search_width |
| 454 |
configuration_parameters.align_frames_average_frame_percent = \ |
| 455 |
self.align_frames_average_frame_percent |
| 456 |
|
| 457 |
configuration_parameters.alignment_points_half_box_width = \ |
| 458 |
self.alignment_points_half_box_width |
| 459 |
configuration_parameters.alignment_points_search_width = self.alignment_points_search_width |
| 460 |
configuration_parameters.alignment_points_structure_threshold = \ |
| 461 |
self.alignment_points_structure_threshold |
| 462 |
configuration_parameters.alignment_points_brightness_threshold = \ |
| 463 |
self.alignment_points_brightness_threshold |
| 464 |
configuration_parameters.alignment_points_frame_percent = \ |
| 465 |
self.alignment_points_frame_percent |
| 466 |
configuration_parameters.alignment_points_frame_number = \ |
| 467 |
self.alignment_points_frame_number |
| 468 |
configuration_parameters.stack_frames_drizzle_factor_string = \ |
| 469 |
self.stack_frames_drizzle_factor_string |
| 470 |
|
| 471 |
def get_all_parameters_from_configparser(self, conf): |
| 472 |
""" |
| 473 |
All parameters which can be modified by the user are stored in the ConfigParser object. |
| 474 |
This way they can be written to a file or read from there. Read parameter values from a |
| 475 |
ConfigParser object and store them with the configuration object. |
| 476 |
|
| 477 |
:param conf: ConfigParser object |
| 478 |
:return: - |
| 479 |
""" |
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
default_conf_obj = ConfigurationParameters() |
| 484 |
default_conf_obj.set_defaults() |
| 485 |
|
| 486 |
|
| 487 |
self.global_parameters_version_imported_from = get_from_conf(conf, 'Global parameters', |
| 488 |
'version', ' version number could not be identified') |
| 489 |
|
| 490 |
self.hidden_parameters_current_dir = get_from_conf(conf, 'Hidden parameters', |
| 491 |
'current directory', default_conf_obj.hidden_parameters_current_dir) |
| 492 |
self.hidden_parameters_main_window_x0 = get_from_conf(conf, 'Hidden parameters', |
| 493 |
'main window x0', default_conf_obj.hidden_parameters_main_window_x0) |
| 494 |
self.hidden_parameters_main_window_y0 = get_from_conf(conf, 'Hidden parameters', |
| 495 |
'main window y0', default_conf_obj.hidden_parameters_main_window_y0) |
| 496 |
self.hidden_parameters_main_window_width = get_from_conf(conf, 'Hidden parameters', |
| 497 |
'main window width', default_conf_obj.hidden_parameters_main_window_width) |
| 498 |
self.hidden_parameters_main_window_height = get_from_conf(conf, 'Hidden parameters', |
| 499 |
'main window height', default_conf_obj.hidden_parameters_main_window_height) |
| 500 |
self.hidden_parameters_main_window_maximized = get_from_conf(conf, 'Hidden parameters', |
| 501 |
'main window maximized', default_conf_obj.hidden_parameters_main_window_maximized) |
| 502 |
|
| 503 |
self.global_parameters_display_quickstart = get_from_conf(conf, 'Global parameters', |
| 504 |
'display quickstart', default_conf_obj.global_parameters_display_quickstart) |
| 505 |
self.global_parameters_protocol_level = get_from_conf(conf, 'Global parameters', |
| 506 |
'protocol level', default_conf_obj.global_parameters_protocol_level) |
| 507 |
self.global_parameters_write_protocol_to_file = get_from_conf(conf, 'Global parameters', |
| 508 |
'write protocol to file', default_conf_obj.global_parameters_write_protocol_to_file) |
| 509 |
self.global_parameters_store_protocol_with_result = get_from_conf(conf, 'Global parameters', |
| 510 |
'store protocol with result', default_conf_obj.global_parameters_store_protocol_with_result) |
| 511 |
self.global_parameters_buffering_level = get_from_conf(conf, 'Global parameters', |
| 512 |
'buffering level', default_conf_obj.global_parameters_buffering_level) |
| 513 |
self.global_parameters_maximum_memory_active = get_from_conf(conf, 'Global parameters', |
| 514 |
'maximum memory active', default_conf_obj.global_parameters_maximum_memory_active) |
| 515 |
self.global_parameters_maximum_memory_amount = get_from_conf(conf, 'Global parameters', |
| 516 |
'maximum memory amount', default_conf_obj.global_parameters_maximum_memory_amount) |
| 517 |
self.global_parameters_include_postprocessing = get_from_conf(conf, 'Global parameters', |
| 518 |
'include postprocessing', default_conf_obj.global_parameters_include_postprocessing) |
| 519 |
self.global_parameters_image_format = get_from_conf(conf, 'Global parameters', |
| 520 |
'image format', default_conf_obj.global_parameters_image_format) |
| 521 |
self.global_parameters_parameters_in_filename = get_from_conf(conf, 'Global parameters', |
| 522 |
'parameters in filename', default_conf_obj.global_parameters_parameters_in_filename) |
| 523 |
self.global_parameters_stack_number_frames = get_from_conf(conf, 'Global parameters', |
| 524 |
'stack number frames', default_conf_obj.global_parameters_stack_number_frames) |
| 525 |
self.global_parameters_stack_percent_frames = get_from_conf(conf, 'Global parameters', |
| 526 |
'stack percent frames', default_conf_obj.global_parameters_stack_percent_frames) |
| 527 |
self.global_parameters_ap_box_size = get_from_conf(conf, 'Global parameters', |
| 528 |
'ap box size', default_conf_obj.global_parameters_ap_box_size) |
| 529 |
self.global_parameters_ap_number = get_from_conf(conf, 'Global parameters', |
| 530 |
'ap number', default_conf_obj.global_parameters_ap_number) |
| 531 |
|
| 532 |
self.frames_gauss_width = get_from_conf(conf, 'Frames', |
| 533 |
'gauss width', default_conf_obj.frames_gauss_width) |
| 534 |
self.frames_debayering_default = get_from_conf(conf, 'Frames', |
| 535 |
'debayering default', default_conf_obj.frames_debayering_default) |
| 536 |
self.frames_debayering_method = get_from_conf(conf, 'Frames', |
| 537 |
'debayering method', default_conf_obj.frames_debayering_method) |
| 538 |
self.frames_normalization = get_from_conf(conf, 'Frames', |
| 539 |
'normalization', default_conf_obj.frames_normalization) |
| 540 |
self.frames_normalization_threshold = get_from_conf(conf, 'Frames', |
| 541 |
'normalization threshold', default_conf_obj.frames_normalization_threshold) |
| 542 |
self.frames_add_selection_dialog = get_from_conf(conf, 'Frames', |
| 543 |
'add selection dialog', default_conf_obj.frames_add_selection_dialog) |
| 544 |
|
| 545 |
self.align_frames_fast_changing_object = get_from_conf(conf, 'Align frames', |
| 546 |
'fast changing object', default_conf_obj.align_frames_fast_changing_object) |
| 547 |
self.align_frames_mode = get_from_conf(conf, 'Align frames', |
| 548 |
'mode', default_conf_obj.align_frames_mode) |
| 549 |
self.align_frames_automation = get_from_conf(conf, 'Align frames', |
| 550 |
'automation', default_conf_obj.align_frames_automation) |
| 551 |
self.align_frames_rectangle_scale_factor = get_from_conf(conf, 'Align frames', |
| 552 |
'rectangle scale factor', default_conf_obj.align_frames_rectangle_scale_factor) |
| 553 |
self.align_frames_search_width = get_from_conf(conf, 'Align frames', |
| 554 |
'search width', default_conf_obj.align_frames_search_width) |
| 555 |
self.align_frames_average_frame_percent = get_from_conf(conf, 'Align frames', |
| 556 |
'average frame percent', default_conf_obj.align_frames_average_frame_percent) |
| 557 |
|
| 558 |
self.alignment_points_half_box_width = get_from_conf(conf, 'Alignment points', |
| 559 |
'half box width', default_conf_obj.alignment_points_half_box_width) |
| 560 |
self.alignment_points_search_width = get_from_conf(conf, 'Alignment points', |
| 561 |
'search width', default_conf_obj.alignment_points_search_width) |
| 562 |
self.alignment_points_structure_threshold = get_from_conf(conf, 'Alignment points', |
| 563 |
'structure threshold', default_conf_obj.alignment_points_structure_threshold) |
| 564 |
self.alignment_points_brightness_threshold = get_from_conf(conf, 'Alignment points', |
| 565 |
'brightness threshold', default_conf_obj.alignment_points_brightness_threshold) |
| 566 |
self.alignment_points_frame_percent = get_from_conf(conf, 'Alignment points', |
| 567 |
'frame percent', default_conf_obj.alignment_points_frame_percent) |
| 568 |
self.alignment_points_frame_number = get_from_conf(conf, 'Alignment points', |
| 569 |
'frame number', default_conf_obj.alignment_points_frame_number) |
| 570 |
|
| 571 |
self.stack_frames_drizzle_factor_string = get_from_conf(conf, 'Stack frames', |
| 572 |
'drizzle factor string', default_conf_obj.stack_frames_drizzle_factor_string) |
| 573 |
|
| 574 |
def store_all_parameters_to_config_parser(self): |
| 575 |
""" |
| 576 |
Write all variable parameters from the current configuration into a ConfigParser object. |
| 577 |
|
| 578 |
:return: ConfigParser object with all parameters |
| 579 |
""" |
| 580 |
|
| 581 |
|
| 582 |
sections = self.config_parser_object.sections() |
| 583 |
for section in sections: |
| 584 |
self.config_parser_object.remove_section(section) |
| 585 |
|
| 586 |
|
| 587 |
self.config_parser_object.add_section('Hidden parameters') |
| 588 |
self.set_parameter('Hidden parameters', 'current directory', |
| 589 |
self.hidden_parameters_current_dir) |
| 590 |
self.set_parameter('Hidden parameters', 'main window x0', |
| 591 |
str(self.hidden_parameters_main_window_x0)) |
| 592 |
self.set_parameter('Hidden parameters', 'main window y0', |
| 593 |
str(self.hidden_parameters_main_window_y0)) |
| 594 |
self.set_parameter('Hidden parameters', 'main window width', |
| 595 |
str(self.hidden_parameters_main_window_width)) |
| 596 |
self.set_parameter('Hidden parameters', 'main window height', |
| 597 |
str(self.hidden_parameters_main_window_height)) |
| 598 |
self.set_parameter('Hidden parameters', 'main window maximized', |
| 599 |
str(self.hidden_parameters_main_window_maximized)) |
| 600 |
self.config_parser_object.add_section('Global parameters') |
| 601 |
self.set_parameter('Global parameters', 'display quickstart', |
| 602 |
str(self.global_parameters_display_quickstart)) |
| 603 |
self.set_parameter('Global parameters', 'version', self.global_parameters_version) |
| 604 |
self.set_parameter('Global parameters', 'protocol level', |
| 605 |
str(self.global_parameters_protocol_level)) |
| 606 |
self.set_parameter('Global parameters', 'write protocol to file', |
| 607 |
str(self.global_parameters_write_protocol_to_file)) |
| 608 |
self.set_parameter('Global parameters', 'store protocol with result', |
| 609 |
str(self.global_parameters_store_protocol_with_result)) |
| 610 |
self.set_parameter('Global parameters', 'buffering level', |
| 611 |
str(self.global_parameters_buffering_level)) |
| 612 |
self.set_parameter('Global parameters', 'maximum memory active', |
| 613 |
str(self.global_parameters_maximum_memory_active)) |
| 614 |
self.set_parameter('Global parameters', 'maximum memory amount', |
| 615 |
str(self.global_parameters_maximum_memory_amount)) |
| 616 |
self.set_parameter('Global parameters', 'include postprocessing', |
| 617 |
str(self.global_parameters_include_postprocessing)) |
| 618 |
self.set_parameter('Global parameters', 'image format', |
| 619 |
self.global_parameters_image_format) |
| 620 |
self.set_parameter('Global parameters', 'parameters in filename', |
| 621 |
str(self.global_parameters_parameters_in_filename)) |
| 622 |
self.set_parameter('Global parameters', 'stack number frames', |
| 623 |
str(self.global_parameters_stack_number_frames)) |
| 624 |
self.set_parameter('Global parameters', 'stack percent frames', |
| 625 |
str(self.global_parameters_stack_percent_frames)) |
| 626 |
self.set_parameter('Global parameters', 'ap box size', |
| 627 |
str(self.global_parameters_ap_box_size)) |
| 628 |
self.set_parameter('Global parameters', 'ap number', |
| 629 |
str(self.global_parameters_ap_number)) |
| 630 |
|
| 631 |
self.config_parser_object.add_section('Frames') |
| 632 |
self.set_parameter('Frames', 'gauss width', str(self.frames_gauss_width)) |
| 633 |
self.set_parameter('Frames', 'debayering default', self.frames_debayering_default) |
| 634 |
self.set_parameter('Frames', 'debayering method', self.frames_debayering_method) |
| 635 |
self.set_parameter('Frames', 'normalization', str(self.frames_normalization)) |
| 636 |
self.set_parameter('Frames', 'normalization threshold', |
| 637 |
str(self.frames_normalization_threshold)) |
| 638 |
self.set_parameter('Frames', 'add selection dialog', str(self.frames_add_selection_dialog)) |
| 639 |
|
| 640 |
self.config_parser_object.add_section('Align frames') |
| 641 |
self.set_parameter('Align frames', 'fast changing object', |
| 642 |
str(self.align_frames_fast_changing_object)) |
| 643 |
self.set_parameter('Align frames', 'mode', self.align_frames_mode) |
| 644 |
self.set_parameter('Align frames', 'automation', str(self.align_frames_automation)) |
| 645 |
self.set_parameter('Align frames', 'rectangle scale factor', |
| 646 |
str(self.align_frames_rectangle_scale_factor)) |
| 647 |
self.set_parameter('Align frames', 'search width', |
| 648 |
str(self.align_frames_search_width)) |
| 649 |
self.set_parameter('Align frames', 'average frame percent', |
| 650 |
str(self.align_frames_average_frame_percent)) |
| 651 |
|
| 652 |
self.config_parser_object.add_section('Alignment points') |
| 653 |
self.set_parameter('Alignment points', 'half box width', |
| 654 |
str(self.alignment_points_half_box_width)) |
| 655 |
self.set_parameter('Alignment points', 'search width', |
| 656 |
str(self.alignment_points_search_width)) |
| 657 |
self.set_parameter('Alignment points', 'structure threshold', |
| 658 |
str(self.alignment_points_structure_threshold)) |
| 659 |
self.set_parameter('Alignment points', 'brightness threshold', |
| 660 |
str(self.alignment_points_brightness_threshold)) |
| 661 |
self.set_parameter('Alignment points', 'frame percent', |
| 662 |
str(self.alignment_points_frame_percent)) |
| 663 |
self.set_parameter('Alignment points', 'frame number', |
| 664 |
str(self.alignment_points_frame_number)) |
| 665 |
|
| 666 |
self.config_parser_object.add_section('Stack frames') |
| 667 |
self.set_parameter('Stack frames', 'drizzle factor string', |
| 668 |
self.stack_frames_drizzle_factor_string) |
| 669 |
|
| 670 |
def set_parameter(self, section, name, value): |
| 671 |
""" |
| 672 |
Assign a new value to a parameter in the configuration object. The value is not checked for |
| 673 |
validity. Therefore, this method should be used with well-defined values internally only. |
| 674 |
|
| 675 |
:param section: section name (e.g. 'Global parameters') within the JSON data object |
| 676 |
:param name: name of the parameter (e.g. 'protocol level') |
| 677 |
:param value: new value to be assigned to the parameter (type str) |
| 678 |
:return: True, if the parameter was assigned successfully. False, otherwise. |
| 679 |
""" |
| 680 |
|
| 681 |
try: |
| 682 |
self.config_parser_object.set(section, name, value) |
| 683 |
return True |
| 684 |
except: |
| 685 |
return False |
| 686 |
|
| 687 |
def set_derived_parameters(self): |
| 688 |
""" |
| 689 |
Set parameters which are computed from other parameters. |
| 690 |
|
| 691 |
:return: - |
| 692 |
""" |
| 693 |
|
| 694 |
|
| 695 |
self.alignment_points_half_patch_width = int( |
| 696 |
round((self.alignment_points_half_box_width * 3) / 2)) |
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
self.alignment_points_step_size = int( |
| 701 |
round((self.alignment_points_half_patch_width * 4.5) / 3)) |
| 702 |
|
| 703 |
|
| 704 |
if self.stack_frames_drizzle_factor_string == "Off": |
| 705 |
self.drizzle_factor = 1 |
| 706 |
self.drizzle_factor_is_1_5 = False |
| 707 |
elif self.stack_frames_drizzle_factor_string == "1.5x": |
| 708 |
self.drizzle_factor = 3 |
| 709 |
self.drizzle_factor_is_1_5 = True |
| 710 |
elif self.stack_frames_drizzle_factor_string == "2x": |
| 711 |
self.drizzle_factor = 2 |
| 712 |
self.drizzle_factor_is_1_5 = False |
| 713 |
elif self.stack_frames_drizzle_factor_string == "3x": |
| 714 |
self.drizzle_factor = 3 |
| 715 |
self.drizzle_factor_is_1_5 = False |
| 716 |
|
| 717 |
def write_config(self, file_name=None): |
| 718 |
""" |
| 719 |
Write all variable configuration parameters to a file. If no file name is specified, take |
| 720 |
the standard ".ini" file |
| 721 |
|
| 722 |
:param file_name: Optional configuration file name. |
| 723 |
:return: - |
| 724 |
""" |
| 725 |
|
| 726 |
|
| 727 |
self.store_all_parameters_to_config_parser() |
| 728 |
|
| 729 |
|
| 730 |
self.postproc_data_object.dump_config(self.config_parser_object) |
| 731 |
|
| 732 |
if not file_name: |
| 733 |
file_name = self.config_filename |
| 734 |
|
| 735 |
with open(file_name, 'w') as config_file: |
| 736 |
self.config_parser_object.write(config_file) |
| 737 |
|
| 738 |
def read_config(self, file_name=None): |
| 739 |
""" |
| 740 |
Read the configuration from a file. If no name is given, the config is read from the |
| 741 |
standard ".ini" file in the user's home directory. |
| 742 |
|
| 743 |
:param file_name: Optional configuration file name |
| 744 |
:return: ConfigParser object with configuration parameters |
| 745 |
""" |
| 746 |
|
| 747 |
|
| 748 |
self.config_parser_object = ConfigParser() |
| 749 |
|
| 750 |
if not file_name: |
| 751 |
file_name = self.config_filename |
| 752 |
self.config_parser_object.read(file_name) |
| 753 |
|
| 754 |
|
| 755 |
self.get_all_parameters_from_configparser(self.config_parser_object) |
| 756 |
|
| 757 |
|
| 758 |
self.postproc_data_object.load_config(self.config_parser_object) |
| 759 |
|
| 760 |
return self.config_parser_object |
| 761 |
|
| 762 |
def get_from_conf(config_parser_object, section, name, default): |
| 763 |
""" |
| 764 |
Try to read a parameter from the config parser object. If it is not found, a default parameter |
| 765 |
is set instead. |
| 766 |
|
| 767 |
:param config_parser_object: Config parser object with all input parameters. |
| 768 |
:param section: Section name in config parser object. |
| 769 |
:param name: Variable name in section. |
| 770 |
:param default: Default value if variable cannot be read. |
| 771 |
:return: Either the value read, or default. |
| 772 |
""" |
| 773 |
try: |
| 774 |
if isinstance(default, str): |
| 775 |
value = config_parser_object.get(section, name) |
| 776 |
elif isinstance(default, bool): |
| 777 |
value = config_parser_object.getboolean(section, name) |
| 778 |
elif isinstance(default, int): |
| 779 |
value = config_parser_object.getint(section, name) |
| 780 |
elif isinstance(default, float): |
| 781 |
value = config_parser_object.getfloat(section, name) |
| 782 |
else: |
| 783 |
raise ArgumentError("Parameter " + name + " in section " + section + " cannot be parsed") |
| 784 |
|
| 785 |
except: |
| 786 |
value = default |
| 787 |
|
| 788 |
return value |
| 789 |
|
| 790 |
class PostprocDataObject(object): |
| 791 |
""" |
| 792 |
This class implements the central data object used in postprocessing. |
| 793 |
|
| 794 |
""" |
| 795 |
|
| 796 |
def __init__(self, postproc_suffix): |
| 797 |
""" |
| 798 |
Initialize the data object. |
| 799 |
|
| 800 |
:param postproc_suffix: File suffix used for postprocessing result. |
| 801 |
:return: - |
| 802 |
""" |
| 803 |
|
| 804 |
self.postproc_suffix = postproc_suffix |
| 805 |
|
| 806 |
|
| 807 |
self.initialize_versions() |
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
initial_version = self.add_postproc_version() |
| 812 |
initial_version.add_postproc_layer(PostprocLayer("Multilevel unsharp masking", 1., 1., 0., |
| 813 |
13, 0., False)) |
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
self.blinking = False |
| 819 |
self.version_compared = 0 |
| 820 |
|
| 821 |
def set_postproc_input_image(self, image_original, name_original, image_format): |
| 822 |
""" |
| 823 |
Set the input image and associated file name for postprocessing, and set derived variables. |
| 824 |
|
| 825 |
:param image_original: Image file (16bit Tiff) holding the input for postprocessing |
| 826 |
:param name_original: Path name of the original image. |
| 827 |
:param image_format: Image format, either 'tiff' or 'fits'. |
| 828 |
:return: - |
| 829 |
""" |
| 830 |
|
| 831 |
self.image_original = image_original |
| 832 |
self.color = len(self.image_original.shape) == 3 |
| 833 |
self.file_name_original = name_original |
| 834 |
|
| 835 |
|
| 836 |
self.file_name_processed = PostprocDataObject.set_file_name_processed( |
| 837 |
self.file_name_original, self.postproc_suffix, image_format) |
| 838 |
|
| 839 |
|
| 840 |
for version in self.versions: |
| 841 |
version.set_image(self.image_original) |
| 842 |
version.input_image_saved = None |
| 843 |
version.input_image_hsv_saved = None |
| 844 |
version.shift_red_saved = None |
| 845 |
version.shift_blue_saved = None |
| 846 |
version.last_rgb_automatic = None |
| 847 |
|
| 848 |
@staticmethod |
| 849 |
def set_file_name_processed(file_name_original, postproc_suffix, image_format): |
| 850 |
""" |
| 851 |
Derive the postprocessing output file name from the name of postprocessing input. |
| 852 |
|
| 853 |
:param file_name_original: Postprocessing input file name (e.g. result from stacking) |
| 854 |
:param postproc_suffix: Additional suffix to be inserted before file extension. |
| 855 |
:param image_format: Image format, either 'png', 'tiff' or 'fits'. |
| 856 |
:return: Name of postprocessing result. |
| 857 |
""" |
| 858 |
|
| 859 |
return splitext(file_name_original)[0] + postproc_suffix + '.' + image_format |
| 860 |
|
| 861 |
def initialize_versions(self): |
| 862 |
""" |
| 863 |
Initialize the postprocessing image versions with the unprocessed image (as version 0). |
| 864 |
|
| 865 |
:return: - |
| 866 |
""" |
| 867 |
|
| 868 |
original_version = PostprocVersion() |
| 869 |
self.versions = [original_version] |
| 870 |
self.number_versions = 0 |
| 871 |
self.version_selected = 0 |
| 872 |
|
| 873 |
def add_postproc_version(self): |
| 874 |
""" |
| 875 |
Add a new postprocessing version, and set the "selected" pointer to it. |
| 876 |
|
| 877 |
:return: The new version object |
| 878 |
""" |
| 879 |
|
| 880 |
new_version = PostprocVersion() |
| 881 |
self.versions.append(new_version) |
| 882 |
self.number_versions += 1 |
| 883 |
self.version_selected = self.number_versions |
| 884 |
return new_version |
| 885 |
|
| 886 |
def new_postproc_version_from_existing(self): |
| 887 |
""" |
| 888 |
Create a new postprocessing version by copying the currently selected one. Append the new |
| 889 |
version to the list of postprocessing versions. Set the current version pointer to the new |
| 890 |
version. |
| 891 |
:return: New postprocessing version. |
| 892 |
""" |
| 893 |
|
| 894 |
new_version = deepcopy(self.versions[self.version_selected]) |
| 895 |
self.versions.append(new_version) |
| 896 |
self.number_versions += 1 |
| 897 |
self.version_selected = self.number_versions |
| 898 |
return new_version |
| 899 |
|
| 900 |
def remove_postproc_version(self, index): |
| 901 |
""" |
| 902 |
Remove a postprocessing version, and set the "selected" pointer to the previous one. The |
| 903 |
initial version (input image) cannot be removed. |
| 904 |
|
| 905 |
:param index: Index of the version to be removed. |
| 906 |
:return: - |
| 907 |
""" |
| 908 |
|
| 909 |
if 0 < index <= self.number_versions: |
| 910 |
self.versions = self.versions[:index] + self.versions[index + 1:] |
| 911 |
self.number_versions -= 1 |
| 912 |
self.version_selected = index - 1 |
| 913 |
|
| 914 |
def finalize_postproc_version(self, version_index=None): |
| 915 |
""" |
| 916 |
If "correction mode" is on, the image stored with the selected version is in 8bit and |
| 917 |
potentially interpolated by the resolution factor. Before the image is saved, it has to be |
| 918 |
processed from the original image using the current shift and layer data. The 16bit image is |
| 919 |
stored with the current version object. |
| 920 |
|
| 921 |
:param version_index: If an index is specified, process the version with this index. |
| 922 |
Otherwise, process the version currently selected. |
| 923 |
:return: The processed 16bit image (also stored with the version). |
| 924 |
""" |
| 925 |
|
| 926 |
if version_index is None: |
| 927 |
|
| 928 |
version = self.versions[self.version_selected] |
| 929 |
else: |
| 930 |
version = self.versions[version_index] |
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
if version.image is not None and version.image.dtype == uint8: |
| 936 |
|
| 937 |
|
| 938 |
shift_red = (version.shift_red[0] + version.correction_red[0], |
| 939 |
version.shift_red[1] + version.correction_red[1]) |
| 940 |
shift_blue = (version.shift_blue[0] + version.correction_blue[0], |
| 941 |
version.shift_blue[1] + version.correction_blue[1]) |
| 942 |
|
| 943 |
|
| 944 |
interpolation_factor = [1, 2, 4][version.rgb_resolution_index] |
| 945 |
shifted_image = Miscellaneous.shift_colors(self.image_original, |
| 946 |
shift_red, shift_blue, |
| 947 |
interpolate_input=interpolation_factor, |
| 948 |
reduce_output=interpolation_factor) |
| 949 |
|
| 950 |
|
| 951 |
version.image = Miscellaneous.post_process(shifted_image, version.layers) |
| 952 |
|
| 953 |
return version.image |
| 954 |
|
| 955 |
def dump_config(self, config_parser_object): |
| 956 |
""" |
| 957 |
Dump all version and layer parameters into a ConfigParser object (except for image data). |
| 958 |
Version 0 is not included because it does not contain any layer info. |
| 959 |
|
| 960 |
:param config_parser_object: ConfigParser object. |
| 961 |
:return: - |
| 962 |
""" |
| 963 |
|
| 964 |
|
| 965 |
for section in config_parser_object.sections(): |
| 966 |
section_items = section.split() |
| 967 |
if section_items[0] == 'PostprocessingVersion' or \ |
| 968 |
section_items[0] == 'PostprocessingInfo': |
| 969 |
config_parser_object.remove_section(section) |
| 970 |
|
| 971 |
|
| 972 |
config_parser_object.add_section('PostprocessingInfo') |
| 973 |
config_parser_object.set('PostprocessingInfo', 'version selected', |
| 974 |
str(self.version_selected)) |
| 975 |
|
| 976 |
|
| 977 |
for version_index, version in enumerate(self.versions): |
| 978 |
section_name = "PostprocessingVersion " + str(version_index) + " RGBAlignment" |
| 979 |
config_parser_object.add_section(section_name) |
| 980 |
|
| 981 |
config_parser_object.set(section_name, 'rgb automatic', str(version.rgb_automatic)) |
| 982 |
config_parser_object.set(section_name, 'rgb gauss width', str(version.rgb_gauss_width)) |
| 983 |
config_parser_object.set(section_name, 'rgb resolution index', |
| 984 |
str(version.rgb_resolution_index)) |
| 985 |
config_parser_object.set(section_name, 'rgb shift red y', |
| 986 |
str(version.shift_red[0])) |
| 987 |
config_parser_object.set(section_name, 'rgb shift red x', |
| 988 |
str(version.shift_red[1])) |
| 989 |
config_parser_object.set(section_name, 'rgb shift blue y', |
| 990 |
str(version.shift_blue[0])) |
| 991 |
config_parser_object.set(section_name, 'rgb shift blue x', |
| 992 |
str(version.shift_blue[1])) |
| 993 |
|
| 994 |
|
| 995 |
for layer_index, layer in enumerate(version.layers): |
| 996 |
section_name = "PostprocessingVersion " + str(version_index) + " Layer " + str( |
| 997 |
layer_index) |
| 998 |
config_parser_object.add_section(section_name) |
| 999 |
|
| 1000 |
|
| 1001 |
config_parser_object.set(section_name, 'postprocessing method', layer.postproc_method) |
| 1002 |
config_parser_object.set(section_name, 'radius', str(layer.radius)) |
| 1003 |
config_parser_object.set(section_name, 'amount', str(layer.amount)) |
| 1004 |
config_parser_object.set(section_name, 'bilateral fraction', str(layer.bi_fraction)) |
| 1005 |
config_parser_object.set(section_name, 'bilateral range', str(layer.bi_range)) |
| 1006 |
config_parser_object.set(section_name, 'denoise', str(layer.denoise)) |
| 1007 |
config_parser_object.set(section_name, 'luminance only', str(layer.luminance_only)) |
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
def load_config(self, config_parser_object): |
| 1012 |
""" |
| 1013 |
Load all postprocessing configuration data from a ConfigParser object. The data replace |
| 1014 |
all versions (apart from version 0) and all associated layer info. The image data is taken |
| 1015 |
from the current data object and is not restored. |
| 1016 |
|
| 1017 |
:param config_parser_object: ConfigParser object. |
| 1018 |
:return: - |
| 1019 |
""" |
| 1020 |
|
| 1021 |
standard_version = PostprocVersion() |
| 1022 |
standard_layer = PostprocLayer("Multilevel unsharp masking", 1., 1., 0., 20, 0., False) |
| 1023 |
|
| 1024 |
|
| 1025 |
self.initialize_versions() |
| 1026 |
|
| 1027 |
|
| 1028 |
try: |
| 1029 |
self.version_selected = config_parser_object.getint('PostprocessingInfo', |
| 1030 |
'version selected') |
| 1031 |
except: |
| 1032 |
|
| 1033 |
|
| 1034 |
return |
| 1035 |
|
| 1036 |
|
| 1037 |
self.number_versions = 0 |
| 1038 |
|
| 1039 |
|
| 1040 |
for section in config_parser_object.sections(): |
| 1041 |
section_items = section.split() |
| 1042 |
if section_items[0] == 'PostprocessingVersion': |
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
if section_items[2] == 'RGBAlignment': |
| 1049 |
version_index = int(section_items[1]) |
| 1050 |
if version_index: |
| 1051 |
new_version = self.add_postproc_version() |
| 1052 |
else: |
| 1053 |
self.initialize_versions() |
| 1054 |
new_version = self.versions[0] |
| 1055 |
new_version.rgb_automatic = get_from_conf(config_parser_object, section, |
| 1056 |
'rgb automatic', standard_version.rgb_automatic) |
| 1057 |
new_version.rgb_gauss_width = get_from_conf(config_parser_object, section, |
| 1058 |
'rgb gauss width', standard_version.rgb_gauss_width) |
| 1059 |
new_version.rgb_resolution_index = get_from_conf(config_parser_object, section, |
| 1060 |
'rgb resolution index', standard_version.rgb_resolution_index) |
| 1061 |
new_version.shift_red = (get_from_conf(config_parser_object, section, |
| 1062 |
'rgb shift red y', standard_version.shift_red[0]), |
| 1063 |
get_from_conf(config_parser_object, section, 'rgb shift red x', |
| 1064 |
standard_version.shift_red[1])) |
| 1065 |
new_version.shift_blue = (get_from_conf(config_parser_object, section, |
| 1066 |
'rgb shift blue y', standard_version.shift_blue[0]), |
| 1067 |
get_from_conf(config_parser_object, section, 'rgb shift blue x', |
| 1068 |
standard_version.shift_blue[1])) |
| 1069 |
|
| 1070 |
|
| 1071 |
elif section_items[2] == 'Layer': |
| 1072 |
|
| 1073 |
method = get_from_conf(config_parser_object, section, |
| 1074 |
'postprocessing method', standard_layer.postproc_method) |
| 1075 |
radius = get_from_conf(config_parser_object, section, |
| 1076 |
'radius', standard_layer.radius) |
| 1077 |
amount = get_from_conf(config_parser_object, section, |
| 1078 |
'amount', standard_layer.amount) |
| 1079 |
bi_fraction = get_from_conf(config_parser_object, section, |
| 1080 |
'bilateral fraction', standard_layer.bi_fraction) |
| 1081 |
bi_range = get_from_conf(config_parser_object, section, |
| 1082 |
'bilateral range', standard_layer.bi_range) |
| 1083 |
denoise = get_from_conf(config_parser_object, section, |
| 1084 |
'denoise', standard_layer.denoise) |
| 1085 |
luminance_only = get_from_conf(config_parser_object, section, |
| 1086 |
'luminance only', standard_layer.luminance_only) |
| 1087 |
new_version.add_postproc_layer(PostprocLayer(method, radius, amount, bi_fraction, |
| 1088 |
bi_range, denoise, luminance_only)) |
| 1089 |
|
| 1090 |
|
| 1091 |
self.version_selected = min(config_parser_object.getint('PostprocessingInfo', |
| 1092 |
'version selected'), self.number_versions) |
| 1093 |
|
| 1094 |
|
| 1095 |
class PostprocVersion(object): |
| 1096 |
""" |
| 1097 |
Instances of this class hold the data defining a single postprocessing version, including the |
| 1098 |
resulting image for the current parameter set. |
| 1099 |
""" |
| 1100 |
|
| 1101 |
def __init__(self): |
| 1102 |
""" |
| 1103 |
Initialize the version object with the input image and an empty set of processing layers. |
| 1104 |
:param image: Input image (16bit Tiff) for postprocessing |
| 1105 |
""" |
| 1106 |
|
| 1107 |
self.postproc_method = "Multilevel unsharp masking" |
| 1108 |
self.layers = [] |
| 1109 |
self.number_layers = 0 |
| 1110 |
self.rgb_automatic = False |
| 1111 |
self.last_rgb_automatic = None |
| 1112 |
self.rgb_gauss_width = 7 |
| 1113 |
self.rgb_resolution_index = 1 |
| 1114 |
self.shift_red = (0., 0.) |
| 1115 |
self.shift_blue = (0., 0.) |
| 1116 |
self.shift_red_saved = None |
| 1117 |
self.shift_blue_saved = None |
| 1118 |
self.rgb_correction_mode = False |
| 1119 |
self.correction_red = (0., 0.) |
| 1120 |
self.correction_blue = (0., 0.) |
| 1121 |
self.correction_red_saved = (0., 0.) |
| 1122 |
self.correction_blue_saved = (0., 0.) |
| 1123 |
self.image = None |
| 1124 |
self.images_uncorrected = [None] * 3 |
| 1125 |
self.input_image_saved = None |
| 1126 |
self.input_image_hsv_saved = None |
| 1127 |
|
| 1128 |
def set_image(self, image): |
| 1129 |
""" |
| 1130 |
Set the current image for this version. |
| 1131 |
|
| 1132 |
:param image: Image file (16bit Tiff) holding the pixel data of this version's image. |
| 1133 |
:return: - |
| 1134 |
""" |
| 1135 |
|
| 1136 |
self.image = image |
| 1137 |
|
| 1138 |
def add_postproc_layer(self, layer): |
| 1139 |
""" |
| 1140 |
Add a postprocessing layer. |
| 1141 |
:param layer: Layer instance to be added to the list of layers. |
| 1142 |
:return: - |
| 1143 |
""" |
| 1144 |
|
| 1145 |
self.layers.append(layer) |
| 1146 |
self.number_layers += 1 |
| 1147 |
|
| 1148 |
def remove_postproc_layer(self, layer_index): |
| 1149 |
""" |
| 1150 |
Remove a postprocessing layer from this version. If there is only one layer, do not delete |
| 1151 |
it, but reset it to standard values instead. This makes sure a version always has at least |
| 1152 |
one layer. |
| 1153 |
|
| 1154 |
:param layer_index: Index of the layer to be removed. |
| 1155 |
:return: - |
| 1156 |
""" |
| 1157 |
|
| 1158 |
if self.number_layers == 1: |
| 1159 |
self.layers = [PostprocLayer("Multilevel unsharp masking", 1., 1., 0., 20, 0., False)] |
| 1160 |
else: |
| 1161 |
if 0 <= layer_index < self.number_layers: |
| 1162 |
self.layers = self.layers[:layer_index] + self.layers[layer_index + 1:] |
| 1163 |
self.number_layers -= 1 |
| 1164 |
|
| 1165 |
|
| 1166 |
class PostprocLayer(object): |
| 1167 |
""" |
| 1168 |
Instances of this class hold the parameters which define a postprocessing layer. |
| 1169 |
""" |
| 1170 |
|
| 1171 |
def __init__(self, method, radius, amount, bi_fraction, bi_range, denoise, luminance_only): |
| 1172 |
""" |
| 1173 |
Initialize the Layer instance with values for Gaussian radius, amount of sharpening and a |
| 1174 |
flag which indicates on which channel the sharpening is to be applied. |
| 1175 |
|
| 1176 |
:param method: Description of the sharpening method. |
| 1177 |
:param radius: Radius (in pixels) of the Gaussian sharpening kernel. |
| 1178 |
:param amount: Amount of sharpening for this layer. |
| 1179 |
:param bi_fraction: Fraction of bilateral vs. Gaussian filter |
| 1180 |
(0.: only Gaussian, 1.: only bilateral). |
| 1181 |
:param bi_range: luminosity range parameter of bilateral filter (0 <= bi_range <= 255). |
| 1182 |
Please note that for 16bit images the true range is 256 times as high. |
| 1183 |
:param denoise: Fraction of Gaussian blur to be applied to this layer |
| 1184 |
(0.: No Gaussian blur, 1.: Full filter application). |
| 1185 |
:param luminance_only: True, if sharpening is to be applied to the luminance channel only. |
| 1186 |
False, otherwise. |
| 1187 |
""" |
| 1188 |
|
| 1189 |
self.postproc_method = method |
| 1190 |
self.radius = radius |
| 1191 |
self.amount = amount |
| 1192 |
self.bi_fraction = bi_fraction |
| 1193 |
self.bi_range = bi_range |
| 1194 |
self.denoise = denoise |
| 1195 |
self.luminance_only = luminance_only |