Skip to content

Commit 17e967b

Browse files
authored
Merge pull request #1409 from MouseLand/guess_3d_chan_0
guess Z dim is 0 instead of argmin of dims
2 parents 4fa1e24 + 63096bf commit 17e967b

2 files changed

Lines changed: 26 additions & 14 deletions

File tree

cellpose/io.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -264,14 +264,18 @@ def imread_3D(img_file):
264264
"""
265265
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.
266266
267-
If multichannel image, the channel axis is assumed to be the smallest dimension, and the z axis is the next smallest dimension.
268-
Use `cellpose.io.imread()` to load the full image without selecting the z and channel axes.
269-
267+
For grayscale images (3D array), axis 0 is assumed to be the Z axis (e.g., Z x Y x X).
268+
For multichannel images (4D array), the channel axis is assumed to be the smallest dimension,
269+
and the Z axis is assumed to be the first remaining axis after the channel axis is removed.
270+
271+
Use ``cellpose.io.imread()`` to load the full image without automatic axis selection,
272+
then specify ``z_axis`` and ``channel_axis`` manually when calling ``model.eval``.
273+
270274
Args:
271275
img_file (str): The path to the image file.
272276
273277
Returns:
274-
img_out (numpy.ndarray): The image data as a NumPy array.
278+
img_out (numpy.ndarray): The image data as a NumPy array with channels last, or None if loading fails.
275279
"""
276280
img = imread(img_file)
277281

@@ -281,16 +285,15 @@ def imread_3D(img_file):
281285
if img.ndim == 3:
282286
channel_axis = None
283287
# guess at z axis:
284-
z_axis = np.argmin(dimension_lengths)
288+
z_axis = 0
285289

286290
elif img.ndim == 4:
287291
# guess at channel axis:
288292
channel_axis = np.argmin(dimension_lengths)
289-
290-
# guess at z axis:
291-
# set channel axis to max so argmin works:
292-
dimension_lengths[channel_axis] = max(dimension_lengths)
293-
z_axis = np.argmin(dimension_lengths)
293+
dimensions = list(range(img.ndim))
294+
dimensions.pop(channel_axis)
295+
# guess at z axis as the first remaining dimension:
296+
z_axis = dimensions[0]
294297

295298
else:
296299
raise ValueError(f'image shape error, 3D image must 3 or 4 dimensional. Number of dimensions: {img.ndim}')

docs/do3d.rst

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,21 @@ then the GUI will automatically run 3D segmentation and display it in the GUI. W
2525
the command line for progress. It is recommended to use a GPU to speed up processing.
2626

2727
In the CLI/notebook, you need to specify the ``z_axis`` and the ``channel_axis``
28-
parameters to specify the axis (0-based) of the image which corresponds to the image channels and to the z axis.
29-
For example an image with 2 channels of shape (1024,1024,2,105,1) can be
30-
specified with ``channel_axis=2`` and ``z_axis=3``. These parameters can be specified using the command line
31-
with ``--channel_axis`` or ``--z_axis`` or as inputs to ``model.eval`` for
28+
parameters to specify the axis (0-based) of the image which corresponds to the image channels and to the z axis.
29+
For example an image with 2 channels of shape (1024,1024,2,105,1) can be
30+
specified with ``channel_axis=2`` and ``z_axis=3``. These parameters can be specified using the command line
31+
with ``--channel_axis`` or ``--z_axis`` or as inputs to ``model.eval`` for
3232
the ``CellposeModel`` model.
3333

34+
As a convenience, :func:`cellpose.io.imread_3D` will attempt to load a 3D image and
35+
automatically guess the axes. For grayscale images (3D array), axis 0 is assumed
36+
to be the Z axis (e.g., Z x Y x X). For multichannel images (4D array), the
37+
channel axis is assumed to be the smallest dimension, and the Z axis is assumed to
38+
be the first remaining axis after the channel axis is identified (e.g., for a
39+
Z x C x Y x X image, channel axis = 1 and z axis = 0). If your image does not
40+
follow these conventions, use ``cellpose.io.imread`` and specify ``z_axis`` and
41+
``channel_axis`` manually.
42+
3443
Volumetric stacks do not always have the same sampling in XY as they do in Z.
3544
Therefore you can set an ``anisotropy`` parameter in CLI/notebook to allow for differences in
3645
sampling, e.g. set to 2.0 if Z is sampled half as dense as X or Y, and then in the algorithm

0 commit comments

Comments
 (0)