pythonimagescipyscikit-imagescipy-spatial

image distance transform different xyz voxel sizes


I would like to find minimum distance of each voxel to a boundary element in a binary image in which the z voxel size is different from the xy voxel size. This is to say that a single voxel represents a 225x110x110 (zyx) nm volume.

Normally, I would do something with scipy.ndimage.morphology.distance_transform_edt (https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.morphology.distance_transform_edt.html) but this gives the assume that isotropic sizes of the voxel:

dtrans_stack = np.zeros_like(segm_stack) # empty array to add to

### iterate over the t dimension and get distance transform
for t_iter in range(dtrans_stack.shape[0]):
    segm_ = segm_stack[t_iter, ...] # segmented image in single t
    neg_segm = np.ones_like(segm_) - segm_ # negative of the segmented image
    
    # get a ditance transform with isotropic voxel sizes
    dtrans_stack_iso = distance_transform_edt(segm_)
    dtrans_neg_stack_iso = -distance_transform_edt(neg_segm) # make distance in the segmented image negative
    dtrans_stack[t_iter, ...] = dtrans_stack_iso + dtrans_neg_stack_iso

I can do this with brute force using scipy.spatial.distance.cdist (https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.cdist.html) but this takes ages and I'd rather avoid it if I can

vox_multiplier = np.array([z_voxelsize, xy_voxelsize, xy_voxelsize]) # array of voxel sizes

## get a subset of coordinatess so I'm not wasting times in empty space
disk_size = 5 # size of disk for binary dilation
mip_tz = np.max(np.max(decon_stack, axis = 1), axis = 0)
thresh_li = threshold_li(mip_tz) # from from skimage.filters
mip_mask = mip_tz >= thresh_li
mip_mask = remove_small_objects(mip_mask) # from skimage.morphology
mip_dilated = binary_dilation(mip_mask, disk(disk_size)) # from skimage.morphology

# get the coordinates of the mask
coords = np.argwhere(mip_dilated == 1)
ycoords = coords[:, 0]
xcoords = coords[:, 1]

# get the lower and upper bounds of the xyz coordinates
ylb = np.min(ycoords)
yub = np.max(ycoords)
xlb = np.min(xcoords)
xub = np.max(xcoords)
zlb = 0
zub = zdims -1

# make zeros arrays of the proper size
dtrans_stack = np.zeros_like(segm_stack)
dtrans_stack_neg = np.zeros_like(segm_stack) # this will be the distance transform into the low inten area

for t_iter in range(dtrans_stack.shape[0]):
    segm_ = segm_stack[t_iter, ...]
    neg_segm_ = np.ones_like(segm_) - segm_ # negative of the segmented image
    
    # get the coordinats of segmented image and convert to nm 
    segm_coords = np.argwhere(segm_ == 1)
    segm_coords_nm = vox_multiplier * segm_coords
    neg_segm_coords = np.argwhere(neg_segm_ == 1)
    neg_segm_coords_nm = vox_multiplier * neg_segm_coords
    
    # make an empty arrays for the xy and z distance transforms
    dtrans_stack_x = np.zeros_like(segm_)
    dtrans_stack_y = np.zeros_like(segm_)
    dtrans_stack_z = np.zeros_like(segm_)
    dtrans_stack_neg_x = np.zeros_like(segm_)
    dtrans_stack_neg_y = np.zeros_like(segm_)
    dtrans_stack_neg_z = np.zeros_like(segm_)
    
    # iterate over the zyx and determine the minimum distance in nm from segmented image
    for z_iter in range(zlb, zub):
        for y_iter in range(ylb, yub):
            for x_iter in range(xlb, xub):
                coord_nm =  vox_multiplier* np.array([z_iter, y_iter, x_iter]) # change coords from pixel to nm
                coord_nm = coord_nm.reshape(1, 3) # reshape for distance calculateion
                dists_segm = distance.cdist(coord_nm, segm_coords_nm) # distance from the segmented image 
                dists_neg_segm = distance.cdist(coord_nm, neg_segm_coords_nm) # distance from the negative segmented image
                dtrans_stack[t_iter, z_iter, y_iter, x_iter] = np.min(dists_segm) # add minimum distance to distance transfrom stack
                dtrans_neg_stack[t_iter, z_iter, y_iter, x_iter] = np.min(dists_neg_segm)
 

Here is image of a single zslice of segmented image if that helps to clear things up single z-slice of segmented image


Solution

  • Normally, I would do something with scipy.ndimage.morphology.distance_transform_edt but this gives the assume that isotropic sizes of the voxel:

    It does no such thing! You are looking for the sampling= parameter. From the latest version of the docs:

    Spacing of elements along each dimension. If a sequence, must be of length equal to the input rank; if a single number, this is used for all axes. If not specified, a grid spacing of unity is implied.

    The wording "sampling" or "spacing" is probably a bit mysterious if you think of pixels as little squares/cubes, and that is probably why you missed it. In most situations, it is better to think of pixels as point samples on a grid, with fixed spacing between samples. I recommend Alvy Ray's a pixel is not a little square for a better understanding of this terminology.