nanopyx.methods.drift_alignment.corrector

  1from .estimator_table import DriftEstimatorTable
  2from ...core.utils.timeit import timeit
  3from ...core.transform._le_interpolation_bicubic import ShiftAndMagnify
  4
  5import cv2
  6import numpy as np
  7from skimage.transform import EuclideanTransform, warp
  8
  9# TODO cv2 to LE
 10
 11
 12class DriftCorrector(object):
 13    """
 14    Main class for aligning timelapse images with drift.
 15
 16    This class is used for aligning timelapse images with drift correction. It requires a previously calculated drift table.
 17    The class implements the following methods:
 18    - apply_correction
 19    - load_estimator_table
 20    - _translate_slice
 21
 22    Args:
 23        None
 24
 25    Attributes:
 26        estimator_table (DriftEstimatorTable): An instance of the DriftEstimatorTable containing the drift table data.
 27        image_arr (numpy.ndarray): The timelapse image array with shape (n_slices, rows, columns).
 28
 29    Methods:
 30        __init__(): Initialize the `DriftCorrector` object.
 31
 32        _translate_slice(slice_idx): Translate an individual image slice based on the drift table.
 33
 34        apply_correction(image_array): Apply drift correction to the entire image array.
 35
 36        load_estimator_table(path=None): Load the drift table from a file.
 37
 38    Example:
 39        corrector = DriftCorrector()
 40        corrected_image = corrector.apply_correction(image_array)
 41        corrector.load_estimator_table("drift_table.csv")
 42
 43    Note:
 44        The `DriftCorrector` class is used for correcting drift in timelapse images using a precomputed drift table.
 45    """
 46
 47    def __init__(self):
 48        """
 49        Initialize the `DriftCorrector` object.
 50
 51        Args:
 52            None
 53
 54        Returns:
 55            None
 56
 57        Example:
 58            corrector = DriftCorrector()
 59        """
 60        self.estimator_table = DriftEstimatorTable()
 61        self.image_arr = None
 62
 63    def _translate_slice(self, slice_idx):
 64        """
 65        Translate an individual image slice based on the drift table.
 66
 67        Args:
 68            slice_idx (int): The index of the slice to be translated.
 69
 70        Returns:
 71            numpy.ndarray: The translated image slice.
 72
 73        Example:
 74            translated_slice = self._translate_slice(0)
 75
 76        Note:
 77            This method is used to translate individual image slices based on the drift information in the drift table.
 78        """
 79        drift_x = self.estimator_table.drift_table[slice_idx][1]
 80        drift_y = self.estimator_table.drift_table[slice_idx][2]
 81
 82        if drift_x == 0 and drift_y == 0:
 83            return self.image_arr[slice_idx]
 84        else:
 85            return cv2.warpAffine(
 86                self.image_arr[slice_idx].astype(np.float32),
 87                np.float32([[1, 0, drift_x], [0, 1, drift_y]]),
 88                self.image_arr[slice_idx].shape[:2][::-1],
 89            ).astype(self.image_arr.dtype)
 90
 91    # @timeit
 92    def apply_correction(self, image_array):
 93        """
 94        Apply drift correction to the entire image array.
 95
 96        Args:
 97            image_array (numpy.ndarray): The input image array with shape (n_slices, rows, columns).
 98
 99        Returns:
100            numpy.ndarray: The aligned image array with shape (n_slices, rows, columns).
101
102        Example:
103            corrected_image = self.apply_correction(image_array)
104
105        Note:
106            This is the main method of the `DriftCorrector` class, which applies drift correction to the entire image array.
107        """
108        if self.estimator_table.drift_table is not None:
109            self.image_arr = image_array
110            corrected_image = [self._translate_slice(i).astype(np.float32) for i in range(0, image_array.shape[0])]
111            return np.array(corrected_image)
112            # return np.array(translation.translate_array(image_array.astype(np.float32),
113            #                                             np.array(self.estimator_table.drift_table).astype(np.float32)))
114
115        else:
116            print("Missing drift calculation")
117            return None
118
119    def load_estimator_table(self, path=None):
120        """
121        Load the drift table from a file.
122
123        Args:
124            path (str, optional): The path to the drift table file (CSV or NPY format). Default is None.
125
126        Returns:
127            None
128
129        Example:
130            self.load_estimator_table("drift_table.csv")
131
132        Note:
133            This method is used to load the drift table data from a file into the `estimator_table` attribute.
134        """
135        if path is None:
136            path = input("Please provide a filepath to the drift table")
137
138        if path.split(".")[-1] == "npy":
139            self.estimator_table.import_npy(path)
140        elif path.split(".")[-1] == "csv":
141            self.estimator_table.import_csv(path)
class DriftCorrector:
 13class DriftCorrector(object):
 14    """
 15    Main class for aligning timelapse images with drift.
 16
 17    This class is used for aligning timelapse images with drift correction. It requires a previously calculated drift table.
 18    The class implements the following methods:
 19    - apply_correction
 20    - load_estimator_table
 21    - _translate_slice
 22
 23    Args:
 24        None
 25
 26    Attributes:
 27        estimator_table (DriftEstimatorTable): An instance of the DriftEstimatorTable containing the drift table data.
 28        image_arr (numpy.ndarray): The timelapse image array with shape (n_slices, rows, columns).
 29
 30    Methods:
 31        __init__(): Initialize the `DriftCorrector` object.
 32
 33        _translate_slice(slice_idx): Translate an individual image slice based on the drift table.
 34
 35        apply_correction(image_array): Apply drift correction to the entire image array.
 36
 37        load_estimator_table(path=None): Load the drift table from a file.
 38
 39    Example:
 40        corrector = DriftCorrector()
 41        corrected_image = corrector.apply_correction(image_array)
 42        corrector.load_estimator_table("drift_table.csv")
 43
 44    Note:
 45        The `DriftCorrector` class is used for correcting drift in timelapse images using a precomputed drift table.
 46    """
 47
 48    def __init__(self):
 49        """
 50        Initialize the `DriftCorrector` object.
 51
 52        Args:
 53            None
 54
 55        Returns:
 56            None
 57
 58        Example:
 59            corrector = DriftCorrector()
 60        """
 61        self.estimator_table = DriftEstimatorTable()
 62        self.image_arr = None
 63
 64    def _translate_slice(self, slice_idx):
 65        """
 66        Translate an individual image slice based on the drift table.
 67
 68        Args:
 69            slice_idx (int): The index of the slice to be translated.
 70
 71        Returns:
 72            numpy.ndarray: The translated image slice.
 73
 74        Example:
 75            translated_slice = self._translate_slice(0)
 76
 77        Note:
 78            This method is used to translate individual image slices based on the drift information in the drift table.
 79        """
 80        drift_x = self.estimator_table.drift_table[slice_idx][1]
 81        drift_y = self.estimator_table.drift_table[slice_idx][2]
 82
 83        if drift_x == 0 and drift_y == 0:
 84            return self.image_arr[slice_idx]
 85        else:
 86            return cv2.warpAffine(
 87                self.image_arr[slice_idx].astype(np.float32),
 88                np.float32([[1, 0, drift_x], [0, 1, drift_y]]),
 89                self.image_arr[slice_idx].shape[:2][::-1],
 90            ).astype(self.image_arr.dtype)
 91
 92    # @timeit
 93    def apply_correction(self, image_array):
 94        """
 95        Apply drift correction to the entire image array.
 96
 97        Args:
 98            image_array (numpy.ndarray): The input image array with shape (n_slices, rows, columns).
 99
100        Returns:
101            numpy.ndarray: The aligned image array with shape (n_slices, rows, columns).
102
103        Example:
104            corrected_image = self.apply_correction(image_array)
105
106        Note:
107            This is the main method of the `DriftCorrector` class, which applies drift correction to the entire image array.
108        """
109        if self.estimator_table.drift_table is not None:
110            self.image_arr = image_array
111            corrected_image = [self._translate_slice(i).astype(np.float32) for i in range(0, image_array.shape[0])]
112            return np.array(corrected_image)
113            # return np.array(translation.translate_array(image_array.astype(np.float32),
114            #                                             np.array(self.estimator_table.drift_table).astype(np.float32)))
115
116        else:
117            print("Missing drift calculation")
118            return None
119
120    def load_estimator_table(self, path=None):
121        """
122        Load the drift table from a file.
123
124        Args:
125            path (str, optional): The path to the drift table file (CSV or NPY format). Default is None.
126
127        Returns:
128            None
129
130        Example:
131            self.load_estimator_table("drift_table.csv")
132
133        Note:
134            This method is used to load the drift table data from a file into the `estimator_table` attribute.
135        """
136        if path is None:
137            path = input("Please provide a filepath to the drift table")
138
139        if path.split(".")[-1] == "npy":
140            self.estimator_table.import_npy(path)
141        elif path.split(".")[-1] == "csv":
142            self.estimator_table.import_csv(path)

