|
| 1 | +# |
| 2 | +# Copyright (c) 2022-2024 Vladislav Tsendrovskii |
| 3 | +# |
| 4 | +# This program is free software: you can redistribute it and/or modify |
| 5 | +# it under the terms of the GNU General Public License as published by |
| 6 | +# the Free Software Foundation, version 3 of the License. |
| 7 | +# This program is distributed in the hope that it will be useful, |
| 8 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 10 | +# See the GNU General Public License for more details. |
| 11 | +# You should have received a copy of the GNU General Public License |
| 12 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 13 | +# |
| 14 | + |
| 15 | +import numpy as np |
| 16 | +import scipy |
| 17 | + |
| 18 | +from vstarstack.library.fine_movement.module import ImageGrid |
| 19 | +from vstarstack.library.fine_movement.module import ImageDeform |
| 20 | +from vstarstack.library.fine_movement.module import ImageDeformLC |
| 21 | +from vstarstack.library.fine_movement.module import ImageDeformGC |
| 22 | + |
| 23 | +from vstarstack.library.data import DataFrame |
| 24 | + |
| 25 | +def _cluster_average(cluster): |
| 26 | + xs = [cluster[name]["x"] for name in cluster] |
| 27 | + ys = [cluster[name]["y"] for name in cluster] |
| 28 | + av = { |
| 29 | + "x" : sum(xs) / len(xs), |
| 30 | + "y" : sum(ys) / len(ys), |
| 31 | + } |
| 32 | + return av |
| 33 | + |
| 34 | +class Aligner: |
| 35 | + """Alignment applier""" |
| 36 | + |
| 37 | + def __init__(self, image_w : int, image_h : int, shift_array : np.ndarray): |
| 38 | + self.image_w = image_w |
| 39 | + self.image_h = image_h |
| 40 | + self.grid_h = shift_array.shape[0] |
| 41 | + self.grid_w = shift_array.shape[1] |
| 42 | + self.deform = ImageDeform(self.image_w, self.image_h, self.grid_w, self.grid_h) |
| 43 | + self.deform.fill(shift_array=shift_array) |
| 44 | + |
| 45 | + def apply_alignment(self, dataframe : DataFrame, subpixels : int): |
| 46 | + """Apply alignment descriptor to file""" |
| 47 | + for channel in dataframe.get_channels(): |
| 48 | + image, opts = dataframe.get_channel(channel) |
| 49 | + if opts["encoded"]: |
| 50 | + continue |
| 51 | + w = image.shape[1] |
| 52 | + h = image.shape[0] |
| 53 | + grid = ImageGrid(w, h) |
| 54 | + grid.fill(image.astype('double')) |
| 55 | + fixed_grid = self.deform.apply_image(image=grid, subpixels=subpixels) |
| 56 | + fixed = fixed_grid.content() |
| 57 | + fixed[np.where(np.isnan(fixed))] = 0 |
| 58 | + dataframe.replace_channel(fixed, channel) |
| 59 | + return dataframe |
| 60 | + |
| 61 | + def serialize(self): |
| 62 | + """Serialize image deform""" |
| 63 | + nitems = self.grid_h * self.grid_w * 2 |
| 64 | + shift_array = np.reshape(self.deform.content(), (nitems,)) |
| 65 | + return { |
| 66 | + "grid_w" : self.grid_w, |
| 67 | + "grid_h" : self.grid_h, |
| 68 | + "image_w" : self.image_w, |
| 69 | + "image_h" : self.image_h, |
| 70 | + "array" : list(shift_array), |
| 71 | + } |
| 72 | + |
| 73 | + @staticmethod |
| 74 | + def deserialize(description : dict): |
| 75 | + """Deserialize image deform""" |
| 76 | + grid_h = description["grid_h"] |
| 77 | + grid_w = description["grid_w"] |
| 78 | + shift_array = np.reshape(description["array"], (grid_h, grid_w, 2)) |
| 79 | + return Aligner(description["image_w"], description["image_h"], shift_array) |
| 80 | + |
| 81 | +class ClusterAlignerBuilder: |
| 82 | + def __init__(self, image_w, image_h, grid_w, grid_h, spk, num_steps, min_points, dh): |
| 83 | + self.image_w = image_w |
| 84 | + self.image_h = image_h |
| 85 | + self.grid_w = grid_w |
| 86 | + self.grid_h = grid_h |
| 87 | + self.spk = spk |
| 88 | + self.num_steps = num_steps |
| 89 | + self.min_points = min_points |
| 90 | + self.dh = dh |
| 91 | + self.correlator = ImageDeformGC(self.image_w, self.image_h, |
| 92 | + self.grid_w, self.grid_h, |
| 93 | + self.spk) |
| 94 | + |
| 95 | + def find_alignment(self, name : str, clusters : list) -> Aligner: |
| 96 | + """Find alignment of image `name` using clusters""" |
| 97 | + expected_points = [] |
| 98 | + actual_points = [] |
| 99 | + for cluster in clusters: |
| 100 | + if name not in cluster: |
| 101 | + continue |
| 102 | + average = _cluster_average(cluster) |
| 103 | + |
| 104 | + # we need reverse transformation |
| 105 | + x = average["x"] |
| 106 | + y = average["y"] |
| 107 | + expected_points.append((x, y)) |
| 108 | + x = cluster[name]["x"] |
| 109 | + y = cluster[name]["y"] |
| 110 | + actual_points.append((x, y)) |
| 111 | + |
| 112 | + print(f"\tusing {len(expected_points)} points") |
| 113 | + if len(expected_points) < self.min_points: |
| 114 | + print("\tskip - too low points") |
| 115 | + return None |
| 116 | + |
| 117 | + deform = self.correlator.find(points=actual_points, |
| 118 | + expected_points=expected_points, |
| 119 | + dh=self.dh, |
| 120 | + Nsteps=self.num_steps) |
| 121 | + shift_array = deform.content() |
| 122 | + return Aligner(self.image_w, self.image_h, shift_array) |
| 123 | + |
| 124 | + def find_all_alignments(self, clusters : list) -> dict: |
| 125 | + """Build alignment descriptor using clusters""" |
| 126 | + names = [] |
| 127 | + for cluster in clusters: |
| 128 | + names += cluster.keys() |
| 129 | + names = set(names) |
| 130 | + deforms = {} |
| 131 | + for name in names: |
| 132 | + deform = self.find_alignment(name, clusters) |
| 133 | + if deform is not None: |
| 134 | + deforms[name] = deform |
| 135 | + return deforms |
| 136 | + |
| 137 | +class CorrelationAlignedBuilder: |
| 138 | + |
| 139 | + def __init__(self, |
| 140 | + image_w : int, |
| 141 | + image_h : int, |
| 142 | + pixels : int, |
| 143 | + radius : int, |
| 144 | + maximal_shift : float, |
| 145 | + subpixels : int): |
| 146 | + self.image_w = image_w |
| 147 | + self.image_h = image_h |
| 148 | + self.pixels = pixels |
| 149 | + self.correlator = ImageDeformLC(self.image_w, self.image_h, self.pixels) |
| 150 | + |
| 151 | + self.radius = radius |
| 152 | + self.max_shift = maximal_shift |
| 153 | + self.subpixels = subpixels |
| 154 | + |
| 155 | + def find_alignment(self, image : np.ndarray, |
| 156 | + image_ref : np.ndarray, |
| 157 | + pre_align : Aligner | None, |
| 158 | + pre_align_ref : Aligner | None, |
| 159 | + smooth : int | None) -> Aligner: |
| 160 | + """Build alignment descriptor of image using correlations""" |
| 161 | + if image.shape != image_ref.shape: |
| 162 | + return None |
| 163 | + w = image.shape[1] |
| 164 | + h = image.shape[0] |
| 165 | + |
| 166 | + grid = ImageGrid(w, h) |
| 167 | + grid.fill(image) |
| 168 | + grid_ref = ImageGrid(w, h) |
| 169 | + grid_ref.fill(image_ref) |
| 170 | + |
| 171 | + deform_img = pre_align.deform if pre_align is not None else None |
| 172 | + deform_ref = pre_align_ref.deform if pre_align_ref is not None else None |
| 173 | + deform = self.correlator.find(grid, deform_img, |
| 174 | + grid_ref, deform_ref, |
| 175 | + self.radius, |
| 176 | + self.max_shift, |
| 177 | + self.subpixels) |
| 178 | + print(f"smooth = {smooth}") |
| 179 | + if smooth is not None: |
| 180 | + data = deform.content() |
| 181 | + data = scipy.ndimage.gaussian_filter(data, sigma=smooth, axes=(0,1)) |
| 182 | + deform.fill(data) |
| 183 | + shift_array = deform.content() |
| 184 | + return Aligner(self.image_w, self.image_h, shift_array) |
0 commit comments