| 1 |
from math import ceil |
| 2 |
|
| 3 |
from numpy import ndarray, zeros |
| 4 |
|
| 5 |
from miscellaneous import Miscellaneous |
| 6 |
|
| 7 |
|
| 8 |
def compute_bounds(x_low, x_high, drizzle_factor, shift): |
| 9 |
""" |
| 10 |
Compute the index bounds in the original (non-drizzled) frame from where the AP patch is to be |
| 11 |
copied, and the (drizzled) index bounds in the summation buffer. The data are copied with stride |
| 12 |
"drizzle_factor". |
| 13 |
|
| 14 |
:param x_low: Lower index bound of AP patch in original frame. |
| 15 |
:param x_high: Upper index bound of AP patch in original frame. |
| 16 |
:param drizzle_factor: (integer) drizzle factor, typically 2 or 3. |
| 17 |
:param shift: Shift (float) between AP patches in summation buffer and original frame, |
| 18 |
expressed in non-drizzled units. |
| 19 |
:return: Tuple (x_low_from, x_high_from, xd_low_to, xd_high_to, x_offset) with: |
| 20 |
x_low_from: Lower source index bound for copy operation in original frame. |
| 21 |
x_high_from: Upper source index bound for copy operation in original frame (stride 1). |
| 22 |
xd_low_to: Lower target index bound for copy operation in summation buffer. |
| 23 |
xd_high_to: Upper target index bound for copy operation in summation buffer |
| 24 |
(stride "drizzle_factor"). |
| 25 |
x_offset: Index offset in target patch for begin of copy operation |
| 26 |
(0, 1, ..., drizzle_factor-1). |
| 27 |
""" |
| 28 |
|
| 29 |
|
| 30 |
shift_d = int(round(drizzle_factor * shift)) |
| 31 |
|
| 32 |
|
| 33 |
shift_rounded = shift_d / drizzle_factor |
| 34 |
|
| 35 |
|
| 36 |
if shift_rounded.is_integer(): |
| 37 |
shift_rounded = int(shift_rounded) |
| 38 |
x_low_from = x_low + shift_rounded |
| 39 |
x_high_from = x_high + shift_rounded |
| 40 |
x_offset = 0 |
| 41 |
|
| 42 |
|
| 43 |
else: |
| 44 |
shift_ceil = ceil(shift) |
| 45 |
x_low_from = x_low + shift_ceil |
| 46 |
x_high_from = x_high + shift_ceil |
| 47 |
x_offset = int(drizzle_factor * shift_ceil - shift_d) |
| 48 |
|
| 49 |
xd_low_to = drizzle_factor * x_low_from - shift_d |
| 50 |
xd_high_to = drizzle_factor * x_high_from - shift_d |
| 51 |
|
| 52 |
return x_low_from, x_high_from, xd_low_to, xd_high_to, x_offset |
| 53 |
|
| 54 |
|
| 55 |
def compute_bounds_2d(y_low, y_high, x_low, x_high, shift_y, shift_x, drizzle_factor): |
| 56 |
""" |
| 57 |
Compute the index bounds in the original (non-drizzled) frame from where the AP patch is |
| 58 |
to be copied, and the (drizzled) index bounds in the summation buffer. The data are copied with |
| 59 |
stride "drizzle_factor". |
| 60 |
|
| 61 |
:param y_low: Lower y index bound of AP patch in original frame. |
| 62 |
:param y_high: Upper y index bound of AP patch in original frame. |
| 63 |
:param x_low: Lower x index bound of AP patch in original frame. |
| 64 |
:param x_high: Upper x index bound of AP patch in original frame. |
| 65 |
:param shift_y: Shift (float) in y between AP patches in summation buffer and original frame, |
| 66 |
expressed in non-drizzled units. |
| 67 |
:param shift_x: Shift (float) in x between AP patches in summation buffer and original frame, |
| 68 |
expressed in non-drizzled units. |
| 69 |
:param drizzle_factor: (integer) drizzle factor, typically 2 or 3. |
| 70 |
:return: Tuple (y_low_from, y_high_from, x_low_from, x_high_from, y_offset, x_offset) with: |
| 71 |
y_low_from: Lower source y index bound for copy operation in original frame. |
| 72 |
y_high_from: Upper source y index bound for copy operation in original frame ( |
| 73 |
stride 1). |
| 74 |
x_low_from: Lower source x index bound for copy operation in original frame. |
| 75 |
x_high_from: Upper source x index bound for copy operation in original frame ( |
| 76 |
stride 1). |
| 77 |
y_offset: Index offset in y direction in target patch for begin of copy operation |
| 78 |
(0, 1, ..., drizzle_factor-1). |
| 79 |
x_offset: Index offset in x direction in target patch for begin of copy operation |
| 80 |
(0, 1, ..., drizzle_factor-1). |
| 81 |
""" |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
shift_d_y = int(round(drizzle_factor * shift_y)) |
| 86 |
shift_d_x = int(round(drizzle_factor * shift_x)) |
| 87 |
|
| 88 |
|
| 89 |
shift_rounded_y = shift_d_y / drizzle_factor |
| 90 |
shift_rounded_x = shift_d_x / drizzle_factor |
| 91 |
|
| 92 |
|
| 93 |
if shift_rounded_y.is_integer(): |
| 94 |
shift_rounded_y = int(shift_rounded_y) |
| 95 |
y_low_from = y_low + shift_rounded_y |
| 96 |
y_high_from = y_high + shift_rounded_y |
| 97 |
y_offset = 0 |
| 98 |
|
| 99 |
|
| 100 |
else: |
| 101 |
shift_ceil = ceil(shift_y) |
| 102 |
y_low_from = y_low + shift_ceil |
| 103 |
y_high_from = y_high + shift_ceil |
| 104 |
y_offset = int(drizzle_factor * shift_ceil - shift_d_y) |
| 105 |
|
| 106 |
|
| 107 |
if shift_rounded_x.is_integer(): |
| 108 |
shift_rounded_x = int(shift_rounded_x) |
| 109 |
x_low_from = x_low + shift_rounded_x |
| 110 |
x_high_from = x_high + shift_rounded_x |
| 111 |
x_offset = 0 |
| 112 |
|
| 113 |
|
| 114 |
else: |
| 115 |
shift_ceil = ceil(shift_x) |
| 116 |
x_low_from = x_low + shift_ceil |
| 117 |
x_high_from = x_high + shift_ceil |
| 118 |
x_offset = int(drizzle_factor * shift_ceil - shift_d_x) |
| 119 |
|
| 120 |
return y_low_from, y_high_from, x_low_from, x_high_from, y_offset, x_offset |
| 121 |
|
| 122 |
def remap_rigid_drizzled(frame, buffer, offset_counters, shift_y, shift_x, y_low, y_high, x_low, |
| 123 |
x_high, drizzle_factor): |
| 124 |
""" |
| 125 |
The alignment point patch is taken from the given frame with a constant shift in x and y |
| 126 |
directions. The shifted patch is then added to the given alignment point buffer. |
| 127 |
|
| 128 |
Please note that all indices related to the frame, and all input arguments in particular, are |
| 129 |
in non-drizzled coordinates. The size of the buffer, on the other side, is extended by the |
| 130 |
drizzling factor "drizzle_factor". To enable sub-pixel accuracy, shift values are input as |
| 131 |
non-integer floats. |
| 132 |
|
| 133 |
:param frame: frame to be stacked |
| 134 |
:param buffer: Stacking buffer of the corresponding alignment point (extended for drizzling) |
| 135 |
:param offset_counters: Integer array, size drizzle_factor x drizzle_factor, with contribution |
| 136 |
counts at all drizzle locations. |
| 137 |
:param shift_y: Constant shift in y direction between frame stack and current frame (float) |
| 138 |
:param shift_x: Constant shift in x direction between frame stack and current frame (float) |
| 139 |
:param y_low: Lower y index of the quality window on which this method operates |
| 140 |
:param y_high: Upper y index of the quality window on which this method operates |
| 141 |
:param x_low: Lower x index of the quality window on which this method operates |
| 142 |
:param x_high: Upper x index of the quality window on which this method operates |
| 143 |
:param drizzle_factor: (integer) drizzle factor, typically 2 or 3. |
| 144 |
:return: - |
| 145 |
""" |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
shift_d_y = int(round(drizzle_factor * shift_y)) |
| 150 |
shift_d_x = int(round(drizzle_factor * shift_x)) |
| 151 |
|
| 152 |
|
| 153 |
shift_rounded_y = shift_d_y / drizzle_factor |
| 154 |
shift_rounded_x = shift_d_x / drizzle_factor |
| 155 |
|
| 156 |
|
| 157 |
if shift_rounded_y.is_integer(): |
| 158 |
shift_rounded_y = int(shift_rounded_y) |
| 159 |
y_low_from = y_low + shift_rounded_y |
| 160 |
y_high_from = y_high + shift_rounded_y |
| 161 |
y_offset = 0 |
| 162 |
|
| 163 |
|
| 164 |
else: |
| 165 |
shift_ceil = ceil(shift_y) |
| 166 |
y_low_from = y_low + shift_ceil |
| 167 |
y_high_from = y_high + shift_ceil |
| 168 |
y_offset = int(drizzle_factor * shift_ceil - shift_d_y) |
| 169 |
|
| 170 |
|
| 171 |
if shift_rounded_x.is_integer(): |
| 172 |
shift_rounded_x = int(shift_rounded_x) |
| 173 |
x_low_from = x_low + shift_rounded_x |
| 174 |
x_high_from = x_high + shift_rounded_x |
| 175 |
x_offset = 0 |
| 176 |
|
| 177 |
|
| 178 |
else: |
| 179 |
shift_ceil = ceil(shift_x) |
| 180 |
x_low_from = x_low + shift_ceil |
| 181 |
x_high_from = x_high + shift_ceil |
| 182 |
x_offset = int(drizzle_factor * shift_ceil - shift_d_x) |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
frame_size_y = frame.shape[0] |
| 188 |
y_low_target = y_offset |
| 189 |
|
| 190 |
if y_low_from < 0: |
| 191 |
y_low_target -= y_low_from*drizzle_factor |
| 192 |
y_low_from = 0 |
| 193 |
if y_high_from > frame_size_y: |
| 194 |
y_high_from = frame_size_y |
| 195 |
y_high_target = y_low_target + (y_high_from - y_low_from)*drizzle_factor |
| 196 |
|
| 197 |
|
| 198 |
frame_size_x = frame.shape[1] |
| 199 |
x_low_target = x_offset |
| 200 |
|
| 201 |
if x_low_from < 0: |
| 202 |
x_low_target -= x_low_from * drizzle_factor |
| 203 |
x_low_from = 0 |
| 204 |
if x_high_from > frame_size_x: |
| 205 |
x_high_from = frame_size_x |
| 206 |
x_high_target = x_low_target + (x_high_from - x_low_from) * drizzle_factor |
| 207 |
|
| 208 |
|
| 209 |
buffer[y_low_target:y_high_target:drizzle_factor, x_low_target:x_high_target:drizzle_factor] += \ |
| 210 |
frame[y_low_from:y_high_from, x_low_from:x_high_from] |
| 211 |
|
| 212 |
|
| 213 |
offset_counters[y_offset, x_offset] += 1 |
| 214 |
|
| 215 |
def equalize_ap_patch(patch, offset_counters, stack_size, drizzle_factor): |
| 216 |
""" |
| 217 |
During drizzling the AP patch gets different numbers of frame contributions at different |
| 218 |
positions in the drizzling pattern, depending on the distribution of sup-pixel shift values. |
| 219 |
The array "offset_counters" contains these numbers, with the sum of all those numbers being the |
| 220 |
total stack size. |
| 221 |
|
| 222 |
Scale all patch entries with "total stack size" / "offset counter". The result are uniform |
| 223 |
patch entries which look as if there had been "total stack size" contributions in all drizzle |
| 224 |
pattern locations. |
| 225 |
|
| 226 |
Special care has to be taken at locations where not a single frame contributed (called "holes"). |
| 227 |
Here insert the average of patch values at all drizzle positions over a circle with minimum |
| 228 |
radius containing at least one non-zero contribution. |
| 229 |
|
| 230 |
:param patch: AP buffer after stacking with drizzling. |
| 231 |
:param offset_counters: Integer array, size drizzle_factor x drizzle_factor, with contribution |
| 232 |
counts at all drizzle locations. |
| 233 |
:param stack_size: Overall number of frames stacked |
| 234 |
:param drizzle_factor: Factor by which the number of pixels is multiplied in each direction. |
| 235 |
:return: Number of drizzle locations without frame contributions. |
| 236 |
""" |
| 237 |
|
| 238 |
dim_y, dim_x = patch.shape[:2] |
| 239 |
holes = [] |
| 240 |
|
| 241 |
|
| 242 |
for y_offset in range(drizzle_factor): |
| 243 |
for x_offset in range(drizzle_factor): |
| 244 |
if offset_counters[y_offset, x_offset]: |
| 245 |
normalization_factor = stack_size / offset_counters[y_offset, x_offset] |
| 246 |
patch[y_offset:dim_y:drizzle_factor, |
| 247 |
x_offset:dim_x:drizzle_factor] *= normalization_factor |
| 248 |
|
| 249 |
else: |
| 250 |
holes.append((y_offset, x_offset)) |
| 251 |
|
| 252 |
|
| 253 |
for (y_offset, x_offset) in holes: |
| 254 |
|
| 255 |
|
| 256 |
patch[y_offset:dim_y:drizzle_factor, x_offset:dim_x:drizzle_factor] = 0. |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
for radius in range(1, drizzle_factor): |
| 261 |
n_success = 0 |
| 262 |
for (y, x) in Miscellaneous.circle_around(y_offset, x_offset, radius): |
| 263 |
if 0 <= y < drizzle_factor and 0 <= x < drizzle_factor and (y, x) not in holes: |
| 264 |
|
| 265 |
patch[y_offset:dim_y:drizzle_factor, x_offset:dim_x:drizzle_factor] += \ |
| 266 |
patch[y:dim_y:drizzle_factor, x:dim_x:drizzle_factor] |
| 267 |
n_success += 1 |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
if n_success: |
| 272 |
patch[y_offset:dim_y:drizzle_factor, |
| 273 |
x_offset:dim_x:drizzle_factor] *= 1. / n_success |
| 274 |
break |
| 275 |
|
| 276 |
|
| 277 |
return len(holes) |
| 278 |
|
| 279 |
|
| 280 |
def test_index_computations(): |
| 281 |
|
| 282 |
drizzle_factor = 3 |
| 283 |
x_low = 4 |
| 284 |
x_high = 6 |
| 285 |
shift = -2.52 |
| 286 |
|
| 287 |
|
| 288 |
x_low_from, x_high_from, xd_low_to, xd_high_to, x_offset = compute_bounds(x_low, x_high, |
| 289 |
drizzle_factor, |
| 290 |
shift) |
| 291 |
|
| 292 |
|
| 293 |
print("x_low_from: " + str(x_low_from) + ", x_high_from: " + str( |
| 294 |
x_high_from) + "\nxd_low_to: " + str(xd_low_to) + ", xd_high_to: " + str( |
| 295 |
xd_high_to) + "\nx_offset: " + str(x_offset)) |
| 296 |
|
| 297 |
|
| 298 |
def test_index_computations_2d(): |
| 299 |
|
| 300 |
drizzle_factor = 3 |
| 301 |
y_low = 4 |
| 302 |
y_high = 6 |
| 303 |
shift_y = 3.14 |
| 304 |
x_low = 4 |
| 305 |
x_high = 6 |
| 306 |
shift_x = -2.52 |
| 307 |
|
| 308 |
|
| 309 |
y_low_from, y_high_from, x_low_from, x_high_from, y_offset, x_offset = \ |
| 310 |
compute_bounds_2d(y_low, y_high, x_low, x_high, shift_y, shift_x, drizzle_factor) |
| 311 |
|
| 312 |
|
| 313 |
print("y_low_from: " + str(y_low_from) + ", y_high_from: " + str( |
| 314 |
y_high_from) + "\ny_offset: " + str(y_offset) + "\nx_low_from: " + str( |
| 315 |
x_low_from) + ", x_high_from: " + str(x_high_from) + "\nx_offset: " + str(x_offset)) |
| 316 |
|
| 317 |
|
| 318 |
def test_remap_rigid_drizzled(): |
| 319 |
drizzle_factor = 3 |
| 320 |
y_dim = 20 |
| 321 |
x_dim = 16 |
| 322 |
y_dim_patch = 3 |
| 323 |
x_dim_patch = 4 |
| 324 |
frame = ndarray(shape=(y_dim, x_dim), dtype=int) |
| 325 |
for y in range(y_dim): |
| 326 |
for x in range(x_dim): |
| 327 |
frame[y, x] = 1000 * y + x |
| 328 |
patch = zeros((y_dim_patch * drizzle_factor, x_dim_patch * drizzle_factor), dtype=int) |
| 329 |
offset_counters = zeros(shape=(drizzle_factor, drizzle_factor), dtype=int) |
| 330 |
|
| 331 |
y_low = 10 |
| 332 |
y_high = y_low + y_dim_patch |
| 333 |
shift_y = 1.26 |
| 334 |
x_low = 6 |
| 335 |
x_high = x_low + x_dim_patch |
| 336 |
shift_x = -1.6 |
| 337 |
|
| 338 |
y_low_from, y_high_from, x_low_from, x_high_from, y_offset, x_offset = \ |
| 339 |
compute_bounds_2d(y_low, y_high, x_low, x_high, shift_y, shift_x, drizzle_factor) |
| 340 |
|
| 341 |
print("y_low_from: " + str(y_low_from) + ", y_high_from: " + str( |
| 342 |
y_high_from) + "\ny_offset: " + str(y_offset) + "\nx_low_from: " + str( |
| 343 |
x_low_from) + ", x_high_from: " + str(x_high_from) + "\nx_offset: " + str(x_offset)) |
| 344 |
|
| 345 |
remap_rigid_drizzled(frame, patch, offset_counters, shift_y, shift_x, y_low, y_high, x_low, |
| 346 |
x_high, drizzle_factor) |
| 347 |
|
| 348 |
print("patch: " + str(patch)) |
| 349 |
|
| 350 |
|
| 351 |
def test_equalize_ap_patch(): |
| 352 |
drizzle_factor = 3 |
| 353 |
y_dim = 3 |
| 354 |
x_dim = 4 |
| 355 |
y_dim_patch = y_dim * drizzle_factor |
| 356 |
x_dim_patch = x_dim * drizzle_factor |
| 357 |
patch = zeros(shape=(y_dim_patch, x_dim_patch), dtype=float) |
| 358 |
offset_counters = zeros(shape=(drizzle_factor, drizzle_factor), dtype=int) |
| 359 |
offset_counters[0, 0] = 1 |
| 360 |
offset_counters[0, 1] = 2 |
| 361 |
offset_counters[1, 0] = 1 |
| 362 |
offset_counters[2, 2] = 3 |
| 363 |
stack_size = offset_counters.sum() |
| 364 |
for y_offset in range(drizzle_factor): |
| 365 |
for x_offset in range(drizzle_factor): |
| 366 |
counter = offset_counters[y_offset, x_offset] |
| 367 |
if counter: |
| 368 |
for y in range(y_offset, y_dim_patch, drizzle_factor): |
| 369 |
for x in range(x_offset, x_dim_patch, drizzle_factor): |
| 370 |
patch[y, x] = counter * (1000 * y + x) |
| 371 |
|
| 372 |
|
| 373 |
print("patch before equalization: \n" + str(patch)) |
| 374 |
len_holes = equalize_ap_patch(patch, offset_counters, stack_size, drizzle_factor) |
| 375 |
print("patch after equalization: \n" + str(patch)) |
| 376 |
print("Number of holes in drizzle pattern: " + str(len_holes)) |
| 377 |
|
| 378 |
def test_numpy(): |
| 379 |
a = ndarray((4,5,3), dtype=int) |
| 380 |
for y in range(4): |
| 381 |
for x in range(5): |
| 382 |
for c in range(3): |
| 383 |
a[y, x, c] = y*100 + x*10 + c |
| 384 |
print("a: " + str(a)) |
| 385 |
|
| 386 |
b = a[2:3, 2:5] |
| 387 |
print ("b: " + str(b)) |
| 388 |
|
| 389 |
d = a[2:3, 2:5, :] |
| 390 |
print("d: " + str(d)) |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
test_equalize_ap_patch() |
| 398 |
|