Distributed Cellpose can execute custom preprocessing functions on each block before segmentation.
Rules for Preprocessing Functions:
- The first parameter must be the
image. - The last parameter must be the
crop (which contains the slices for the current block). - You can include any number of other parameters in between.
- Example signature:
def my_step(image, param1, crop): ...
Use Cases:
- Smoothing: Applying Gaussian filters.
- Multi-channel segmentation: Using a preprocessing step to stack a second channel (from another Zarr array) onto the current block's image using the
crop to ensure spatial alignment. - Background subtraction: Subtracting a background channel using the
crop to index into a background Zarr array.
from scipy.ndimage import gaussian_filter
from cellpose.contrib.distributed_segmentation import distributed_eval
# Example: Preprocessing with Gaussian smoothing and channel stacking
def pp_step_one(image, sigma, crop):
return gaussian_filter(image, sigma)
def stack_channels(image, crop):
# second_channel_zarr must be a Zarr array
return np.stack((image, second_channel_zarr[crop]), axis=1)
preprocessing_steps = [
(pp_step_one, {'sigma': 2.0}),
(stack_channels, {})
]
# Pass to distributed_eval
segments, boxes = distributed_eval(
input_zarr=large_zarr_array,
blocksize=(256, 256, 256),
write_path='/path/to/output.zarr',
preprocessing_steps=preprocessing_steps,
model_kwargs=model_kwargs,
eval_kwargs={'channels': [2, 1], ...},
cluster_kwargs=cluster_kwargs,
)