[5d12a0]: / ants / contrib / sampling / transforms.py

Download this file

767 lines (649 with data), 24.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
"""
Various data augmentation transforms for ANTsImage types
List of Transformations:
======================
- CastIntensity
- BlurIntensity
- NormalizeIntensity
- RescaleIntensity
- ShiftScaleIntensity
- SigmoidIntensity
======================
- FlipImage
- TranslateImage
TODO
----
- RotateImage
- ShearImage
- ScaleImage
- DeformImage
- PadImage
- HistogramEqualizeIntensity
- TruncateIntensity
- SharpenIntensity
- MorpholigicalIntensity
- MD
- ME
- MO
- MC
- GD
- GE
- GO
- GC
"""
__all__ = ['CastIntensity',
'BlurIntensity',
'LocallyBlurIntensity',
'NormalizeIntensity',
'RescaleIntensity',
'ShiftScaleIntensity',
'SigmoidIntensity',
'FlipImage',
'ScaleImage',
'TranslateImage',
'MultiResolutionImage']
from ... import utils
from ...core import ants_image as iio
class MultiResolutionImage(object):
"""
Generate a set of images at multiple resolutions from an original image
"""
def __init__(self, levels=4, keep_shape=False):
self.levels = levels
self.keep_shape = keep_shape
def transform(self, X, y=None):
"""
Generate a set of multi-resolution ANTsImage types
Arguments
---------
X : ANTsImage
image to transform
y : ANTsImage (optional)
another image to transform
Example
-------
>>> import ants
>>> multires = ants.contrib.MultiResolutionImage(levels=4)
>>> img = ants.image_read(ants.get_data('r16'))
>>> imgs = multires.transform(img)
"""
insuffix = X._libsuffix
multires_fn = utils.get_lib_fn('multiResolutionAntsImage%s' % (insuffix))
casted_ptrs = multires_fn(X.pointer, self.levels)
imgs = []
for casted_ptr in casted_ptrs:
img = iio.ANTsImage(pixeltype=X.pixeltype, dimension=X.dimension,
components=X.components, pointer=casted_ptr)
if self.keep_shape:
img = img.resample_image_to_target(X)
imgs.append(img)
return imgs
## Intensity Transforms ##
class CastIntensity(object):
"""
Cast the pixeltype of an ANTsImage to a given type.
This code uses the C++ ITK library directly, so it is fast.
NOTE: This offers a ~2.5x speedup over using img.clone(pixeltype):
Timings vs Cloning
------------------
>>> import ants
>>> import time
>>> caster = ants.contrib.CastIntensity('float')
>>> img = ants.image_read(ants.get_data('mni')).clone('unsigned int')
>>> s = time.time()
>>> for i in range(1000):
... img_float = caster.transform(img)
>>> e = time.time()
>>> print(e - s) # 9.6s
>>> s = time.time()
>>> for i in range(1000):
... img_float = img.clone('float')
>>> e = time.time()
>>> print(e - s) # 25.3s
"""
def __init__(self, pixeltype):
"""
Initialize a CastIntensity transform
Arguments
---------
pixeltype : string
pixeltype to which images will be casted
Example
-------
>>> import ants
>>> caster = ants.contrib.CastIntensity('float')
"""
self.pixeltype = pixeltype
def transform(self, X, y=None):
"""
Transform an image by casting its type
Arguments
---------
X : ANTsImage
image to cast
y : ANTsImage (optional)
another image to cast.
Example
-------
>>> import ants
>>> caster = ants.contrib.CastIntensity('float')
>>> img2d = ants.image_read(ants.get_data('r16')).clone('unsigned int')
>>> img2d_float = caster.transform(img2d)
>>> print(img2d.pixeltype, '- ', img2d_float.pixeltype)
>>> img3d = ants.image_read(ants.get_data('mni')).clone('unsigned int')
>>> img3d_float = caster.transform(img3d)
>>> print(img3d.pixeltype, ' - ' , img3d_float.pixeltype)
"""
insuffix = X._libsuffix
outsuffix = '%s%i' % (utils.short_ptype(self.pixeltype), X.dimension)
cast_fn = utils.get_lib_fn('castAntsImage%s%s' % (insuffix, outsuffix))
casted_ptr = cast_fn(X.pointer)
return iio.ANTsImage(pixeltype=self.pixeltype, dimension=X.dimension,
components=X.components, pointer=casted_ptr)
class BlurIntensity(object):
"""
Transform for blurring the intensity of an ANTsImage
using a Gaussian Filter
"""
def __init__(self, sigma, width):
"""
Initialize a BlurIntensity transform
Arguments
---------
sigma : float
variance of gaussian kernel intensity
increasing this value increasing the amount
of blur
width : int
width of gaussian kernel shape
increasing this value increase the number of
neighboring voxels which are used for blurring
Example
-------
>>> import ants
>>> blur = ants.contrib.BlurIntensity(2,3)
"""
self.sigma = sigma
self.width = width
def transform(self, X, y=None):
"""
Blur an image by applying a gaussian filter.
Arguments
---------
X : ANTsImage
image to transform
y : ANTsImage (optional)
another image to transform.
Example
-------
>>> import ants
>>> blur = ants.contrib.BlurIntensity(2,3)
>>> img2d = ants.image_read(ants.get_data('r16'))
>>> img2d_b = blur.transform(img2d)
>>> ants.plot(img2d)
>>> ants.plot(img2d_b)
>>> img3d = ants.image_read(ants.get_data('mni'))
>>> img3d_b = blur.transform(img3d)
>>> ants.plot(img3d)
>>> ants.plot(img3d_b)
"""
if X.pixeltype != 'float':
raise ValueError('image.pixeltype must be float ... use TypeCast transform or clone to float')
insuffix = X._libsuffix
cast_fn = utils.get_lib_fn('blurAntsImage%s' % (insuffix))
casted_ptr = cast_fn(X.pointer, self.sigma, self.width)
return iio.ANTsImage(pixeltype=X.pixeltype, dimension=X.dimension,
components=X.components, pointer=casted_ptr,
origin=X.origin)
class LocallyBlurIntensity(object):
"""
Blur an ANTsImage locally using a gradient anisotropic
diffusion filter, thereby preserving the sharpeness of edges as best
as possible.
"""
def __init__(self, conductance=1, iters=5):
self.conductance = conductance
self.iters = iters
def transform(self, X, y=None):
"""
Locally blur an image by applying a gradient anisotropic diffusion filter.
Arguments
---------
X : ANTsImage
image to transform
y : ANTsImage (optional)
another image to transform.
Example
-------
>>> import ants
>>> blur = ants.contrib.LocallyBlurIntensity(1,5)
>>> img2d = ants.image_read(ants.get_data('r16'))
>>> img2d_b = blur.transform(img2d)
>>> ants.plot(img2d)
>>> ants.plot(img2d_b)
>>> img3d = ants.image_read(ants.get_data('mni'))
>>> img3d_b = blur.transform(img3d)
>>> ants.plot(img3d)
>>> ants.plot(img3d_b)
"""
#if X.pixeltype != 'float':
# raise ValueError('image.pixeltype must be float ... use TypeCast transform or clone to float')
insuffix = X._libsuffix
cast_fn = utils.get_lib_fn('locallyBlurAntsImage%s' % (insuffix))
casted_ptr = cast_fn(X.pointer, self.iters, self.conductance)
return iio.ANTsImage(pixeltype=X.pixeltype, dimension=X.dimension,
components=X.components, pointer=casted_ptr)
class NormalizeIntensity(object):
"""
Normalize the intensity values of an ANTsImage to have
zero mean and unit variance
NOTE: this transform is more-or-less the same in speed
as an equivalent numpy+scikit-learn solution.
Timing vs Numpy+Scikit-Learn
----------------------------
>>> import ants
>>> import numpy as np
>>> from sklearn.preprocessing import StandardScaler
>>> import time
>>> img = ants.image_read(ants.get_data('mni'))
>>> arr = img.numpy().reshape(1,-1)
>>> normalizer = ants.contrib.NormalizeIntensity()
>>> normalizer2 = StandardScaler()
>>> s = time.time()
>>> for i in range(100):
... img_scaled = normalizer.transform(img)
>>> e = time.time()
>>> print(e - s) # 3.3s
>>> s = time.time()
>>> for i in range(100):
... arr_scaled = normalizer2.fit_transform(arr)
>>> e = time.time()
>>> print(e - s) # 3.5s
"""
def __init__(self):
"""
Initialize a NormalizeIntensity transform
"""
pass
def transform(self, X, y=None):
"""
Transform an image by normalizing its intensity values to
have zero mean and unit variance.
Arguments
---------
X : ANTsImage
image to transform
y : ANTsImage (optional)
another image to transform.
Example
-------
>>> import ants
>>> normalizer = ants.contrib.NormalizeIntensity()
>>> img2d = ants.image_read(ants.get_data('r16'))
>>> img2d_r = normalizer.transform(img2d)
>>> print(img2d.mean(), ',', img2d.std(), ' -> ', img2d_r.mean(), ',', img2d_r.std())
>>> img3d = ants.image_read(ants.get_data('mni'))
>>> img3d_r = normalizer.transform(img3d)
>>> print(img3d.mean(), ',' , img3d.std(), ',', ' -> ', img3d_r.mean(), ',' , img3d_r.std())
"""
if X.pixeltype != 'float':
raise ValueError('image.pixeltype must be float ... use TypeCast transform or clone to float')
insuffix = X._libsuffix
cast_fn = utils.get_lib_fn('normalizeAntsImage%s' % (insuffix))
casted_ptr = cast_fn(X.pointer)
return iio.ANTsImage(pixeltype=X.pixeltype, dimension=X.dimension,
components=X.components, pointer=casted_ptr)
class RescaleIntensity(object):
"""
Rescale the pixeltype of an ANTsImage linearly to be between a given
minimum and maximum value.
This code uses the C++ ITK library directly, so it is fast.
NOTE: this offered a ~5x speedup over using built-in arithmetic operations in ANTs.
It is also more-or-less the same in speed as an equivalent numpy+scikit-learn
solution.
Timing vs Built-in Operations
-----------------------------
>>> import ants
>>> import time
>>> rescaler = ants.contrib.RescaleIntensity(0,1)
>>> img = ants.image_read(ants.get_data('mni'))
>>> s = time.time()
>>> for i in range(100):
... img_float = rescaler.transform(img)
>>> e = time.time()
>>> print(e - s) # 2.8s
>>> s = time.time()
>>> for i in range(100):
... maxval = img.max()
... img_float = (img - maxval) / (maxval - img.min())
>>> e = time.time()
>>> print(e - s) # 13.9s
Timing vs Numpy+Scikit-Learn
----------------------------
>>> import ants
>>> import numpy as np
>>> from sklearn.preprocessing import MinMaxScaler
>>> import time
>>> img = ants.image_read(ants.get_data('mni'))
>>> arr = img.numpy().reshape(1,-1)
>>> rescaler = ants.contrib.RescaleIntensity(-1,1)
>>> rescaler2 = MinMaxScaler((-1,1)).fit(arr)
>>> s = time.time()
>>> for i in range(100):
... img_scaled = rescaler.transform(img)
>>> e = time.time()
>>> print(e - s) # 2.8s
>>> s = time.time()
>>> for i in range(100):
... arr_scaled = rescaler2.transform(arr)
>>> e = time.time()
>>> print(e - s) # 3s
"""
def __init__(self, min_val, max_val):
"""
Initialize a RescaleIntensity transform.
Arguments
---------
min_val : float
minimum value to which image(s) will be rescaled
max_val : float
maximum value to which image(s) will be rescaled
Example
-------
>>> import ants
>>> rescaler = ants.contrib.RescaleIntensity(0,1)
"""
self.min_val = min_val
self.max_val = max_val
def transform(self, X, y=None):
"""
Transform an image by linearly rescaling its intensity to
be between a minimum and maximum value
Arguments
---------
X : ANTsImage
image to transform
y : ANTsImage (optional)
another image to transform.
Example
-------
>>> import ants
>>> rescaler = ants.contrib.RescaleIntensity(0,1)
>>> img2d = ants.image_read(ants.get_data('r16'))
>>> img2d_r = rescaler.transform(img2d)
>>> print(img2d.min(), ',', img2d.max(), ' -> ', img2d_r.min(), ',', img2d_r.max())
>>> img3d = ants.image_read(ants.get_data('mni'))
>>> img3d_r = rescaler.transform(img3d)
>>> print(img3d.min(), ',' , img3d.max(), ' -> ', img3d_r.min(), ',' , img3d_r.max())
"""
if X.pixeltype != 'float':
raise ValueError('image.pixeltype must be float ... use TypeCast transform or clone to float')
insuffix = X._libsuffix
cast_fn = utils.get_lib_fn('rescaleAntsImage%s' % (insuffix))
casted_ptr = cast_fn(X.pointer, self.min_val, self.max_val)
return iio.ANTsImage(pixeltype=X.pixeltype, dimension=X.dimension,
components=X.components, pointer=casted_ptr)
class ShiftScaleIntensity(object):
"""
Shift and scale the intensity of an ANTsImage
"""
def __init__(self, shift, scale):
"""
Initialize a ShiftScaleIntensity transform
Arguments
---------
shift : float
shift all of the intensity values by the given amount through addition.
For example, if the minimum image value is 0.0 and the shift
is 10.0, then the new minimum value (before scaling) will be 10.0
scale : float
scale all the intensity values by the given amount through multiplication.
For example, if the min/max image values are 10/20 and the scale
is 2.0, then then new min/max values will be 20/40
Example
-------
>>> import ants
>>> shiftscaler = ants.contrib.ShiftScaleIntensity(shift=10, scale=2)
"""
self.shift = shift
self.scale = scale
def transform(self, X, y=None):
"""
Transform an image by shifting and scaling its intensity values.
Arguments
---------
X : ANTsImage
image to transform
y : ANTsImage (optional)
another image to transform.
Example
-------
>>> import ants
>>> shiftscaler = ants.contrib.ShiftScaleIntensity(10,2.)
>>> img2d = ants.image_read(ants.get_data('r16'))
>>> img2d_r = shiftscaler.transform(img2d)
>>> print(img2d.min(), ',', img2d.max(), ' -> ', img2d_r.min(), ',', img2d_r.max())
>>> img3d = ants.image_read(ants.get_data('mni'))
>>> img3d_r = shiftscaler.transform(img3d)
>>> print(img3d.min(), ',' , img3d.max(), ',', ' -> ', img3d_r.min(), ',' , img3d_r.max())
"""
if X.pixeltype != 'float':
raise ValueError('image.pixeltype must be float ... use TypeCast transform or clone to float')
insuffix = X._libsuffix
cast_fn = utils.get_lib_fn('shiftScaleAntsImage%s' % (insuffix))
casted_ptr = cast_fn(X.pointer, self.scale, self.shift)
return iio.ANTsImage(pixeltype=X.pixeltype, dimension=X.dimension,
components=X.components, pointer=casted_ptr)
class SigmoidIntensity(object):
"""
Transform an image using a sigmoid function
"""
def __init__(self, min_val, max_val, alpha, beta):
"""
Initialize a SigmoidIntensity transform
Arguments
---------
min_val : float
minimum value
max_val : float
maximum value
alpha : float
alpha value for sigmoid
beta : flaot
beta value for sigmoid
Example
-------
>>> import ants
>>> sigscaler = ants.contrib.SigmoidIntensity(0,1,1,1)
"""
self.min_val = min_val
self.max_val = max_val
self.alpha = alpha
self.beta = beta
def transform(self, X, y=None):
"""
Transform an image by applying a sigmoid function.
Arguments
---------
X : ANTsImage
image to transform
y : ANTsImage (optional)
another image to transform.
Example
-------
>>> import ants
>>> sigscaler = ants.contrib.SigmoidIntensity(0,1,1,1)
>>> img2d = ants.image_read(ants.get_data('r16'))
>>> img2d_r = sigscaler.transform(img2d)
>>> img3d = ants.image_read(ants.get_data('mni'))
>>> img3d_r = sigscaler.transform(img3d)
"""
if X.pixeltype != 'float':
raise ValueError('image.pixeltype must be float ... use TypeCast transform or clone to float')
insuffix = X._libsuffix
cast_fn = utils.get_lib_fn('sigmoidAntsImage%s' % (insuffix))
casted_ptr = cast_fn(X.pointer, self.min_val, self.max_val, self.alpha, self.beta)
return iio.ANTsImage(pixeltype=X.pixeltype, dimension=X.dimension,
components=X.components, pointer=casted_ptr)
## Physical Transforms ##
class FlipImage(object):
"""
Transform an image by flipping two axes.
"""
def __init__(self, axis1, axis2):
"""
Initialize a SigmoidIntensity transform
Arguments
---------
axis1 : int
axis to flip
axis2 : int
other axis to flip
Example
-------
>>> import ants
>>> flipper = ants.contrib.FlipImage(0,1)
"""
self.axis1 = axis1
self.axis2 = axis2
def transform(self, X, y=None):
"""
Transform an image by applying a sigmoid function.
Arguments
---------
X : ANTsImage
image to transform
y : ANTsImage (optional)
another image to transform.
Example
-------
>>> import ants
>>> flipper = ants.contrib.FlipImage(0,1)
>>> img2d = ants.image_read(ants.get_data('r16'))
>>> img2d_r = flipper.transform(img2d)
>>> ants.plot(img2d)
>>> ants.plot(img2d_r)
>>> flipper2 = ants.contrib.FlipImage(1,0)
>>> img2d = ants.image_read(ants.get_data('r16'))
>>> img2d_r = flipper2.transform(img2d)
>>> ants.plot(img2d)
>>> ants.plot(img2d_r)
"""
if X.pixeltype != 'float':
raise ValueError('image.pixeltype must be float ... use TypeCast transform or clone to float')
insuffix = X._libsuffix
cast_fn = utils.get_lib_fn('flipAntsImage%s' % (insuffix))
casted_ptr = cast_fn(X.pointer, self.axis1, self.axis2)
return iio.ANTsImage(pixeltype=X.pixeltype, dimension=X.dimension,
components=X.components, pointer=casted_ptr,
origin=X.origin)
class TranslateImage(object):
"""
Translate an image in physical space. This function calls
highly optimized ITK/C++ code.
"""
def __init__(self, translation, reference=None, interp='linear'):
"""
Initialize a TranslateImage transform
Arguments
---------
translation : list, tuple, or numpy.ndarray
absolute pixel transformation in each axis
reference : ANTsImage (optional)
image which provides the reference physical space in which
to perform the transform
interp : string
type of interpolation to use
options: linear, nearest
Example
-------
>>> import ants
>>> translater = ants.contrib.TranslateImage((10,10), interp='linear')
"""
if interp not in {'linear', 'nearest'}:
raise ValueError('interp must be one of {linear, nearest}')
self.translation = list(translation)
self.reference = reference
self.interp = interp
def transform(self, X, y=None):
"""
Example
-------
>>> import ants
>>> translater = ants.contrib.TranslateImage((40,0))
>>> img2d = ants.image_read(ants.get_data('r16'))
>>> img2d_r = translater.transform(img2d)
>>> ants.plot(img2d, img2d_r)
>>> translater = ants.contrib.TranslateImage((40,0,0))
>>> img3d = ants.image_read(ants.get_data('mni'))
>>> img3d_r = translater.transform(img3d)
>>> ants.plot(img3d, img3d_r, axis=2)
"""
if X.pixeltype != 'float':
raise ValueError('image.pixeltype must be float ... use TypeCast transform or clone to float')
if len(self.translation) != X.dimension:
raise ValueError('must give a translation value for each image dimension')
if self.reference is None:
reference = X
else:
reference = self.reference
insuffix = X._libsuffix
cast_fn = utils.get_lib_fn('translateAntsImage%s_%s' % (insuffix, self.interp))
casted_ptr = cast_fn(X.pointer, reference.pointer, self.translation)
return iio.ANTsImage(pixeltype=X.pixeltype, dimension=X.dimension,
components=X.components, pointer=casted_ptr)
class ScaleImage(object):
"""
Scale an image in physical space. This function calls
highly optimized ITK/C++ code.
"""
def __init__(self, scale, reference=None, interp='linear'):
"""
Initialize a TranslateImage transform
Arguments
---------
scale : list, tuple, or numpy.ndarray
relative scaling along each axis
reference : ANTsImage (optional)
image which provides the reference physical space in which
to perform the transform
interp : string
type of interpolation to use
options: linear, nearest
Example
-------
>>> import ants
>>> translater = ants.contrib.TranslateImage((10,10), interp='linear')
"""
if interp not in {'linear', 'nearest'}:
raise ValueError('interp must be one of {linear, nearest}')
self.scale = list(scale)
self.reference = reference
self.interp = interp
def transform(self, X, y=None):
"""
Example
-------
>>> import ants
>>> scaler = ants.contrib.ScaleImage((1.2,1.2))
>>> img2d = ants.image_read(ants.get_data('r16'))
>>> img2d_r = scaler.transform(img2d)
>>> ants.plot(img2d, img2d_r)
>>> scaler = ants.contrib.ScaleImage((1.2,1.2,1.2))
>>> img3d = ants.image_read(ants.get_data('mni'))
>>> img3d_r = scaler.transform(img3d)
>>> ants.plot(img3d, img3d_r)
"""
if X.pixeltype != 'float':
raise ValueError('image.pixeltype must be float ... use TypeCast transform or clone to float')
if len(self.scale) != X.dimension:
raise ValueError('must give a scale value for each image dimension')
if self.reference is None:
reference = X
else:
reference = self.reference
insuffix = X._libsuffix
cast_fn = utils.get_lib_fn('scaleAntsImage%s_%s' % (insuffix, self.interp))
casted_ptr = cast_fn(X.pointer, reference.pointer, self.scale)
return iio.ANTsImage(pixeltype=X.pixeltype, dimension=X.dimension,
components=X.components, pointer=casted_ptr)