Skip to content

Commit d14dacd

Browse files
author
Nicholas Cullen, PhD
authored
Merge pull request #609 from ANTsX/resample-channels
ENH: support resampling multi-channel images
2 parents 54ec16e + 6f4fa76 commit d14dacd

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

ants/registration/resample_image.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ def resample_image(image, resample_params, use_voxels=False, interp_type=1):
4040
>>> fi = ants.image_read( ants.get_ants_data("r16"))
4141
>>> finn = ants.resample_image(fi,(50,60),True,0)
4242
>>> filin = ants.resample_image(fi,(1.5,1.5),False,1)
43+
>>> img = ants.image_read( ants.get_ants_data("r16"))
44+
>>> img = ants.merge_channels([img, img])
45+
>>> outimg = ants.resample_image(img, (128,128), True)
4346
"""
4447
if image.components == 1:
4548
inimage = image.clone('float')
@@ -53,7 +56,21 @@ def resample_image(image, resample_params, use_voxels=False, interp_type=1):
5356
outimage = outimage.clone(image.pixeltype)
5457
return outimage
5558
else:
56-
raise ValueError('images with more than 1 component not currently supported')
59+
images = utils.split_channels(image)
60+
new_images = []
61+
for image in images:
62+
inimage = image.clone('float')
63+
outimage = image.clone('float')
64+
rsampar = 'x'.join([str(rp) for rp in resample_params])
65+
66+
args = [image.dimension, inimage, outimage, rsampar, int(use_voxels), interp_type]
67+
processed_args = utils._int_antsProcessArguments(args)
68+
libfn = utils.get_lib_fn('ResampleImage')
69+
libfn(processed_args)
70+
outimage = outimage.clone(image.pixeltype)
71+
new_images.append(outimage)
72+
outimage = utils.merge_channels(new_images)
73+
return outimage
5774

5875

5976
def resample_image_to_target(image, target, interp_type='linear', imagetype=0, verbose=False, **kwargs):

tests/test_registration.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,13 @@ def test_resample_image_example(self):
295295
fi = ants.image_read(ants.get_ants_data("r16"))
296296
finn = ants.resample_image(fi, (50, 60), True, 0)
297297
filin = ants.resample_image(fi, (1.5, 1.5), False, 1)
298+
299+
def test_resample_channels(self):
300+
img = ants.image_read( ants.get_ants_data("r16"))
301+
img = ants.merge_channels([img, img])
302+
outimg = ants.resample_image(img, (128,128), True)
303+
self.assertEqual(outimg.shape, (128, 128))
304+
self.assertEqual(outimg.components, 2)
298305

299306
def test_resample_image_to_target_example(self):
300307
fi = ants.image_read(ants.get_ants_data("r16"))

0 commit comments

Comments
 (0)