[5d12a0]: / ants / ops / slice_image.py

Download this file

69 lines (52 with data), 2.0 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
__all__ = ['slice_image']
import math
import numpy as np
import ants
from ants.decorators import image_method
from ants.internal import get_lib_fn
@image_method
def slice_image(image, axis, idx, collapse_strategy=0):
"""
Slice an image.
Arguments
---------
axis: integer
Which axis.
idx: integer
Which slice number.
collapse_strategy: integer
Collapse strategy for sub-matrix: 0, 1, or 2. 0: collapse to sub-matrix
if positive-definite. Otherwise throw an exception. Default. 1: Collapse
to identity. 2: Collapse to sub-matrix if positive definite. Otherwise
collapse to identity.
Example
-------
>>> import ants
>>> mni = ants.image_read(ants.get_data('mni'))
>>> mni2 = ants.slice_image(mni, axis=1, idx=100)
"""
if image.has_components:
ilist = ants.split_channels(image)
if image.dimension == 2:
return np.stack(tuple([i.slice_image(axis, idx, collapse_strategy) for i in ilist]), axis=-1)
else:
return ants.merge_channels([i.slice_image(axis, idx, collapse_strategy) for i in ilist])
if axis == -1:
axis = image.dimension - 1
if axis > (image.dimension - 1) or axis < 0:
raise Exception('The axis must be between 0 and image.dimension - 1')
if image.dimension == 2:
if axis == 0:
return image[idx,:]
elif axis == 1:
return image[:,idx]
raise Exception('Parameters not understood for 2D image.')
if collapse_strategy != 0 and collapse_strategy != 1 and collapse_strategy != 2:
raise ValueError('collapse_strategy must be 0, 1, or 2.')
inpixeltype = image.pixeltype
ndim = image.dimension
if image.pixeltype != 'float':
image = image.clone('float')
libfn = get_lib_fn('sliceImage')
itkimage = libfn(image.pointer, axis, idx, collapse_strategy)
return ants.from_pointer(itkimage).clone(inpixeltype)