| 1 |
from math import ceil |
| 2 |
from time import time |
| 3 |
|
| 4 |
from cv2 import resize, INTER_AREA, INTER_LINEAR, INTER_CUBIC, imshow, waitKey, destroyAllWindows, \ |
| 5 |
imread, IMREAD_GRAYSCALE, namedWindow, setWindowProperty, WND_PROP_FULLSCREEN, \ |
| 6 |
WINDOW_FULLSCREEN, GaussianBlur |
| 7 |
from numpy import arange, zeros, float32 |
| 8 |
|
| 9 |
from miscellaneous import Miscellaneous |
| 10 |
|
| 11 |
|
| 12 |
def drizzle_shift(): |
| 13 |
shifts = arange(-3., 3., 0.1, dtype=float) |
| 14 |
for shift in shifts: |
| 15 |
shift_ceil = ceil(shift / 2) * 2 |
| 16 |
frac = shift_ceil - shift - 1. / 3. |
| 17 |
frac_shift = int(round(frac * 3. / 2.)) |
| 18 |
|
| 19 |
print("shift: " + str(shift) + ", ceil: " + str(shift_ceil) + ", frac:" + str(frac_shift)) |
| 20 |
|
| 21 |
|
| 22 |
def drizzle_shift_patch(shift, ilo, ihi): |
| 23 |
frame = arange(30.) |
| 24 |
frame_d = zeros((45,)) |
| 25 |
|
| 26 |
shift_ceil_even = ceil((shift - 1. / 3.) / 2) * 2 |
| 27 |
frac = shift_ceil_even - shift |
| 28 |
frac_shift_even = int(round(frac * 3. / 2.)) |
| 29 |
shift_ceil_odd = ceil(shift / 2) * 2 |
| 30 |
frac = shift_ceil_odd - shift - 1. / 3. |
| 31 |
frac_shift_odd = int(round(frac * 3. / 2.)) |
| 32 |
if ilo % 2: |
| 33 |
frame_low_even = ilo + 1 + shift_ceil_even |
| 34 |
frame_high_even = ihi + shift_ceil_even |
| 35 |
frame_d_low_even = int((ilo + 1) / 2 * 3 + frac_shift_even) |
| 36 |
frame_d_high_even = int(round(ihi / 2 * 3)) + frac_shift_even |
| 37 |
frame_low_odd = ilo + shift_ceil_odd |
| 38 |
frame_high_odd = ihi + shift_ceil_odd |
| 39 |
frame_d_low_odd = int((ilo + 1. / 3.) / 2 * 3 + frac_shift_odd) |
| 40 |
frame_d_high_odd = int(round(ihi / 2 * 3)) + frac_shift_odd |
| 41 |
else: |
| 42 |
frame_low_even = ilo + shift_ceil_even |
| 43 |
frame_high_even = ihi + shift_ceil_even |
| 44 |
frame_d_low_even = int(ilo / 2 * 3 + frac_shift_even) |
| 45 |
frame_d_high_even = int(ihi / 2 * 3 + frac_shift_even) |
| 46 |
frame_low_odd = ilo + 1 + shift_ceil_odd |
| 47 |
frame_high_odd = ihi + shift_ceil_odd |
| 48 |
frame_d_low_odd = int((ilo + 4. / 3.) / 2 * 3 + frac_shift_odd) |
| 49 |
frame_d_high_odd = int(ihi / 2 * 3 + frac_shift_odd) |
| 50 |
frame_d[frame_d_low_even:frame_d_high_even:3] = frame[frame_low_even:frame_high_even:2] |
| 51 |
frame_d[frame_d_low_odd:frame_d_high_odd:3] = frame[frame_low_odd:frame_high_odd:2] |
| 52 |
|
| 53 |
pass |
| 54 |
|
| 55 |
|
| 56 |
def resize_test(image, drizzle_factor, inter, patch_size): |
| 57 |
width = int(image.shape[1] * drizzle_factor) |
| 58 |
height = int(image.shape[0] * drizzle_factor) |
| 59 |
dim = (width, height) |
| 60 |
time_before = time() |
| 61 |
rep_count = 100 |
| 62 |
for i in range(rep_count): |
| 63 |
resized = resize(image, dim, interpolation=inter) |
| 64 |
exec_time = (time() - time_before) / rep_count |
| 65 |
print("Time for one resize operation: " + str(exec_time) + ", method: " + |
| 66 |
str(["INTER_LINEAR", "INTER_CUBIC", "INTER_AREA"][inter - 1])) |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
start_y = 350 |
| 72 |
start_x = 500 |
| 73 |
patch = image[start_y:start_y + patch_size, start_x:start_x + patch_size] |
| 74 |
width = int(patch.shape[1] * drizzle_factor) |
| 75 |
height = int(patch.shape[0] * drizzle_factor) |
| 76 |
dim = (width, height) |
| 77 |
rep_count_patch = int( |
| 78 |
round(rep_count * image.shape[1] * image.shape[0] / (patch.shape[1] * patch.shape[0]))) |
| 79 |
time_before = time() |
| 80 |
for i in range(rep_count_patch): |
| 81 |
resized = resize(patch, dim, interpolation=inter) |
| 82 |
exec_time_patched = (time() - time_before) / rep_count |
| 83 |
factor = exec_time_patched / exec_time |
| 84 |
print("Time for one patched resize operation: " + str(exec_time_patched) + ", factor: " |
| 85 |
+ str(factor) + str("\n")) |
| 86 |
|
| 87 |
|
| 88 |
def show_image(window_name, image, fullscreen=False): |
| 89 |
if fullscreen: |
| 90 |
namedWindow(window_name, WND_PROP_FULLSCREEN) |
| 91 |
setWindowProperty(window_name, WND_PROP_FULLSCREEN, WINDOW_FULLSCREEN) |
| 92 |
imshow(window_name, image) |
| 93 |
waitKey() |
| 94 |
destroyAllWindows() |
| 95 |
|
| 96 |
|
| 97 |
def subpixel_shifted_frame(frame, subpixel_shift_y, subpixel_shift_x): |
| 98 |
interpolation_method = INTER_CUBIC |
| 99 |
undersampling_factor = 0.25 |
| 100 |
interpol_factor = 10 |
| 101 |
width = int(frame.shape[1] * interpol_factor) |
| 102 |
height = int(frame.shape[0] * interpol_factor) |
| 103 |
dim = (width, height) |
| 104 |
frame_extended = resize(frame, dim, interpolation=interpolation_method) |
| 105 |
print("shape original: " + str(frame.shape) + ", shape extended: " + str( |
| 106 |
frame_extended.shape)) |
| 107 |
shift_factor = interpol_factor / undersampling_factor |
| 108 |
shift_y = int(round(subpixel_shift_y * shift_factor)) |
| 109 |
shift_x = int(round(subpixel_shift_x * shift_factor)) |
| 110 |
border_y = abs(shift_y) |
| 111 |
border_x = abs(shift_x) |
| 112 |
|
| 113 |
frame_cropped_original = frame_extended[border_y:frame_extended.shape[0] - border_y, |
| 114 |
border_x:frame_extended.shape[1] - border_x] |
| 115 |
frame_cropped_shifted = frame_extended[ |
| 116 |
border_y + shift_y:frame_extended.shape[0] - border_y + shift_y, |
| 117 |
border_x + shift_x:frame_extended.shape[1] - border_x + shift_x] |
| 118 |
|
| 119 |
width = int(round(frame_cropped_original.shape[1] / shift_factor)) |
| 120 |
height = int(round(frame_cropped_original.shape[0] / shift_factor)) |
| 121 |
dim = (width, height) |
| 122 |
|
| 123 |
frame_resized = resize(frame_cropped_original, dim, interpolation=interpolation_method) |
| 124 |
frame_shifted = resize(frame_cropped_shifted, dim, interpolation=interpolation_method) |
| 125 |
|
| 126 |
print("shape resized: " + str(frame_resized.shape) + ", shape shifted: " + str( |
| 127 |
frame_shifted.shape)) |
| 128 |
return frame_resized, frame_shifted |
| 129 |
|
| 130 |
|
| 131 |
def drizzle_shift_test(): |
| 132 |
drizzle_shift() |
| 133 |
drizzle_shift_patch(-1.4, 2, 7) |
| 134 |
|
| 135 |
def interpolation_timing(): |
| 136 |
img = imread('../Images/Moon_Tile-024_043939_stacked_with_blurr_pp.tif', IMREAD_GRAYSCALE) |
| 137 |
|
| 138 |
|
| 139 |
drizzle_factor = 3. |
| 140 |
patch_size = 40 |
| 141 |
|
| 142 |
for inter in [INTER_AREA, INTER_LINEAR, INTER_CUBIC]: |
| 143 |
resize_test(img, drizzle_factor, inter, patch_size) |
| 144 |
|
| 145 |
def sub_pixel_shift_test(): |
| 146 |
img = imread('../Images/Moon_Tile-024_043939_stacked_with_blurr_pp.tif', IMREAD_GRAYSCALE) |
| 147 |
show_image("Original image", img, fullscreen=True) |
| 148 |
spx_shifty = 5.2 |
| 149 |
spx_shiftx = 3.5 |
| 150 |
|
| 151 |
img_resized, img_shifted = subpixel_shifted_frame(img, spx_shifty, spx_shiftx) |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
gauss_width_reference = 15 |
| 157 |
gauss_width_frame = 19 |
| 158 |
reference_frame_blurred_intermediate = GaussianBlur(img_resized, |
| 159 |
(gauss_width_reference, gauss_width_reference), |
| 160 |
0).astype(float32) |
| 161 |
reference_frame_blurred = GaussianBlur(reference_frame_blurred_intermediate, |
| 162 |
(gauss_width_reference, gauss_width_reference), 0).astype( |
| 163 |
float32) |
| 164 |
frame_blurred = GaussianBlur(img_shifted, (gauss_width_frame, gauss_width_frame), 0) |
| 165 |
|
| 166 |
y_ap = 170 |
| 167 |
x_ap = 200 |
| 168 |
half_box_width = 24 |
| 169 |
y_low = y_ap - half_box_width |
| 170 |
y_high = y_ap + half_box_width |
| 171 |
x_low = x_ap - half_box_width |
| 172 |
x_high = x_ap + half_box_width |
| 173 |
reference_box_second_phase = reference_frame_blurred[y_low: y_high, x_low: x_high] |
| 174 |
reference_box_first_phase = reference_box_second_phase[::2, ::2] |
| 175 |
|
| 176 |
search_width = 10 |
| 177 |
shift_y_local_first_phase, shift_x_local_first_phase, success_first_phase, \ |
| 178 |
shift_y_local_second_phase, shift_x_local_second_phase, success_second_phase = \ |
| 179 |
Miscellaneous.multilevel_correlation(reference_box_first_phase, frame_blurred, |
| 180 |
gauss_width_frame, |
| 181 |
reference_box_second_phase, y_low, y_high, x_low, x_high, |
| 182 |
search_width, |
| 183 |
weight_matrix_first_phase=None, subpixel_solve=True) |
| 184 |
|
| 185 |
print("Shift in y, first phase: " + str(shift_y_local_first_phase) + ", second phase: " + str( |
| 186 |
shift_y_local_second_phase) + ", total: " + str( |
| 187 |
shift_y_local_first_phase + shift_y_local_second_phase)) |
| 188 |
print("Shift in x, first phase: " + str(shift_x_local_first_phase) + ", second phase: " + str( |
| 189 |
shift_x_local_second_phase) + ", total: " + str( |
| 190 |
shift_x_local_first_phase + shift_x_local_second_phase)) |
| 191 |
|
| 192 |
|
| 193 |
interpolation_timing() |
| 194 |
|