Bug in distance transform

distance.cppipe (5.9 KB)
white.bmp (65.1 KB)

I have discovered a strange behavior in the distance transform in CellProfiler (3.1.9). If you do the distance transform on a completely white image (i.e. all values = 1) then the result is a gradient image with value 1 (if non-scaled values) in the upper left corner (0,0) and increasing values towards the lower right corner. I would expect the resulting image either to contain a gradient towards the center of the image (i.e., treating the border as 0), or a homogenous image with value Infinity (or 1 if scaled values) (i.e., treating the border as 1). I would prefer the latter, but maybe it should be possible to choose how to treat the border?

This has caught me out before, it seems to be an issue with the underlying scipy.ndimage.distance_transform_edt() function. You can get the same behaviour like so:

import numpy as np
import scipy.ndimage

scipy.ndimage.distance_transform_edt(np.ones([5, 5]))
array([[ 1.        ,  1.41421356,  2.23606798,  3.16227766,  4.12310563],
       [ 2.        ,  2.23606798,  2.82842712,  3.60555128,  4.47213595],
       [ 3.        ,  3.16227766,  3.60555128,  4.24264069,  5.        ],
       [ 4.        ,  4.12310563,  4.47213595,  5.        ,  5.65685425],
       [ 5.        ,  5.09901951,  5.38516481,  5.83095189,  6.40312424]])
2 Likes