Bizarre result in manual image filtering

Hello,
I have a code for manual 2D image filter. You need a 512 by 512 image to execute the code.
xx=-255:256;
[X, Y]=(meshgrid(xx,xx));
psf=exp(-(X.^2/1+Y.^2/1).^0.5);
psf2=fft2(psf);
testimage2fft=ifft2(fft2(testimage2));%same with original.
testimage2psf=(ifft2(fft2(testimage2).*(psf2)));% complex valued, fftshifted, filtered image
testimage2psf2=(ifft2(fft2(testimage2).*abs(psf2)));% complex valued, original, filtered image
This code is meant to be a low pass filter.
As you expect,
testimage2fft=ifft2(fft2(testimage2));%same with original
is real value, and same with testimage2.
What about below?
testimage2psf=(ifft2(fft2(testimage2).*(psf2)));% complex valued, fftshifted, filtered image
figure;imagesc(abs(testimage2psf));
It is complex valued, that deserves. I expected to see the filtered image, but it is fftshifted.
The last one is as follow,
testimage2psf2=(ifft2(fft2(testimage2).*abs(psf2)));% complex valued, original, filtered image
figure;imagesc(abs(testimage2psf2));
Yes, it shows the filtered image, and not shifted, what I expected.
What am I missing here? How does the '.*psf2' give shifted result while '.*abs(psf2)' give correct result?

1 Comment

The second one is multiplying two complex-valued results together so the two imaginary terms will become real and the imaginary will be a combination of the real part of each with the imaginary part of the other. That will clearly give a different result than just multiplying by an absolute envelope result as you are in the last of your solutions.

Sign in to comment.

 Accepted Answer

Hello Hyosub, [1] first of all if you don't want off-by-one problems, your x grid should be -256:255. [2] the reason you are getting image displacement is that for the fft and ifft, the zero of both position and spacial frequency is the first element of the array. Your function psf is centered in the middle of the array, far away from zero. When you fft it to go into the frequency domain, you pick up a large phase shift in psf2. With that phase shift, going back to the spacial domain gives a large displacement of the image. If you redo your x array as in [1] and shift psf down to the origin,
psf2 = fft2(ifftshift(psf));
the phase shift won't be there. You should be able to transform back without using abs on psf2.

1 Comment

Thank you. It works. Your answer may be the same analogue with this : https://kr.mathworks.com/matlabcentral/newsreader/view_thread/285244
I have to carefully use FFT and fftshift from now on.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!