@@ -94,7 +94,7 @@ def concat_images(images, check_affines=True, axis=None):
94
94
Parameters
95
95
----------
96
96
images : sequence
97
- sequence of ``SpatialImage`` or of filenames\s
97
+ sequence of ``SpatialImage`` or filenames\s
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
@@ -109,32 +109,33 @@ def concat_images(images, check_affines=True, axis=None):
109
109
'''
110
110
n_imgs = len (images )
111
111
img0 = images [0 ]
112
- is_filename = False
113
112
if not hasattr (img0 , 'get_data' ):
114
113
img0 = load (img0 )
115
- is_filename = True
116
114
affine = img0 .affine
117
115
header = img0 .header
116
+ i0shape = img0 .shape
117
+ del img0
118
118
119
119
if axis is None : # collect images in output array for efficiency
120
- out_shape = (n_imgs , ) + img0 . shape [:3 ]
120
+ out_shape = (n_imgs , ) + i0shape [:3 ]
121
121
out_data = np .empty (out_shape )
122
122
else : # collect images in list for use with np.concatenate
123
123
out_data = [None ] * n_imgs
124
124
125
125
for i , img in enumerate (images ):
126
- if is_filename :
126
+ if not hasattr ( img , 'get_data' ) :
127
127
img = load (img )
128
+
128
129
if check_affines and not np .all (img .affine == affine ):
129
130
raise ValueError ('Affines do not match' )
130
131
132
+ # Special case for 4D image with size[3] == 1; reshape to work!
131
133
if axis is None and img .get_data ().ndim == 4 and img .get_data ().shape [3 ] == 1 :
132
134
out_data [i ] = np .reshape (img .get_data (), img .get_data ().shape [:- 1 ])
133
135
else :
134
136
out_data [i ] = img .get_data ()
135
137
136
- if is_filename :
137
- del img
138
+ del img
138
139
139
140
if axis is None :
140
141
out_data = np .rollaxis (out_data , 0 , out_data .ndim )
0 commit comments