[5d12a0]: / ants / utils / ndimage_to_list.py

Download this file

112 lines (95 with data), 3.4 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
__all__ = ['ndimage_to_list',
'list_to_ndimage']
import numpy as np
import ants
from ants.decorators import image_method
@image_method
def list_to_ndimage( image, image_list ):
"""
Merge list of multiple scalar ANTsImage types of dimension into one
ANTsImage of dimension plus one
ANTsR function: `mergeListToNDImage`
Arguments
---------
image : target image space
image_list : list/tuple of ANTsImage types
scalar images to merge into target image space
Returns
-------
ANTsImage
Example
-------
>>> import ants
>>> image = ants.image_read(ants.get_ants_data('r16'))
>>> image2 = ants.image_read(ants.get_ants_data('r16'))
>>> imageTar = ants.make_image( ( *image2.shape, 2 ) )
>>> image3 = ants.list_to_ndimage( imageTar, [image,image2])
>>> image3.dimension == 3
"""
inpixeltype = image_list[0].pixeltype
dimension = image_list[0].dimension
components = len(image_list)
for imageL in image_list:
if not ants.is_image(imageL):
raise ValueError('list may only contain ANTsImage objects')
if image.pixeltype != inpixeltype:
raise ValueError('all images must have the same pixeltype')
dimensionout = ( *image_list[0].shape, len( image_list ) )
newImage = ants.make_image(
dimensionout,
spacing = ants.get_spacing( image ),
origin = ants.get_origin( image ),
direction = ants.get_direction( image ),
pixeltype = inpixeltype
)
# FIXME - should implement paste image filter from ITK
for x in range( len( image_list ) ):
if dimension == 2:
newImage[:,:,x] = image_list[x][:,:]
if dimension == 3:
newImage[:,:,:,x] = image_list[x][:,:,:]
return newImage
@image_method
def ndimage_to_list(image):
"""
Split a n dimensional ANTsImage into a list
of n-1 dimensional ANTsImages
Arguments
---------
image : ANTsImage
n-dimensional image to split
Returns
-------
list of ANTsImage types
Example
-------
>>> import ants
>>> image = ants.image_read(ants.get_ants_data('r16'))
>>> image2 = ants.image_read(ants.get_ants_data('r16'))
>>> imageTar = ants.make_image( ( *image2.shape, 2 ) )
>>> image3 = ants.list_to_ndimage( imageTar, [image,image2])
>>> image3.dimension == 3
>>> images_unmerged = ants.ndimage_to_list( image3 )
>>> len(images_unmerged) == 2
>>> images_unmerged[0].dimension == 2
"""
inpixeltype = image.pixeltype
dimension = image.dimension
components = 1
imageShape = image.shape
nSections = imageShape[ dimension - 1 ]
subdimension = dimension - 1
suborigin = ants.get_origin( image )[0:subdimension]
subspacing = ants.get_spacing( image )[0:subdimension]
subdirection = np.eye( subdimension )
for i in range( subdimension ):
subdirection[i,:] = ants.get_direction( image )[i,0:subdimension]
subdim = image.shape[ 0:subdimension ]
imagelist = []
for i in range( nSections ):
img = ants.slice_image( image, axis = subdimension, idx = i )
ants.set_spacing( img, subspacing )
ants.set_origin( img, suborigin )
ants.set_direction( img, subdirection )
imagelist.append( img )
return imagelist