So I was following the article Victor Podlozhnyuk (nVidia) - FFT Based 2D Convolution (Page 7).

I have expanded the kernel to the correct way they have done it. However when it comes to the part on clamping to the edge its very confusing. I would love for someone to explain this as i require the image convolved as the same shape before the padding, but in this article it just shows how to convolve with clamp to edge but not how to recover it.

code as of now

def circExt(k, rows, cols):
    radiusV = np.floor(k.shape[0] / 2)
    radiusH = np.floor(k.shape[1] / 2)
    k_c = k

    k_c = np.pad(k_c,((0, int(rows-k.shape[0])), (0, int(cols-k.shape[1]))))

    k_c = np.roll(k_c, int(-radiusV), axis = 0)
    k_c = np.roll(k_c, int(-radiusH), axis = 1)
    return k_c

def testfft2(image, kernel):
    imageC = image.copy()
    kSize = (kernel.size // 2) + 1
    kernelShape = tuple(ti//2 for ti in kernel.shape)
    k_width = kernel.shape[1]
    k_height = kernel.shape[0]
    centery ,centerx = kernelShape
    imageC = np.pad(imageC, ((0, int(centery)), (0, int(centerx))) , mode = "edge")

    imageC = np.pad(imageC,((0, int(k_height-centery-1)), (0, int(k_width-centerx-1))), mode = "wrap" )

    kernelShift = circExt(kernel, imageC.shape[0], imageC.shape[1])   

    imageC = np.fft.fft2(imageC)
    kernel= np.fft.fft2(kernelShift)
    output = np.real(np.fft.ifft2(np.multiply(imageC, kernel)))

    return output

1 Answer 1


You may follow the answers to the following questions which implements the paper you linked above:

All the above include code you may use to implement the paper.

  • $\begingroup$ Nearly perfect, it breaks down when I use non square kernels, but thanks a lot for the resources. $\endgroup$ Nov 18, 2021 at 7:19
  • $\begingroup$ @SimonBalfe, What breaks down? Please let me know what's missing to mark this answer. $\endgroup$
    – Royi
    Nov 21, 2021 at 17:19
  • $\begingroup$ Its not perfect convolution thats all compare to scipys one, for non square kernels $\endgroup$ Nov 23, 2021 at 3:09
  • $\begingroup$ @SimonBalfe, Could you give example? It is perfectly imitating convolution. $\endgroup$
    – Royi
    Nov 26, 2021 at 5:52
  • $\begingroup$ well in my code above that is, if its not a square kernel, the image is not perfectly the same as scipy.convolve, I followed your matlab code specifically unless there was a kernel edge case I missed. $\endgroup$ Nov 28, 2021 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.