Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: support resampling multi-channel images #609

Merged
merged 2 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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