Shape corrections (darsia.corrections.shape)#
Shape corrections target geometrical transformations as translations, rotations, but also locally varying bulge and stretch as well as general deformation resulting from direct comparison with a reference image.
Curvature correction (darsia.corrections.shape.curvature)#
Module containing polynomical Curvature correction.
- class CurvatureCorrection(config=None, **kwargs)[source]#
Bases:
BaseCorrectionPolynomial curvature correction.
Contains routines for setting up the curvature correction, as well as applying it to images.
- config#
config dictionary for curvture correction.
- Type:
dict
- Circumstantial attributes
reference_image (np.ndarray): image matrix of the reference image. current_image (np.ndarray): image matrix of the updated reference image. width (float): physical width of reference image. height (float): physical height of reference image. in_meters (bool): True if width/height is in meters. Ny (int): number of pixels in vertical direction in reference image. Nx (int): number of pixels in horizontal direction in reference image.
- bulge_correction(left=0, right=0, top=0, bottom=0)[source]#
Bulge correction
Corrects bulging of image, depending on the amount of pixels that the image is bulged inwards on each side.
- Parameters:
left (int) – amount of bulged pixels on the left side of the image.
right (int) – amount of bulged pixels on the right side of the image.
top (int) – amount of bulged pixels on the top of the image.
bottom (int) – amount of bulged pixels on the bottom of the image.
- compute_bulge(img=None, **kwargs)[source]#
Compute the bulge parameters depending on the maximum number of pixels that the image has been displaced on each side.
- Parameters:
img (np.ndarray, optional) – image array, basis for the computation.
kwargs (optional keyword arguments) –
- “left” (int): the maximum number of pixels that the image
has been displaced on the left side
- ”right” (int): the maximum number of pixels that the image
has been displaced on the right side
- ”top” (int): the maximum number of pixels that the image
has been displaced on the top
- ”bottom” (int): the maximum number of pixels that the image
has been displaced on the bottom
- compute_stretch(img=None, **kwargs)[source]#
Compute the stretch parameters depending on the stretch center, and a known translation.
- Parameters:
img (np.ndarray, optional) – image array, basis for the computation.
kwargs (optional keyword arguments) – “point_source” (list): point that has been translated. “point_destination” (list): the ought to be position. “stretch_center” (list): the stretch center.
- correct_array(img)[source]#
Call method of the curvature correction.
Applies the curvature correction to a provided image, and returns the corrected image as an array. If set in the constructor, the image will be resized in the first step.
- Parameters:
img (np.ndarray) – image array
- Returns:
curvature corrected image.
- Return type:
np.ndarray
- correct_metadata(metadata={})[source]#
Extract metadata from the config file.
- Parameters:
metadata (dict, optional) – metadata dictionary to be updated. Defaults to {}.
- Returns:
metadata
- Return type:
dict
- crop(corner_points)[source]#
Crop the image along the corners of the image.
The four corner points of the image should be provided, and this method will update the config file and modify the current image.
- Parameters:
corner_points (VoxelArray) – list of the corner points. Preferably the list should be ordered starting from the upper left corner and going counter clockwise.
- load(path)[source]#
Load the curvature correction from a file.
- Parameters:
path (Path) – path to the file
- pre_bulge_correction(**kwargs)[source]#
Initialize the curvature correction by forcing all stright lines to curve inwards and not outwards.
- Parameters:
kwargs (optional keyword arguments) –
- “horizontal_bulge” (float): parameter for the curvature correction related to
the horizontal bulge of the image.
- ”horizontal_center_offset” (int): offset in terms of pixel of the image center
in x-direction, as compared to the numerical center
- vertical_bulge (float): parameter for the curvature correction related to the
vertical bulge of the image.
- ”vertical_center_offset” (int): offset in terms of pixel of the image center in
y-direction, as compared to the numerical center
- read_config_from_file(path)[source]#
Reads a json-file to the config disctionary.
- Parameters:
path (Path) – path to the json-file.
- return_image()[source]#
Returns the current image as a darsia image width provided width and height.
- save(path)[source]#
Save the curvature correction to a file.
- Parameters:
path (Path) – path to the file
- simple_curvature_correction(img, **kwargs)[source]#
General routine for applying stretch and bulge transforms. This routine in contrast to __call__ does always use the keyword arguments and constructs the transformation instead of using cached values.
- Parameters:
img (np.ndarray) – image array
kwargs (optional keyword arguments) – see _transform_coordinates for more details.
- Returns:
corrected image
- Return type:
np.ndarray
- stretch_correction(point_source, point_destination, stretch_center)[source]#
Stretch correction.
Stretches the image depending on the displacement of a single point (point source <–> point_destination) and an undisplaced point (stretch_center)
- Parameters:
"point_source" (list) – point that has been translated.
"point_destination" (list) – the ought to be position.
"stretch_center" (list) – the stretch center.
- write_config_to_file(path)[source]#
Writes the config dictionary to a json-file.
- Parameters:
path (Path) – path to the json file
- property temporary_image#
Local deformation correction (darsia.corrections.shape.deformation)#
Module containing a local deformation correction.
This is essentially a correction object through wrappping
darsia.DiffeomorphicImageRegistration.
- class DeformationCorrection(base, config)[source]#
Bases:
BaseCorrectionPatch-wise defined deformation correction defined through image registration.
- base#
reference (baseline) image.
- Type:
darsia.Image
- correct_array(img)[source]#
Main routine for aligning image with baseline image.
- Parameters:
img (np.ndarray) – input image, to be aligned.
- Returns:
aligned image array.
- Return type:
np.ndarray
- correct_metadata(metadata={})#
Correction routine on metadata level.
- Parameters:
metadata (dict) – metadata dictionary.
- Returns:
corrected metadata dictionary.
- Return type:
dict
- abstractmethod load(path)#
Load the correction from a file.
The method should load a npz file, containing the class name and required data for loading the correction from file.
- Parameters:
path (str) – path to the file
- abstractmethod save(path)#
Save the correction to a file.
The method should store a npz file, continaing the class name and required data for loading the correction from file.
- Parameters:
path (str) – path to the file
Drift correction (darsia.corrections.shape.drift)#
Module containing objects to align images with respect to a baseline image.
The alignment is based on feature detection. It is possible to use segments of the image. Finally, corrections are based on single, global translation.
- class DriftCorrection(base=None, config=None)[source]#
Bases:
BaseCorrectionClass for drift correction of images wrt. a baseline image.
- correct_array(img, roi=None)[source]#
Main routine for aligning image with baseline image.
- Parameters:
img (np.ndarray) – input image, to be aligned.
roi (2-tuple of slices, optional) – ROI to be applied to img; if None the cached roi is used.
- Returns:
aligned image array.
- Return type:
np.ndarray
- correct_metadata(metadata={})#
Correction routine on metadata level.
- Parameters:
metadata (dict) – metadata dictionary.
- Returns:
corrected metadata dictionary.
- Return type:
dict
- base#
Base image array.
- translation_estimator#
Detection of effective translation based on feature detection.
Piecewise perspective transformation (darsia.corrections.shape.piecewiseperspective)#
Module defining how to determine and apply patch-wise defined perspective transform.
- class PiecewisePerspectiveTransform(**kwargs)[source]#
Bases:
objectGlobally constant, patch-wise defined perspective transform.
- find_and_warp(patches, displacement, reverse=False)[source]#
Continuously transform entire image via patchwise perspective transform.
Perspective transforms determined by the function evaluation in all corners of each patch results in continuous transformations. Hence, stiching together the transformed images results in an image without overlap and gaps.
- Parameters:
patches (darsia.Patches) – patched image
displacement (Callable) – relative deformation map; assumed to be continuous
reverse (bool) – flag whether displacement is applied with negative weight
- Returns:
transformed image
- Return type:
darsia.Image
Rotation correction (darsia.corrections.shape.rotation)#
Module containing objects useful for correcting images wrt rotations
- class RotationCorrection(anchor, **kwargs)[source]#
Bases:
BaseCorrectionRotation correction.
Rotations are defined as combination of multiple basic rotations. In 2d, a single basic rotation is sufficient. In 3d, although three are available, two are sufficient.
- dim#
ambient dimension
- Type:
int
- anchor#
voxel coordinates of anchor
- Type:
array or list
- rotation#
rotation matrix
- Type:
array
- rotation_inv#
inverted rotation matrix
- Type:
array
- correct_array(img)[source]#
Main routine: Application of inherent rotation to provided image.
- Parameters:
img (array) – image
- Returns:
rotated image
- Return type:
array
- correct_metadata(metadata={})#
Correction routine on metadata level.
- Parameters:
metadata (dict) – metadata dictionary.
- Returns:
corrected metadata dictionary.
- Return type:
dict
Translation correction (darsia.corrections.shape.translation)#
Module defining estimator for translation between ROIs and corresponding correction.
- class TranslationCorrection(translation=None)[source]#
Bases:
BaseCorrectionTranslation correction based on user-input global translation.
Translation is provided as array or path to numpy array.
- correct_array(img)[source]#
Main routine. Perform translation.
- Parameters:
img (np.ndarray) – image to be corrected.
- Returns:
Corrected image
- Return type:
array
- correct_metadata(metadata={})#
Correction routine on metadata level.
- Parameters:
metadata (dict) – metadata dictionary.
- Returns:
corrected metadata dictionary.
- Return type:
dict
- active#
Flag controlling whether the correction is active.
- translation#
Translation map/array.
- class TranslationEstimator(max_features=200, tol=0.05, keep_percent=0.1)[source]#
Bases:
objectEstimator of translation to align images based on feature detection.
Estimation is performed by restriction to ROIs.
- find_effective_translation(img_src, img_dst, roi_src=None, roi_dst=None, mask_src=None, mask_dst=None, plot_matches=False)[source]#
Find translation to align two images through feature matching.
All lengths are measured in number of pixels.
- Parameters:
img_src (np.ndarray) – source image
img_dst (np.ndarray) – destination image
roi_src (tuple of slices) – region of interested associated to the source image
roi_dst (tuple of slices) – region of interested associated to the destination image
mask_src (np.ndarray, optional) – boolean mask detecting considered pixels in the analysis; if None, all pixels are considered.
mask_dst (np.ndarray, optional) – boolean mask detecting considered pixels in the analysis; if None, all pixels are considered.
plot_matches (bool) – flag controlling whether the matching features are plotted; useful for debugging; default value is False
- Returns:
- transformation matrix operating on pixel coordinates using reverse
matrix indexing
bool: flag indicating whether the procedure was successful
- Return type:
np.ndarray
- match_roi(img_src, img_dst, roi_src=None, roi_dst=None, plot_matches=False)[source]#
Align two images through feature matching in provided ROIs.
- Parameters:
img_src (np.ndarray or darsia.Image) – source image
img_dst (np.ndarray or darsia.Image) – destination image
roi_src (tuple of slices) – region of interested associated to the source image
roi_dst (tuple of slices) – region of interested associated to the destination image translation
plot_matches (bool) – flag controlling whether the matching features are plotted; useful for debugging; default value is False
- Returns:
aligned source image, if input of type array
- Return type:
np.ndarray, optional