airlab-unibas / airlab

Image registration laboratory for 2D and 3D image data
Apache License 2.0
418 stars 93 forks source link

Jacobian for B-spline #40

Open Mikerov opened 2 years ago

Mikerov commented 2 years ago

Is there a way to calculate the determinant of the Jacobian to see whether the used B-spline transformation is volume preserving? What alternative methods can I use to ensure that there is no shrinkage?

koegl commented 5 months ago
import SimpleITK as sitk

def jacobian_determinant_from_displacement(displacement: np.ndarray) -> np.ndarray:

    displacement = displacement.squeeze()

    assert displacement.ndim == 4, "Displacement field should have shape (h, w, d, 3)"

    # transpose if necessary 
    if displacement.shape[-1] != 3 and displacement.shape[0] == 3:
        displacement = displacement.transpose(1, 2, 3, 0)

    displacement_image = sitk.GetImageFromArray(displacement, isVector=True)
    jacobian_determinant_image = sitk.DisplacementFieldJacobianDeterminant(
        displacement_image)
    return sitk.GetArrayFromImage(jacobian_determinant_image)