DeepRegNet / DeepReg

Medical image registration using deep learning
Apache License 2.0
564 stars 76 forks source link

Uniform 1d kernels #723

Closed mathpluscode closed 3 years ago

mathpluscode commented 3 years ago

Subject of the feature

Once we merged #719, we will have two nearly identical gaussian kernels

def gaussian_kernel1d_size(kernel_size: int) -> tf.Tensor:
    """
    Return a the 1D Gaussian kernel for LocalNormalizedCrossCorrelation.

    Sum of the weights is 1.

    :param kernel_size: scalar, size of the 1-D kernel
    :return: filters, of shape (kernel_size, )
    """
    mean = (kernel_size - 1) / 2.0
    sigma = kernel_size / 3

    grid = tf.range(0, kernel_size, dtype=tf.float32)
    filters = tf.exp(-tf.square(grid - mean) / (2 * sigma ** 2))
    filters = filters / tf.reduce_sum(filters)

    return filters

def gaussian_kernel1d_sigma(sigma: int) -> tf.Tensor:
    """
    Calculate a gaussian kernel.

    :param sigma: number defining standard deviation for
                  gaussian kernel.
    :return: shape = (dim, )
    """
    assert sigma > 0
    tail = int(sigma * 3)
    kernel = tf.exp([-0.5 * x ** 2 / sigma ** 2 for x in range(-tail, tail + 1)])
    kernel = kernel / tf.reduce_sum(kernel)
    return kernel

Therefore it's better to merge them, so that kernel accepts uniformly the size as an argument.

The Cauchy kernel needs to be updated to

mathpluscode commented 3 years ago

Now we no longer normalize the kernel, but it's still worth uniforming the kernels.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.