Skip to content

Commit

Permalink
Merge pull request #609 from ANTsX/resample-channels
Browse files Browse the repository at this point in the history
ENH: support resampling multi-channel images
  • Loading branch information
ncullen93 committed Apr 16, 2024
2 parents 54ec16e + 6f4fa76 commit d14dacd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
19 changes: 18 additions & 1 deletion ants/registration/resample_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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)
return outimage


def resample_image_to_target(image, target, interp_type='linear', imagetype=0, verbose=False, **kwargs):
Expand Down
7 changes: 7 additions & 0 deletions tests/test_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down

0 comments on commit d14dacd

Please sign in to comment.