Skip to content

Instantly share code, notes, and snippets.

@caniko
Created January 5, 2022 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caniko/d699dc280a478ca06c6710e39a8202e0 to your computer and use it in GitHub Desktop.
Save caniko/d699dc280a478ca06c6710e39a8202e0 to your computer and use it in GitHub Desktop.
Implementation of morphological interpolation along the z-axis for binarized images
from typing import Union
import itk
import numpy as np
from yaspin import yaspin
from yaspin.spinners import Spinners
INTERPOLATION_TEXT = "Interpolating for the first time"
@yaspin(Spinners.bouncingBall, text=INTERPOLATION_TEXT)
def morphological_interpolation(
labeled_stack: np.ndarray, stack_depth_spacing: Union[float, int]
):
spacing_array = np.zeros(
(round(stack_depth_spacing), *labeled_stack.shape[1:]),
dtype=labeled_stack.dtype,
)
spaced_stack = []
for xy_plane in labeled_stack:
spaced_stack.append(np.expand_dims(xy_plane, axis=0))
spaced_stack.append(spacing_array)
spaced_stack.pop(-1)
spaced_stack = np.concatenate(spaced_stack)
spaced_stack.astype(labeled_stack.dtype, copy=False)
return itk.GetArrayFromImage(
itk.morphological_contour_interpolator(itk.GetImageFromArray(spaced_stack))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment