airlab-unibas / airlab

Image registration laboratory for 2D and 3D image data
Apache License 2.0
408 stars 92 forks source link

some question about B-spline kernel matrix #21

Open MangoWAY opened 4 years ago

MangoWAY commented 4 years ago

Hi! I'm studying the source code. I find the method

def bspline_kernel_3d(sigma=[1, 1, 1], order=2, asTensor=False, dtype=th.float32, device='cpu'):
    kernel_ones = th.ones(1, 1, *sigma)
    kernel = kernel_ones
    padding = np.array(sigma) - 1

    for i in range(1, order + 1):
        kernel = F.conv3d(kernel, kernel_ones, padding=(padding).tolist())/(sigma[0]*sigma[1]*sigma[2])

    if asTensor:
        return kernel[0, 0, ...].to(dtype=dtype, device=device)
    else:
        return kernel[0, 0, ...].numpy()

I know we can use the control points to control the displacements. I've also read about other bspline implementations. The airlab bspline method looks like very simple. So I want to know how does it work, the meaning of sigma and why the for loop can get the bspline kernel. Thanks! :)

cherise215 commented 4 years ago

Not sure if my answer is 100 per cent correct or not. But in WIKI it says: 'Fast b-spline interpolation on a uniform sample domain can be done by iterative mean-filtering'. https://en.wikipedia.org/wiki/B-spline. In this case, the initial kernel can be view as a mean-filter, which is then interactively convolved to achieve higher-order interpolation.

qiuhuaqi commented 4 years ago

Just adding some details to @cherise215's answer to explain this implementation:

As an example, 2D quadratic (order=2) B-spline kernel with sigma=[6, 6] (kernel size 16x16) looks like this image

This implementation allows us to use an arbitrary order of B-spline function, which is pretty neat.

qiuhuaqi commented 4 years ago

A small problem in my opinon is that the transformation model in this implementation doesn't seem to allow flexible combinations of kernel size and control point spacing (stride=sigma in transposed conv), which are both locked by sigma.