Main class for aligning timelapse images with drift.

This class is used for aligning timelapse images with drift correction. It requires a previously calculated drift table. The class implements the following methods:

  • apply_correction
  • load_estimator_table
  • _translate_slice

Args: None

Attributes: estimator_table (DriftEstimatorTable): An instance of the DriftEstimatorTable containing the drift table data. image_arr (numpy.ndarray): The timelapse image array with shape (n_slices, rows, columns).

Methods: __init__(): Initialize the DriftCorrector object.

_translate_slice(slice_idx): Translate an individual image slice based on the drift table.

apply_correction(image_array): Apply drift correction to the entire image array.

load_estimator_table(path=None): Load the drift table from a file.

Example: corrector = DriftCorrector() corrected_image = corrector.apply_correction(image_array) corrector.load_estimator_table("drift_table.csv")

Note: The DriftCorrector class is used for correcting drift in timelapse images using a precomputed drift table.

DriftCorrector()
48    def __init__(self):
49        """
50        Initialize the `DriftCorrector` object.
51
52        Args:
53            None
54
55        Returns:
56            None
57
58        Example:
59            corrector = DriftCorrector()
60        """
61        self.estimator_table = DriftEstimatorTable()
62        self.image_arr = None

Initialize the DriftCorrector object.

Args: None

