Skip to content

Commit f2e2950

Browse files
committed
Merge branch 'main' into fix-nd2
2 parents 9210e2e + 5efc56c commit f2e2950

3 files changed

Lines changed: 30 additions & 14 deletions

File tree

cellpose/io.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -288,14 +288,18 @@ def imread_3D(img_file):
288288
"""
289289
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.
290290
291-
If multichannel image, the channel axis is assumed to be the smallest dimension, and the z axis is the next smallest dimension.
292-
Use `cellpose.io.imread()` to load the full image without selecting the z and channel axes.
293-
291+
For grayscale images (3D array), axis 0 is assumed to be the Z axis (e.g., Z x Y x X).
292+
For multichannel images (4D array), the channel axis is assumed to be the smallest dimension,
293+
and the Z axis is assumed to be the first remaining axis after the channel axis is removed.
294+
295+
Use ``cellpose.io.imread()`` to load the full image without automatic axis selection,
296+
then specify ``z_axis`` and ``channel_axis`` manually when calling ``model.eval``.
297+
294298
Args:
295299
img_file (str): The path to the image file.
296300
297301
Returns:
298-
img_out (numpy.ndarray): The image data as a NumPy array.
302+
img_out (numpy.ndarray): The image data as a NumPy array with channels last, or None if loading fails.
299303
"""
300304
img = imread(img_file)
301305
if img is None:
@@ -307,16 +311,15 @@ def imread_3D(img_file):
307311
if img.ndim == 3:
308312
channel_axis = None
309313
# guess at z axis:
310-
z_axis = np.argmin(dimension_lengths)
314+
z_axis = 0
311315

312316
elif img.ndim == 4:
313317
# guess at channel axis:
314318
channel_axis = np.argmin(dimension_lengths)
315-
316-
# guess at z axis:
317-
# set channel axis to max so argmin works:
318-
dimension_lengths[channel_axis] = max(dimension_lengths)
319-
z_axis = np.argmin(dimension_lengths)
319+
dimensions = list(range(img.ndim))
320+
dimensions.pop(channel_axis)
321+
# guess at z axis as the first remaining dimension:
322+
z_axis = dimensions[0]
320323

321324
else:
322325
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

tox.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ passenv =
2626
DISPLAY,XAUTHORITY
2727
NUMPY_EXPERIMENTAL_ARRAY_FUNCTION
2828
PYVISTA_OFF_SCREEN
29+
USERNAME
30+
USER
31+
LOGNAME
32+
LNAME
2933
TEMP
3034
TMP
3135
TMPDIR

0 commit comments

Comments
 (0)