[Numpy-discussion] strange behavior convolving via fft
Mon May 11 10:40:35 CDT 2009
at least I think this is strange behavior.
When convolving an image with a large kernel, its know that its faster to
perform the operation as multiplication in the frequency domain. The below
code example shows that the results of my 2d filtering are shifted from the
expected value a distance 1/2 the width of the filter in both the x and y
directions. Can anyone explain why this occurs? I have been able to find the
answer in any of my image processing books.
The code sample below is an artificial image of size (100, 100) full of
zeros, the center of the image is populated by a (10, 10) square of 1's. The
filter kernel is also a (10,10) square of 1's. The expected result of the
convolution would therefore be a peak at location (50,50) in the image.
Instead, I get (54, 54). The same shifting occurs regardless of the image
and filter (assuming the filter is symetric, so flipping isnt necessary).
I came across this behavior when filtering actual images, so this is not a
byproduct of this example. The same effect also occurs using the full FFT as
opposed to RFFT.
I have links to the images produced by this process below.
Thanks for any insight anyone can give!
In : a = np.zeros((100,100))
In : a[45:55,45:55] = 1
In : k = np.ones((10,10))
In : afft = np.fft.rfft2(a, s=(256,256))
In : kfft = np.fft.rfft2(k, s=(256,256))
In : result = np.fft.irfft2(afft*kfft).real[0:100,0:100]
In : result.argmax()
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Numpy-discussion