| 1 |
|
| 2 |
""" |
| 3 |
Copyright (c) 2018 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 |
from glob import glob |
| 24 |
from math import ceil |
| 25 |
from statistics import median |
| 26 |
from time import sleep |
| 27 |
from warnings import filterwarnings |
| 28 |
|
| 29 |
import matplotlib |
| 30 |
matplotlib.use('Agg') |
| 31 |
import matplotlib.pyplot as plt |
| 32 |
from cv2 import FONT_HERSHEY_SIMPLEX, putText, resize, INTER_CUBIC, INTER_LINEAR |
| 33 |
from numpy import zeros, full, empty, float32, newaxis, arange, count_nonzero, \ |
| 34 |
sqrt, uint16, clip, minimum, mean |
| 35 |
from skimage import img_as_uint, img_as_ubyte |
| 36 |
|
| 37 |
from align_frames import AlignFrames |
| 38 |
from alignment_points import AlignmentPoints |
| 39 |
from configuration import Configuration |
| 40 |
from exceptions import InternalError, NotSupportedError, Error |
| 41 |
from frames import Frames |
| 42 |
from miscellaneous import Miscellaneous |
| 43 |
from rank_frames import RankFrames |
| 44 |
from timer import timer |
| 45 |
|
| 46 |
|
| 47 |
class StackFrames(object): |
| 48 |
""" |
| 49 |
For every frame de-warp the quality areas selected for stacking. Then stack all the |
| 50 |
de-warped frame sections into a single image. |
| 51 |
|
| 52 |
""" |
| 53 |
|
| 54 |
def __init__(self, configuration, frames, rank_frames, align_frames, alignment_points, my_timer, |
| 55 |
progress_signal=None, debug=False, create_image_window_signal=None, |
| 56 |
update_image_window_signal=None, terminate_image_window_signal=None): |
| 57 |
""" |
| 58 |
Initialze the StackFrames object. In particular, allocate empty numpy arrays used in the |
| 59 |
stacking process for buffering and the final stacked image. The size of all those objects |
| 60 |
in y and x directions is equal to the intersection of all frames. |
| 61 |
|
| 62 |
:param configuration: Configuration object with parameters |
| 63 |
:param frames: Frames object with all video frames |
| 64 |
:param rank_frames: RankFrames object with global quality ranks (between 0. and 1., |
| 65 |
1. being optimal) for all frames |
| 66 |
:param align_frames: AlignFrames object with global shift information for all frames |
| 67 |
:param alignment_points: AlignmentPoints object with information of all alignment points |
| 68 |
:param my_timer: Timer object for accumulating times spent in specific code sections |
| 69 |
:param progress_signal: Either None (no progress signalling), or a signal with the signature |
| 70 |
(str, int) with the current activity (str) and the progress in |
| 71 |
percent (int). |
| 72 |
""" |
| 73 |
|
| 74 |
|
| 75 |
filterwarnings("ignore", category=UserWarning) |
| 76 |
|
| 77 |
self.configuration = configuration |
| 78 |
self.frames = frames |
| 79 |
self.rank_frames = rank_frames |
| 80 |
self.align_frames = align_frames |
| 81 |
self.alignment_points = alignment_points |
| 82 |
self.my_timer = my_timer |
| 83 |
self.progress_signal = progress_signal |
| 84 |
self.signal_step_size = max(int(self.frames.number / 10), 1) |
| 85 |
self.shift_distribution = None |
| 86 |
|
| 87 |
|
| 88 |
self.drizzle = self.configuration.drizzle_factor != 1 |
| 89 |
|
| 90 |
for name in ['Stacking: AP initialization', 'Stacking: initialize background blending', |
| 91 |
'Stacking: compute AP shifts', 'Stacking: remapping and adding', |
| 92 |
'Stacking: computing background', 'Stacking: merging AP buffers']: |
| 93 |
self.my_timer.create_no_check(name) |
| 94 |
|
| 95 |
self.my_timer.start('Stacking: AP initialization') |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
self.dim_y = (self.align_frames.intersection_shape[0][1] - |
| 100 |
self.align_frames.intersection_shape[0][0]) |
| 101 |
self.dim_y_drizzled = self.dim_y * self.configuration.drizzle_factor |
| 102 |
self.dim_x = (self.align_frames.intersection_shape[1][1] - |
| 103 |
self.align_frames.intersection_shape[1][0]) |
| 104 |
self.dim_x_drizzled = self.dim_x * self.configuration.drizzle_factor |
| 105 |
self.number_pixels = self.dim_y * self.dim_x |
| 106 |
self.number_pixels_drizzled = self.dim_y_drizzled * self.dim_x_drizzled |
| 107 |
|
| 108 |
|
| 109 |
for ap in self.alignment_points.alignment_points: |
| 110 |
AlignmentPoints.initialize_ap_stacking_buffer(ap, self.configuration.drizzle_factor, |
| 111 |
self.frames.color) |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
if self.frames.color: |
| 117 |
self.stacked_image_buffer = zeros([self.dim_y_drizzled, self.dim_x_drizzled, 3], |
| 118 |
dtype=float32) |
| 119 |
if self.drizzle: |
| 120 |
self.frame_drizzled = zeros([self.dim_y_drizzled, self.dim_x_drizzled, 3], |
| 121 |
dtype=float32) |
| 122 |
else: |
| 123 |
self.stacked_image_buffer = zeros([self.dim_y_drizzled, self.dim_x_drizzled], |
| 124 |
dtype=float32) |
| 125 |
if self.drizzle: |
| 126 |
self.frame_drizzled = zeros([self.dim_y_drizzled, self.dim_x_drizzled], |
| 127 |
dtype=float32) |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
self.background_patches = None |
| 132 |
self.averaged_background = None |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
self.sum_single_frame_weights = full([self.dim_y_drizzled, self.dim_x_drizzled], 1.e-30, |
| 138 |
dtype=float32) |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
self.debug = debug |
| 145 |
self.scale_factor = 3 |
| 146 |
self.border = 2 |
| 147 |
self.image_delay = 0.5 |
| 148 |
self.create_image_window_signal = create_image_window_signal |
| 149 |
self.update_image_window_signal = update_image_window_signal |
| 150 |
self.terminate_image_window_signal = terminate_image_window_signal |
| 151 |
|
| 152 |
self.my_timer.stop('Stacking: AP initialization') |
| 153 |
|
| 154 |
def prepare_for_stack_blending(self): |
| 155 |
""" |
| 156 |
Find image locations where the background image is needed for stacking. If the fraction of |
| 157 |
such pixels is above a given parameter, a full background image is constructed from the best |
| 158 |
frames during the stacking process. If the fraction is low, the image is subdivided into |
| 159 |
quadratic patches. To save computing time, the background image is constructed only in those |
| 160 |
patches which contain at least one pixel where the background is needed. |
| 161 |
|
| 162 |
:return: |
| 163 |
""" |
| 164 |
|
| 165 |
self.my_timer.start('Stacking: initialize background blending') |
| 166 |
|
| 167 |
|
| 168 |
single_stack_size_float = float(self.alignment_points.stack_size) |
| 169 |
|
| 170 |
|
| 171 |
for alignment_point in self.alignment_points.alignment_points: |
| 172 |
patch_y_low_drizzled = alignment_point['patch_y_low_drizzled'] |
| 173 |
patch_y_high_drizzled = alignment_point['patch_y_high_drizzled'] |
| 174 |
patch_x_low_drizzled = alignment_point['patch_x_low_drizzled'] |
| 175 |
patch_x_high_drizzled = alignment_point['patch_x_high_drizzled'] |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
extend_low_y = patch_y_low_drizzled == 0 |
| 181 |
extend_high_y = patch_y_high_drizzled == self.dim_y_drizzled |
| 182 |
extend_low_x = patch_x_low_drizzled == 0 |
| 183 |
extend_high_x = patch_x_high_drizzled == self.dim_x_drizzled |
| 184 |
|
| 185 |
|
| 186 |
alignment_point['weights_yx'] = minimum(self.one_dim_weight(patch_y_low_drizzled, |
| 187 |
patch_y_high_drizzled, |
| 188 |
alignment_point['y_drizzled'], |
| 189 |
extend_low=extend_low_y, |
| 190 |
extend_high=extend_high_y)[ |
| 191 |
:, newaxis], |
| 192 |
self.one_dim_weight(patch_x_low_drizzled, |
| 193 |
patch_x_high_drizzled, |
| 194 |
alignment_point['x_drizzled'], |
| 195 |
extend_low=extend_low_x, |
| 196 |
extend_high=extend_high_x)[ |
| 197 |
newaxis, :]) |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
self.sum_single_frame_weights[patch_y_low_drizzled:patch_y_high_drizzled, |
| 209 |
patch_x_low_drizzled: patch_x_high_drizzled] += single_stack_size_float \ |
| 210 |
* alignment_point['weights_yx'] |
| 211 |
|
| 212 |
|
| 213 |
self.number_stacking_holes = count_nonzero(self.sum_single_frame_weights < 1.e-10) |
| 214 |
|
| 215 |
|
| 216 |
if self.number_stacking_holes == 0: |
| 217 |
self.my_timer.stop('Stacking: initialize background blending') |
| 218 |
return |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
if self.frames.color: |
| 228 |
self.averaged_background = zeros([self.dim_y, self.dim_x, 3], dtype=float32) |
| 229 |
else: |
| 230 |
self.averaged_background = zeros([self.dim_y, self.dim_x], dtype=float32) |
| 231 |
|
| 232 |
|
| 233 |
points_where_background_used = count_nonzero(self.sum_single_frame_weights < |
| 234 |
self.configuration.stack_frames_background_blend_threshold * |
| 235 |
single_stack_size_float) |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
if points_where_background_used/self.number_pixels_drizzled < \ |
| 241 |
self.configuration.stack_frames_background_fraction: |
| 242 |
|
| 243 |
|
| 244 |
self.background_patches = [] |
| 245 |
|
| 246 |
|
| 247 |
for patch_y_low in range(0, self.dim_y, |
| 248 |
self.configuration.stack_frames_background_patch_size): |
| 249 |
patch_y_high = min( |
| 250 |
patch_y_low + self.configuration.stack_frames_background_patch_size, |
| 251 |
self.dim_y - 1) |
| 252 |
|
| 253 |
|
| 254 |
if patch_y_low == patch_y_high: |
| 255 |
continue |
| 256 |
for patch_x_low in range(0, self.dim_x, |
| 257 |
self.configuration.stack_frames_background_patch_size): |
| 258 |
patch_x_high = min( |
| 259 |
patch_x_low + self.configuration.stack_frames_background_patch_size, |
| 260 |
self.dim_x - 1) |
| 261 |
if patch_x_low == patch_x_high: |
| 262 |
continue |
| 263 |
|
| 264 |
|
| 265 |
if count_nonzero(self.sum_single_frame_weights[ |
| 266 |
patch_y_low * self.configuration.drizzle_factor: |
| 267 |
patch_y_high * self.configuration.drizzle_factor, |
| 268 |
patch_x_low * self.configuration.drizzle_factor: |
| 269 |
patch_x_high * self.configuration.drizzle_factor] < |
| 270 |
self.configuration.stack_frames_background_blend_threshold * |
| 271 |
single_stack_size_float) > 0: |
| 272 |
background_patch = {} |
| 273 |
background_patch['patch_y_low'] = patch_y_low |
| 274 |
background_patch['patch_y_high'] = patch_y_high |
| 275 |
background_patch['patch_x_low'] = patch_x_low |
| 276 |
background_patch['patch_x_high'] = patch_x_high |
| 277 |
self.background_patches.append(background_patch) |
| 278 |
|
| 279 |
self.my_timer.stop('Stacking: initialize background blending') |
| 280 |
|
| 281 |
def stack_frames(self): |
| 282 |
""" |
| 283 |
Compute the shifted contributions of all frames to all alignment points and add them to the |
| 284 |
appropriate alignment point stacking buffers. |
| 285 |
|
| 286 |
:return: - |
| 287 |
""" |
| 288 |
|
| 289 |
|
| 290 |
self.prepare_for_stack_blending() |
| 291 |
|
| 292 |
|
| 293 |
self.shift_distribution = full(( |
| 294 |
self.configuration.alignment_points_search_width * |
| 295 |
self.configuration.drizzle_factor * 2,), 0, dtype=int) |
| 296 |
self.shift_failure_counter = 0 |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
if self.configuration.alignment_points_method == 'MultiLevelCorrelation': |
| 301 |
|
| 302 |
self.alignment_points.set_reference_boxes_correlation() |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
search_width_second_phase = 4 |
| 307 |
max_search_width_first_phase = int( |
| 308 |
(self.configuration.alignment_points_search_width - search_width_second_phase) / 2) |
| 309 |
search_width_first_phase = max_search_width_first_phase |
| 310 |
extent = 2 * search_width_first_phase + 1 |
| 311 |
weight_matrix_first_phase = empty((extent, extent), dtype=float32) |
| 312 |
for y in range(extent): |
| 313 |
for x in range(extent): |
| 314 |
weight_matrix_first_phase[ |
| 315 |
y, x] = 1. - self.configuration.alignment_points_penalty_factor * ( |
| 316 |
(y / search_width_first_phase - 1) ** 2 + ( |
| 317 |
x / search_width_first_phase - 1) ** 2) |
| 318 |
|
| 319 |
else: |
| 320 |
weight_matrix_first_phase = None |
| 321 |
|
| 322 |
|
| 323 |
if self.debug: |
| 324 |
self.create_image_window_signal.emit() |
| 325 |
|
| 326 |
|
| 327 |
if self.configuration.frames_normalization: |
| 328 |
median_brightness = median([self.frames.average_brightness(index) |
| 329 |
for index in range(self.frames.number)]) |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
self.border_y_low = self.border_y_high = self.border_x_low = self.border_x_high = 0 |
| 336 |
|
| 337 |
|
| 338 |
for frame_index in range(self.frames.number): |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
if self.configuration.frames_normalization: |
| 343 |
frame = self.frames.frames(frame_index) * median_brightness / \ |
| 344 |
(self.frames.average_brightness(frame_index) + 1.e-7) |
| 345 |
else: |
| 346 |
frame = self.frames.frames(frame_index) |
| 347 |
|
| 348 |
|
| 349 |
if self.drizzle: |
| 350 |
self.frame_drizzled = resize(frame.astype(float32), |
| 351 |
(frame.shape[1]*self.configuration.drizzle_factor, frame.shape[0]*self.configuration.drizzle_factor), |
| 352 |
interpolation=INTER_LINEAR) |
| 353 |
else: |
| 354 |
self.frame_drizzled = frame.astype(float32) |
| 355 |
|
| 356 |
frame_mono_blurred = self.frames.frames_mono_blurred(frame_index) |
| 357 |
|
| 358 |
|
| 359 |
if self.progress_signal is not None and frame_index % self.signal_step_size == 1: |
| 360 |
self.progress_signal.emit("Stack frames", |
| 361 |
int(round(10 * frame_index / self.frames.number) * 10)) |
| 362 |
|
| 363 |
|
| 364 |
dy = self.align_frames.dy[frame_index] |
| 365 |
dx = self.align_frames.dx[frame_index] |
| 366 |
|
| 367 |
|
| 368 |
for alignment_point_index in self.frames.used_alignment_points[frame_index]: |
| 369 |
alignment_point = self.alignment_points.alignment_points[alignment_point_index] |
| 370 |
|
| 371 |
|
| 372 |
self.my_timer.start('Stacking: compute AP shifts') |
| 373 |
[shift_y, shift_x], success = self.alignment_points.compute_shift_alignment_point( |
| 374 |
frame_mono_blurred, frame_index, alignment_point_index, |
| 375 |
de_warp=self.configuration.alignment_points_de_warp, |
| 376 |
weight_matrix_first_phase=weight_matrix_first_phase, |
| 377 |
subpixel_solve=self.drizzle) |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
shift_y_drizzled = int(round(shift_y * self.configuration.drizzle_factor)) |
| 384 |
shift_x_drizzled = int(round(shift_x * self.configuration.drizzle_factor)) |
| 385 |
total_shift_y_drizzled = dy * self.configuration.drizzle_factor - shift_y_drizzled |
| 386 |
total_shift_x_drizzled = dx * self.configuration.drizzle_factor - shift_x_drizzled |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
if success: |
| 391 |
self.shift_distribution[int(round(sqrt(shift_y_drizzled ** 2 + shift_x_drizzled ** 2)))] += 1 |
| 392 |
else: |
| 393 |
self.shift_failure_counter += 1 |
| 394 |
|
| 395 |
self.my_timer.stop('Stacking: compute AP shifts') |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
if self.debug and not alignment_point_index: |
| 400 |
frame_mono_blurred = self.frames.frames_mono_blurred(frame_index) |
| 401 |
total_shift_y = dy - shift_y |
| 402 |
total_shift_y_int = int(round(total_shift_y)) |
| 403 |
total_shift_x = dx - shift_x |
| 404 |
total_shift_x_int = int(round(total_shift_x)) |
| 405 |
y_low = alignment_point['patch_y_low'] |
| 406 |
y_high = alignment_point['patch_y_high'] |
| 407 |
x_low = alignment_point['patch_x_low'] |
| 408 |
x_high = alignment_point['patch_x_high'] |
| 409 |
reference_patch = (self.alignment_points.mean_frame[y_low:y_high, x_low:x_high]).astype(uint16) |
| 410 |
reference_patch = resize(reference_patch, None, |
| 411 |
fx=float(self.scale_factor), |
| 412 |
fy=float(self.scale_factor)) |
| 413 |
|
| 414 |
try: |
| 415 |
|
| 416 |
frame_stabilized = frame_mono_blurred[y_low+dy:y_high+dy, x_low+dx:x_high+dx] |
| 417 |
frame_stabilized = resize(frame_stabilized, None, |
| 418 |
fx=float(self.scale_factor), |
| 419 |
fy=float(self.scale_factor)) |
| 420 |
font = FONT_HERSHEY_SIMPLEX |
| 421 |
fontScale = 0.5 |
| 422 |
fontColor = (0, 255, 0) |
| 423 |
lineType = 1 |
| 424 |
putText(frame_stabilized, 'stabilized: ' + str(dy) + ', ' + str(dx), |
| 425 |
(5, 25), font, fontScale, fontColor, lineType) |
| 426 |
|
| 427 |
frame_dewarped = frame_mono_blurred[y_low+total_shift_y_int:y_high+total_shift_y_int, |
| 428 |
x_low+total_shift_x_int:x_high+total_shift_x_int] |
| 429 |
frame_dewarped = resize(frame_dewarped, None, |
| 430 |
fx=float(self.scale_factor), |
| 431 |
fy=float(self.scale_factor)) |
| 432 |
putText(frame_dewarped, 'de-warped: ' + str(int(round(shift_y))) + ', ' + |
| 433 |
str(int(round(shift_x))), (5, 25), font, fontScale, fontColor, lineType) |
| 434 |
|
| 435 |
|
| 436 |
composed_image = Miscellaneous.compose_image([frame_stabilized, |
| 437 |
reference_patch, frame_dewarped], |
| 438 |
border=self.border) |
| 439 |
self.update_image_window_signal.emit(composed_image) |
| 440 |
except Exception as e: |
| 441 |
print(str(e)) |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
sleep(self.image_delay) |
| 446 |
|
| 447 |
|
| 448 |
self.my_timer.start('Stacking: remapping and adding') |
| 449 |
self.remap_rigid(self.frame_drizzled, alignment_point['stacking_buffer'], |
| 450 |
total_shift_y_drizzled, total_shift_x_drizzled, |
| 451 |
alignment_point['patch_y_low_drizzled'], |
| 452 |
alignment_point['patch_y_high_drizzled'], |
| 453 |
alignment_point['patch_x_low_drizzled'], |
| 454 |
alignment_point['patch_x_high_drizzled']) |
| 455 |
self.my_timer.stop('Stacking: remapping and adding') |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
if self.number_stacking_holes > 0 and \ |
| 460 |
frame_index in self.rank_frames.quality_sorted_indices[ |
| 461 |
:self.alignment_points.stack_size]: |
| 462 |
self.my_timer.start('Stacking: computing background') |
| 463 |
|
| 464 |
|
| 465 |
if self.background_patches: |
| 466 |
for patch in self.background_patches: |
| 467 |
self.averaged_background[patch['patch_y_low']:patch['patch_y_high'], |
| 468 |
patch['patch_x_low']:patch['patch_x_high']] += \ |
| 469 |
frame[patch['patch_y_low'] + dy : patch['patch_y_high'] + dy, |
| 470 |
patch['patch_x_low'] + dx : patch['patch_x_high'] + dx] |
| 471 |
|
| 472 |
|
| 473 |
else: |
| 474 |
self.averaged_background += frame[dy:self.dim_y + dy, dx:self.dim_x + dx] |
| 475 |
self.my_timer.stop('Stacking: computing background') |
| 476 |
|
| 477 |
if self.progress_signal is not None: |
| 478 |
self.progress_signal.emit("Stack frames", 100) |
| 479 |
|
| 480 |
|
| 481 |
shift_counter = sum(self.shift_distribution) |
| 482 |
self.shift_entries_total = shift_counter + self.shift_failure_counter |
| 483 |
if self.shift_entries_total: |
| 484 |
self.shift_failure_percent = round( |
| 485 |
100. * self.shift_failure_counter / self.shift_entries_total, 3) |
| 486 |
else: |
| 487 |
|
| 488 |
self.shift_failure_percent = -1. |
| 489 |
|
| 490 |
|
| 491 |
if self.debug: |
| 492 |
self.terminate_image_window_signal.emit() |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
if self.number_stacking_holes > 0: |
| 497 |
self.my_timer.start('Stacking: computing background') |
| 498 |
|
| 499 |
if self.drizzle: |
| 500 |
|
| 501 |
self.averaged_background = resize(self.averaged_background, |
| 502 |
(self.dim_x_drizzled, self.dim_y_drizzled), |
| 503 |
interpolation=INTER_CUBIC) |
| 504 |
|
| 505 |
self.averaged_background /= self.alignment_points.stack_size |
| 506 |
self.my_timer.stop('Stacking: computing background') |
| 507 |
|
| 508 |
def remap_rigid(self, frame, buffer, shift_y, shift_x, y_low, y_high, x_low, x_high): |
| 509 |
""" |
| 510 |
The alignment point patch is taken from the given frame with a constant shift in x and y |
| 511 |
directions. The shifted patch is then added to the given alignment point buffer. |
| 512 |
|
| 513 |
:param frame: frame to be stacked |
| 514 |
:param buffer: Stacking buffer of the corresponding alignment point |
| 515 |
:param shift_y: Constant shift in y direction between frame stack and current frame |
| 516 |
:param shift_x: Constant shift in x direction between frame stack and current frame |
| 517 |
:param y_low: Lower y index of the quality window on which this method operates |
| 518 |
:param y_high: Upper y index of the quality window on which this method operates |
| 519 |
:param x_low: Lower x index of the quality window on which this method operates |
| 520 |
:param x_high: Upper x index of the quality window on which this method operates |
| 521 |
:return: - |
| 522 |
""" |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
frame_size_y = frame.shape[0] |
| 528 |
y_low_source = y_low + shift_y |
| 529 |
y_high_source = y_high + shift_y |
| 530 |
y_low_target = 0 |
| 531 |
|
| 532 |
if y_low_source < 0: |
| 533 |
y_low_target = -y_low_source |
| 534 |
y_low_source = 0 |
| 535 |
self.border_y_low = max(self.border_y_low, y_low_target) |
| 536 |
if y_high_source > frame_size_y: |
| 537 |
self.border_y_high = max(self.border_y_high, y_high_source - frame_size_y) |
| 538 |
y_high_source = frame_size_y |
| 539 |
y_high_target = y_low_target + y_high_source - y_low_source |
| 540 |
|
| 541 |
frame_size_x = frame.shape[1] |
| 542 |
x_low_source = x_low + shift_x |
| 543 |
x_high_source = x_high + shift_x |
| 544 |
x_low_target = 0 |
| 545 |
|
| 546 |
if x_low_source < 0: |
| 547 |
x_low_target = -x_low_source |
| 548 |
x_low_source = 0 |
| 549 |
self.border_x_low = max(self.border_x_low, x_low_target) |
| 550 |
if x_high_source > frame_size_x: |
| 551 |
self.border_x_high = max(self.border_x_high, x_high_source - frame_size_x) |
| 552 |
x_high_source = frame_size_x |
| 553 |
x_high_target = x_low_target + x_high_source - x_low_source |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
buffer[y_low_target:y_high_target, x_low_target:x_high_target] += \ |
| 559 |
frame[y_low_source:y_high_source, x_low_source:x_high_source] |
| 560 |
|
| 561 |
def merge_alignment_point_buffers(self): |
| 562 |
""" |
| 563 |
Merge the summation buffers for all alignment points into the global stacking buffer. For |
| 564 |
every pixel location divide the global buffer by the number of contributing image patches. |
| 565 |
This results in a uniform brightness level across the whole image, even if alignment point |
| 566 |
patches overlap. |
| 567 |
|
| 568 |
:return: The final stacked image |
| 569 |
""" |
| 570 |
|
| 571 |
self.my_timer.start('Stacking: merging AP buffers') |
| 572 |
|
| 573 |
|
| 574 |
for alignment_point in self.alignment_points.alignment_points: |
| 575 |
patch_y_low_drizzled = alignment_point['patch_y_low_drizzled'] |
| 576 |
patch_y_high_drizzled = alignment_point['patch_y_high_drizzled'] |
| 577 |
patch_x_low_drizzled = alignment_point['patch_x_low_drizzled'] |
| 578 |
patch_x_high_drizzled = alignment_point['patch_x_high_drizzled'] |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
if self.frames.color: |
| 583 |
self.stacked_image_buffer[patch_y_low_drizzled:patch_y_high_drizzled, |
| 584 |
patch_x_low_drizzled: patch_x_high_drizzled, :] += \ |
| 585 |
alignment_point['stacking_buffer'] * alignment_point['weights_yx'][:, :, newaxis] |
| 586 |
else: |
| 587 |
self.stacked_image_buffer[patch_y_low_drizzled:patch_y_high_drizzled, |
| 588 |
patch_x_low_drizzled: patch_x_high_drizzled] += alignment_point['stacking_buffer'] * \ |
| 589 |
alignment_point['weights_yx'] |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
if self.frames.color: |
| 595 |
self.stacked_image_buffer /= self.sum_single_frame_weights[:, :, newaxis] |
| 596 |
else: |
| 597 |
self.stacked_image_buffer /= self.sum_single_frame_weights |
| 598 |
|
| 599 |
self.my_timer.stop('Stacking: merging AP buffers') |
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
if self.number_stacking_holes > 0: |
| 605 |
self.my_timer.create_no_check('Stacking: blending APs with background') |
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
foreground_weight = self.sum_single_frame_weights / \ |
| 613 |
(self.configuration.stack_frames_background_blend_threshold * |
| 614 |
self.alignment_points.stack_size) |
| 615 |
clip(foreground_weight, 0., 1., out=foreground_weight) |
| 616 |
|
| 617 |
|
| 618 |
if self.frames.color: |
| 619 |
self.stacked_image_buffer = (self.stacked_image_buffer-self.averaged_background) * \ |
| 620 |
foreground_weight[:, :, newaxis] + \ |
| 621 |
self.averaged_background |
| 622 |
else: |
| 623 |
self.stacked_image_buffer = (self.stacked_image_buffer-self.averaged_background) * \ |
| 624 |
foreground_weight + self.averaged_background |
| 625 |
|
| 626 |
self.my_timer.stop('Stacking: blending APs with background') |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
if self.border_y_low or self.border_y_high or self.border_x_low or self.border_x_high: |
| 631 |
self.stacked_image_buffer = self.stacked_image_buffer[ |
| 632 |
self.border_y_low:self.dim_y_drizzled - self.border_y_high, |
| 633 |
self.border_x_low:self.dim_x_drizzled - self.border_x_high] |
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
if self.frames.depth == 8: |
| 638 |
self.stacked_image = img_as_uint( |
| 639 |
clip(self.stacked_image_buffer / 255, 0., 1., out=self.stacked_image_buffer)) |
| 640 |
else: |
| 641 |
self.stacked_image = img_as_uint( |
| 642 |
clip(self.stacked_image_buffer / 65535, 0., 1., out=self.stacked_image_buffer)) |
| 643 |
|
| 644 |
return self.stacked_image |
| 645 |
|
| 646 |
def half_stacked_image_buffer_resolution(self): |
| 647 |
""" |
| 648 |
If drizzling is active with the option 1.5x, the computations are performed with the factor |
| 649 |
3x. As a final step, the resolution of the stacked image buffer is halved. |
| 650 |
|
| 651 |
:return: - |
| 652 |
""" |
| 653 |
|
| 654 |
self.stacked_image = resize(self.stacked_image, None, |
| 655 |
fx=float(0.5), fy=float(0.5)) |
| 656 |
|
| 657 |
@staticmethod |
| 658 |
def one_dim_weight(patch_low, patch_high, box_center, extend_low=False, extend_high=False): |
| 659 |
""" |
| 660 |
Compute one-dimensional weighting ramps between box center and patch borders. This function |
| 661 |
is called for y and x dimensions separately. |
| 662 |
|
| 663 |
:param patch_low: Lower index of AP patch in the given coordinate direction |
| 664 |
:param patch_high: Upper index of AP patch in the given coordinate direction |
| 665 |
:param box_center: AP coordinate index in the given direction (center of box) |
| 666 |
:param extend_low: If true, set all weights from patch_low to box_center to 1. |
| 667 |
(and thus replace the ramp in that index range) |
| 668 |
:param extend_high: If true, set all weights from box_center to patch_high-1 to 1. |
| 669 |
(and thus replace the ramp in that index range) |
| 670 |
:return: Vector with weights, starting with 1./(box_center - patch_low +1) at patch_low, |
| 671 |
ramping up to 1. at box_center, and than ramping down to |
| 672 |
1./(patch_high - box_center) at patch_high-1. |
| 673 |
""" |
| 674 |
|
| 675 |
|
| 676 |
patch_high_offset = patch_high - patch_low |
| 677 |
center_offset = box_center - patch_low |
| 678 |
|
| 679 |
|
| 680 |
weights = empty((patch_high_offset,), dtype=float32) |
| 681 |
|
| 682 |
|
| 683 |
if extend_low: |
| 684 |
weights[0:center_offset] = 1. |
| 685 |
|
| 686 |
else: |
| 687 |
weights[0:center_offset] = arange(1, center_offset + 1, 1) / float32( |
| 688 |
center_offset + 1) |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
if extend_high: |
| 694 |
weights[center_offset:patch_high_offset] = 1. |
| 695 |
else: |
| 696 |
weights[center_offset:patch_high_offset] = arange(patch_high - box_center, 0, |
| 697 |
-1) / float32( |
| 698 |
patch_high - box_center) |
| 699 |
|
| 700 |
return weights |
| 701 |
|
| 702 |
def print_shift_table(self): |
| 703 |
""" |
| 704 |
Print a table giving for each shift (in pixels) the number of occurrences. The table ends at |
| 705 |
the last non-zero entry. |
| 706 |
|
| 707 |
:return: String with three lines to be printed to the protocol file(s) |
| 708 |
""" |
| 709 |
|
| 710 |
|
| 711 |
if max(self.shift_distribution) > 0: |
| 712 |
max_index = [index for index, item in enumerate(self.shift_distribution) if item != 0][-1] \ |
| 713 |
+ 1 |
| 714 |
|
| 715 |
|
| 716 |
s = " Shift (pixels):" |
| 717 |
line = " ---------------" |
| 718 |
t = " Percent: " |
| 719 |
|
| 720 |
|
| 721 |
for index in range(max_index): |
| 722 |
s += "|{:7d} ".format(index) |
| 723 |
line += "---------" |
| 724 |
t += "|{:7.3f} ".format(100.*self.shift_distribution[index]/self.shift_entries_total) |
| 725 |
|
| 726 |
|
| 727 |
s += "|" |
| 728 |
line += "-" |
| 729 |
t += "|" |
| 730 |
|
| 731 |
|
| 732 |
return s + "\n" + line + "\n" + t + "\n\n" + \ |
| 733 |
" Failed shift measurements: {:7.3f} %".format( |
| 734 |
self.shift_failure_percent) |
| 735 |
else: |
| 736 |
return "" |
| 737 |
|
| 738 |
|
| 739 |
if __name__ == "__main__": |
| 740 |
|
| 741 |
my_timer = timer() |
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
type = 'video' |
| 746 |
if type == 'image': |
| 747 |
names = glob('Images/2012*.tif') |
| 748 |
|
| 749 |
|
| 750 |
else: |
| 751 |
names = 'Videos/another_short_video.avi' |
| 752 |
print(names) |
| 753 |
|
| 754 |
my_timer.create('Execution over all') |
| 755 |
|
| 756 |
|
| 757 |
configuration = Configuration() |
| 758 |
configuration.initialize_configuration() |
| 759 |
|
| 760 |
my_timer.create('Read all frames') |
| 761 |
try: |
| 762 |
frames = Frames(configuration, names, type=type, bayer_option_selected="Grayscale") |
| 763 |
print("Number of images read: " + str(frames.number)) |
| 764 |
print("Image shape: " + str(frames.shape)) |
| 765 |
except Error as e: |
| 766 |
print("Error: " + e.message) |
| 767 |
exit() |
| 768 |
my_timer.stop('Read all frames') |
| 769 |
|
| 770 |
|
| 771 |
my_timer.create('Ranking images') |
| 772 |
rank_frames = RankFrames(frames, configuration) |
| 773 |
rank_frames.frame_score() |
| 774 |
my_timer.stop('Ranking images') |
| 775 |
|
| 776 |
print("Index of maximum: " + str(rank_frames.frame_ranks_max_index)) |
| 777 |
print("Frame scores: " + str(rank_frames.frame_ranks)) |
| 778 |
print("Frame scores (sorted): " + str( |
| 779 |
[rank_frames.frame_ranks[i] for i in rank_frames.quality_sorted_indices])) |
| 780 |
print("Sorted index list: " + str(rank_frames.quality_sorted_indices)) |
| 781 |
|
| 782 |
|
| 783 |
my_timer.create('Select optimal alignment patch') |
| 784 |
align_frames = AlignFrames(frames, rank_frames, configuration) |
| 785 |
|
| 786 |
if configuration.align_frames_mode == "Surface": |
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
(y_low_opt, y_high_opt, x_low_opt, x_high_opt) = align_frames.compute_alignment_rect( |
| 791 |
configuration.align_frames_rectangle_scale_factor) |
| 792 |
my_timer.stop('Select optimal alignment patch') |
| 793 |
print("optimal alignment rectangle, x_low: " + str(x_low_opt) + ", x_high: " + str( |
| 794 |
x_high_opt) + ", y_low: " + str(y_low_opt) + ", y_high: " + str(y_high_opt)) |
| 795 |
reference_frame_with_alignment_points = frames.frames_mono( |
| 796 |
rank_frames.frame_ranks_max_index).copy() |
| 797 |
reference_frame_with_alignment_points[y_low_opt, |
| 798 |
x_low_opt:x_high_opt] = reference_frame_with_alignment_points[y_high_opt - 1, |
| 799 |
x_low_opt:x_high_opt] = 255 |
| 800 |
reference_frame_with_alignment_points[y_low_opt:y_high_opt, |
| 801 |
x_low_opt] = reference_frame_with_alignment_points[y_low_opt:y_high_opt, x_high_opt - 1] = 255 |
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
my_timer.create('Global frame alignment') |
| 807 |
try: |
| 808 |
align_frames.align_frames() |
| 809 |
except NotSupportedError as e: |
| 810 |
print("Error: " + e.message) |
| 811 |
exit() |
| 812 |
except InternalError as e: |
| 813 |
print("Warning: " + e.message) |
| 814 |
my_timer.stop('Global frame alignment') |
| 815 |
|
| 816 |
|
| 817 |
print("Intersection: " + str(align_frames.intersection_shape)) |
| 818 |
|
| 819 |
|
| 820 |
my_timer.create('Compute reference frame') |
| 821 |
average = align_frames.average_frame() |
| 822 |
my_timer.stop('Compute reference frame') |
| 823 |
print("Reference frame computed from the best " + str( |
| 824 |
align_frames.average_frame_number) + " frames.") |
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
my_timer.create('Initialize alignment point object') |
| 833 |
alignment_points = AlignmentPoints(configuration, frames, rank_frames, align_frames) |
| 834 |
my_timer.stop('Initialize alignment point object') |
| 835 |
|
| 836 |
|
| 837 |
my_timer.create('Create alignment points') |
| 838 |
alignment_points.create_ap_grid() |
| 839 |
my_timer.stop('Create alignment points') |
| 840 |
print("Number of alignment points created: " + str(len(alignment_points.alignment_points)) + |
| 841 |
", number of dropped aps (dim): " + str(alignment_points.alignment_points_dropped_dim) + |
| 842 |
", number of dropped aps (structure): " + str( |
| 843 |
alignment_points.alignment_points_dropped_structure)) |
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
my_timer.create('Rank frames at alignment points') |
| 851 |
alignment_points.compute_frame_qualities() |
| 852 |
my_timer.stop('Rank frames at alignment points') |
| 853 |
|
| 854 |
|
| 855 |
stack_frames = StackFrames(configuration, frames, rank_frames, align_frames, alignment_points, my_timer) |
| 856 |
|
| 857 |
|
| 858 |
stack_frames.stack_frames() |
| 859 |
|
| 860 |
|
| 861 |
stacked_image = stack_frames.merge_alignment_point_buffers() |
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
if configuration.drizzle_factor_is_1_5: |
| 866 |
stack_frames.half_stacked_image_buffer_resolution() |
| 867 |
|
| 868 |
|
| 869 |
Frames.save_image('Images/example_stacked.tiff', stacked_image, color=frames.color, |
| 870 |
header=configuration.global_parameters_version) |
| 871 |
|
| 872 |
|
| 873 |
plt.imshow(img_as_ubyte(stacked_image)) |
| 874 |
plt.show() |
| 875 |
|
| 876 |
|
| 877 |
my_timer.stop('Execution over all') |
| 878 |
my_timer.print() |