@@ -235,7 +235,10 @@ def imread_2D(img_file):
235235
236236def imread_3D (img_file ):
237237 """
238- Read in a 3D image file and convert it to have a channel axis last. Attempts to do this for multi-channel and grayscale images.
238+ Read in a 3D image file and convert it to have a channel axis last automatically. Attempts to do this for multi-channel and grayscale images.
239+
240+ If multichannel image, the channel axis is assumed to be the smallest dimension, and the z axis is the next smallest dimension.
241+ Use `cellpose.io.imread()` to load the full image without selecting the z and channel axes.
239242
240243 Args:
241244 img_file (str): The path to the image file.
@@ -247,16 +250,23 @@ def imread_3D(img_file):
247250
248251 dimension_lengths = list (img .shape )
249252
250- # guess at channel axis:
251- channel_axis = np .argmin (img .shape )
252- del dimension_lengths [channel_axis ]
253-
254- # guess at z axis:
255- z_axis = np .argmin (dimension_lengths )
256-
257253 # grayscale images:
258254 if img .ndim == 3 :
259255 channel_axis = None
256+ # guess at z axis:
257+ z_axis = np .argmin (dimension_lengths )
258+
259+ elif img .ndim == 4 :
260+ # guess at channel axis:
261+ channel_axis = np .argmin (dimension_lengths )
262+
263+ # guess at z axis:
264+ # set channel axis to max so argmin works:
265+ dimension_lengths [channel_axis ] = max (dimension_lengths )
266+ z_axis = np .argmin (dimension_lengths )
267+
268+ else :
269+ raise ValueError (f'image shape error, 3D image must 3 or 4 dimensional. Number of dimensions: { img .ndim } ' )
260270
261271 try :
262272 return transforms .convert_image (img , channel_axis = channel_axis , z_axis = z_axis , do_3D = True )
0 commit comments