From de0af5c156fddfd53bcab57892655cdddd130ce0 Mon Sep 17 00:00:00 2001 From: ncullen93 Date: Tue, 16 Apr 2024 13:39:45 +0200 Subject: [PATCH 1/2] support resampling multi-channel images --- ants/registration/resample_image.py | 19 ++++++++++++++++++- tests/test_registration.py | 7 +++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/ants/registration/resample_image.py b/ants/registration/resample_image.py index 0151b5e1..767110cf 100644 --- a/ants/registration/resample_image.py +++ b/ants/registration/resample_image.py @@ -40,6 +40,9 @@ def resample_image(image, resample_params, use_voxels=False, interp_type=1): >>> fi = ants.image_read( ants.get_ants_data("r16")) >>> finn = ants.resample_image(fi,(50,60),True,0) >>> filin = ants.resample_image(fi,(1.5,1.5),False,1) + >>> img = ants.image_read( ants.get_ants_data("r16")) + >>> img = ants.merge_channels([img, img]) + >>> outimg = ants.resample_image(img, (128,128), True) """ if image.components == 1: inimage = image.clone('float') @@ -53,7 +56,21 @@ def resample_image(image, resample_params, use_voxels=False, interp_type=1): outimage = outimage.clone(image.pixeltype) return outimage else: - raise ValueError('images with more than 1 component not currently supported') + images = utils.split_channels(image) + new_images = [] + for image in images: + inimage = image.clone('float') + outimage = image.clone('float') + rsampar = 'x'.join([str(rp) for rp in resample_params]) + + args = [image.dimension, inimage, outimage, rsampar, int(use_voxels), interp_type] + processed_args = utils._int_antsProcessArguments(args) + libfn = utils.get_lib_fn('ResampleImage') + libfn(processed_args) + outimage = outimage.clone(image.pixeltype) + new_images.append(outimage) + outimage = utils.merge_channels(new_images) + raise outimage def resample_image_to_target(image, target, interp_type='linear', imagetype=0, verbose=False, **kwargs): diff --git a/tests/test_registration.py b/tests/test_registration.py index 4037692b..a2cf3268 100644 --- a/tests/test_registration.py +++ b/tests/test_registration.py @@ -295,6 +295,13 @@ def test_resample_image_example(self): fi = ants.image_read(ants.get_ants_data("r16")) finn = ants.resample_image(fi, (50, 60), True, 0) filin = ants.resample_image(fi, (1.5, 1.5), False, 1) + + def test_resample_channels(self): + img = ants.image_read( ants.get_ants_data("r16")) + img = ants.merge_channels([img, img]) + outimg = ants.resample_image(img, (128,128), True) + self.assertEqual(outimg.shape, (128, 128)) + self.assertEqual(outimg.components, 2) def test_resample_image_to_target_example(self): fi = ants.image_read(ants.get_ants_data("r16")) From 6f4fa76f2d5d9168106b234ad7a25d00532f487c Mon Sep 17 00:00:00 2001 From: ncullen93 Date: Tue, 16 Apr 2024 16:54:09 +0200 Subject: [PATCH 2/2] fix typo --- ants/registration/resample_image.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ants/registration/resample_image.py b/ants/registration/resample_image.py index 767110cf..32ab42ad 100644 --- a/ants/registration/resample_image.py +++ b/ants/registration/resample_image.py @@ -70,7 +70,7 @@ def resample_image(image, resample_params, use_voxels=False, interp_type=1): outimage = outimage.clone(image.pixeltype) new_images.append(outimage) outimage = utils.merge_channels(new_images) - raise outimage + return outimage def resample_image_to_target(image, target, interp_type='linear', imagetype=0, verbose=False, **kwargs):