Returns: None

Example: corrector = DriftCorrector()

estimator_table
image_arr
def apply_correction(self, image_array):
 93    def apply_correction(self, image_array):
 94        """
 95        Apply drift correction to the entire image array.
 96
 97        Args:
 98            image_array (numpy.ndarray): The input image array with shape (n_slices, rows, columns).
 99
100        Returns:
101            numpy.ndarray: The aligned image array with shape (n_slices, rows, columns).
102
103        Example:
104            corrected_image = self.apply_correction(image_array)
105
106        Note:
107            This is the main method of the `DriftCorrector` class, which applies drift correction to the entire image array.
108        """
109        if self.estimator_table.drift_table is not None:
110            self.image_arr = image_array
111            corrected_image = [self._translate_slice(i).astype(np.float32) for i in range(0, image_array.shape[0])]
112            return np.array(corrected_image)
113            # return np.array(translation.translate_array(image_array.astype(np.float32),
114            #                                             np.array(self.estimator_table.drift_table).astype(np.float32)))
115
116        else:
117            print("Missing drift calculation")
118            return None

Apply drift correction to the entire image array.

Args: image_array (numpy.ndarray): The input image array with shape (n_slices, rows, columns).

Returns: numpy.ndarray: The aligned image array with shape (n_slices, rows, columns).

Example: corrected_image = self.apply_correction(image_array)

Note: This is the main method of the DriftCorrector class, which applies drift correction to the entire image array.

def load_estimator_table(self, path=None):
120    def load_estimator_table(self, path=None):
121        """
122        Load the drift table from a file.
123
124        Args:
125            path (str, optional): The path to the drift table file (CSV or NPY format). Default is None.
126
127        Returns:
128            None
129
130        Example:
131            self.load_estimator_table("drift_table.csv")
132
133        Note:
134            This method is used to load the drift table data from a file into the `estimator_table` attribute.
135        """
136        if path is None:
137            path = input("Please provide a filepath to the drift table")
138
139        if path.split(".")[-1] == "npy":
140            self.estimator_table.import_npy(path)
141        elif path.split(".")[-1] == "csv":
142            self.estimator_table.import_csv(path)

Load the drift table from a file.

Args: path (str, optional): The path to the drift table file (CSV or NPY format). Default is None.

Returns: None

Example: self.load_estimator_table("drift_table.csv")

Note: This method is used to load the drift table data from a file into the estimator_table attribute.