Tensorflow dilation behave differently than morphological dilation

As mentioned in the documentation page linked,

Computes the grayscale dilation of 4-D input and 3-D filter tensors.

and

In detail, the grayscale morphological 2-D dilation is the max-sum correlation [...]

What this means is that the kernel's values are added to the image's values at each position, then the maximum value is taken as the output value.

Compare this to correlation, replacing the multiplication with an addition, and the integral (or sum) with the maximum:

      convolution: g(t) = ∫ f() h(-t) d

      dilation: g(t) = max { f() + h(-t) }

Or in the discrete world:

      convolution: g[n] = ∑k f[k] h[k-n]

      dilation: g[n] = maxk { f[k] + h[k-n] }


The dilation with a binary structuring element (kernel, what the question refers to as a “conventional dilation”) uses a structuring element (kernel) that contains only 1s and 0s. These indicate “included” and “excluded”. That is, the 1s determine the domain of the structuring element.

To recreate the same behavior with a grey-value dilation, set the “included” pixels to 0 and the “excluded” pixels to minus infinity.

For example, the 3x3 square structuring element used in the question should be a 3x3 matrix of zeros.


can do it like this:

def dilation2d(self, img4D):
    '''
    '''
    with tf.variable_scope('dilation2d'):
        kernel = tf.ones((3, 3, img4D.get_shape()[3])) 
        output4D = tf.nn.dilation2d(img4D, filter=kernel, strides=(1,1,1,1), rates=(1,1,1,1), padding="SAME")
        output4D = output4D - tf.ones_like(output4D)

        return output4D