@@ -88,8 +88,8 @@ def squeeze_image(img):
88
88
img .extra )
89
89
90
90
91
- def concat_images (images , check_affines = True ):
92
- ''' Concatenate images in list to single image, along last dimension
91
+ def concat_images (images , check_affines = True , axis = None ):
92
+ ''' Concatenate images in list to single image, along specified dimension
93
93
94
94
Parameters
95
95
----------
@@ -98,7 +98,9 @@ def concat_images(images, check_affines=True):
98
98
check_affines : {True, False}, optional
99
99
If True, then check that all the affines for `images` are nearly
100
100
the same, raising a ``ValueError`` otherwise. Default is True
101
-
101
+ axis : int, optional
102
+ If None, concatenates on the last dimension.
103
+ If not None, concatenates on the specified dimension.
102
104
Returns
103
105
-------
104
106
concat_img : ``SpatialImage``
@@ -122,8 +124,13 @@ def concat_images(images, check_affines=True):
122
124
if check_affines :
123
125
if not np .all (img .affine == affine ):
124
126
raise ValueError ('Affines do not match' )
125
- out_data [i ] = img .get_data ()
126
- out_data = np .rollaxis (out_data , 0 , len (i0shape )+ 1 )
127
+ out_data [i ] = img .get_data ().copy ()
128
+ if axis is not None :
129
+ out_data = np .concatenate (out_data , axis = axis )
130
+ elif np .all ([d .shape [- 1 ] == 1 for d in out_data ]):
131
+ out_data = np .concatenate (out_data , axis = d .ndim - 1 )
132
+ else :
133
+ out_data = np .rollaxis (out_data , 0 , len (i0shape )+ 1 )
127
134
klass = img0 .__class__
128
135
return klass (out_data , affine , header )
129
136
0 commit comments