diff --git a/.github/workflows/test_readme_works.yml b/.github/workflows/test_readme_works.yml index b63515c..e1add7b 100644 --- a/.github/workflows/test_readme_works.yml +++ b/.github/workflows/test_readme_works.yml @@ -24,6 +24,7 @@ jobs: pip install -e . wget https://www.hgreer.com/assets/slicer_mirror/RegLib_C01_1.nrrd wget https://www.hgreer.com/assets/slicer_mirror/RegLib_C01_2.nrrd + wget https://www.hgreer.com/assets/RegLib_C01_1_foreground_mask.nii.gz - name: Test run: | unigradicon-register --fixed=RegLib_C01_2.nrrd --fixed_modality=mri --moving=RegLib_C01_1.nrrd --moving_modality=mri \ @@ -32,3 +33,6 @@ jobs: --transform_out=trans.hdf5 --warped_moving_out=warped_C01_1.nrrd --io_iterations=3 unigradicon-warp --fixed=RegLib_C01_2.nrrd --moving=RegLib_C01_1.nrrd \ --transform=trans.hdf5 --warped_moving_out=warped_2_C01_1.nrrd --nearest_neighbor + unigradicon-warp --fixed=RegLib_C01_2.nrrd --moving=RegLib_C01_1_foreground_mask.nii.gz \ + --transform=trans.hdf5 --warped_moving_out=warped_2_C01_1.nrrd --nearest_neighbor + diff --git a/src/unigradicon/__init__.py b/src/unigradicon/__init__.py index c862557..f7ffb43 100644 --- a/src/unigradicon/__init__.py +++ b/src/unigradicon/__init__.py @@ -337,7 +337,7 @@ def main(): itk.transformwrite([phi_AB], args.transform_out) if args.warped_moving_out: - moving = itk.CastImageFilter[type(moving), itk.Image[itk.F, 3]].New()(moving) + moving, maybe_cast_back = maybe_cast(moving) interpolator = itk.LinearInterpolateImageFunction.New(moving) warped_moving_image = itk.resample_image_filter( moving, @@ -346,6 +346,7 @@ def main(): use_reference_image=True, reference_image=fixed ) + warped_moving_image = maybe_cast_back(warped_moving_image) itk.imwrite(warped_moving_image, args.warped_moving_out) def warp_command(): @@ -370,6 +371,8 @@ def warp_command(): else: phi_AB = itk.transformread(args.transform)[0] + moving, maybe_cast_back = maybe_cast(moving) + if args.linear: interpolator = itk.LinearInterpolateImageFunction.New(moving) elif args.nearest_neighbor: @@ -385,6 +388,24 @@ def warp_command(): ) itk.imwrite(warped_moving_image, args.warped_moving_out) +def maybe_cast(img: itk.Image): + """ + If an itk image is of a type that can't be used with InterpolateImageFunctions, cast it + and be able to cast it back + """ + maybe_cast_back = lambda x: x + + if str((type(img), itk.D)) not in itk.NearestNeighborInterpolateImageFunction.GetTypesAsList(): + + if type(img) in (itk.Image[itk.ULL, 3], itk.Image[itk.UL, 3]): + raise Exception("Label maps of type unsigned long may have values that cannot be represented in a double") + + maybe_cast_back = itk.CastImageFilter[itk.Image[itk.D, 3], type(img)].New() + + img = itk.CastImageFilter[type(img), itk.Image[itk.D, 3]].New()(img) + + return img, maybe_cast_back + def compute_jacobian_map_command(): import itk import argparse