Python implementation of ImageJ Cell Magic Wand tool originally created by Theo Walker and provided at https://www.maxplanckflorida.org/fitzpatricklab/software/cellMagicWand/.
This implementation uses the same dynamic programming and edge following algorithm as the original, but operates on Python Numpy arrays. This makes it easier to incorporate into automated image analysis pipelines. This implementation is more robust to seed point selection and can also optionally operate on 3D images using information from all z slices.
There are 3 primary functions in cell_magic_wand.py
, all of which have the same calling conventions.
-
cell_magic_wand_single_point(image, center, min_radius, max_radius, roughness, zoom_factor)
is analogous to the original ImageJ implementation -
cell_magic_wand(image, center, min_radius, max_radius, roughness, zoom_factor, center_range)
runscell_magic_wand_single_point
with 9 seed points located in a square around and including the given center. A point is included in the final ROI if it is in the majority of ROIs found from each of the 9 seed points. -
cell_magic_wand_3d(image, center, min_radius, max_radius, roughness, zoom_factor, center_range, z_step)
runscell_magic_wand
on multiple z slices in a 3D image. A point is included in the final ROI if it is in the majority of ROIs found from the z slices. Note that this function can take considerably longer to run than above 2D functions depending on the z depth and thez_step
argument.
-
image
A 2D or 3D image in Numpy array format. A 3Dimage
argument tocell_magic_wand_3D
should have its z dimension as the 0th dimension. For TIFF images, importtifffile
(or install usingpip
) and runimage = tifffile.load([image_filename])
to get an image variable with the right format. -
center
A seed point located as close to the center of the cell as possible. Should be a 2-element tuple of integers(y,x)
(pixels down, pixels right). If you are having problems, make sure you have the right coordinate system. -
min_radius
The minimum distance from the seed point that the edge detection algorithm will search. Increasingmin_radius
helps the tool avoid finding organelles and other boundaries inside the cell. -
max_radius
The maximum distance from the seed point the edge detection algorithm will search. Decreasingmax_radius
helps keep the tool from circling multiple cells. -
roughness
Controls the roughness of the edge of the detected ROI. Analogous to the roughness parameter of the ImageJ Cell Magic Wand tool but not exactly equivalent. The tool samples points at2 * pi * max_radius * roughness
angles around the center. Increasingroughness
increases runtime but can result in better detection. The default value is 2 and values [1,10] are reasonable. -
zoom_factor
Factor by which the image is upsampled prior to edge detection. In most cases, the default value of 1 is appropriate. -
center_range
The distance tocenter
of the additional 8 seed points used in the robustcell_magic_wand
andcell_magic_wand_3d
functions. The default 2, so for center(y,x)
, points(y-2,x)
,(y,x+2)
, etc. will be used. Set such that all points used will be inside your cells and near the center. -
z_step
The z stride used incell_magic_wand_3d
. For an image with N z-slices,floor(N/z_stride)
will be used.
All cell_magic_wand
functions return a binary mask that is the same size as the input image with 1s inside the detected cell and 0s elsewhere.
cell_magic_wand_single_point
also returns an array of points that are on the edge of the detected cell
Even with multi-seed sampling, the center
argument still considerably affects cell magic wand tool accuracy.
If your image has a lot of contrast variability, preprocessing it with a median filter can improve cell magic wand tool results. The easiest way is to use the median_filter
function from scipy.ndimage.filters
.