# DFT and Inverse DFT in Image Processing

I have implemented DFT from this link.

1) Tried taking dft for the three Channels(R, G and B) and reconstructed the original image by taking inverse dft for all three channels and merged them together, but the image reconstructed doesnot have the same contrast as the original image.

But while altering the flags used in the link as such

  dft(input, Complex, DFT_SCALE);
dft(Complex, InverseDFTImage, DFT_INVERSE + DFT_REALOUTPUT);
InverseDFTImage.converTo(InverseDFTImage, CV_8U);


The output is same as the input, but while applying Gaussian filter, there are few colour differences near the edges.

1. Original Input Image

2. Output without any filters(followed the Procedure in the link)

3. After applying Gaussian Low Pass Filter

Can someone clarify this doubt.?

• Are you using MATLAB? – Royi Feb 26 '15 at 9:24
• @Drazick: No I'm using OpenCV – OpenCV User Feb 26 '15 at 10:20
• Then sorry, I don't understand their implementation of DFT. Usually, doing DFT and then IDFT result with the same vector / matirx up to numerical inaccuracies. – Royi Feb 26 '15 at 10:21
• to add to what @Royi said, i doubt that (even with single-precision IEEE float for the RGB bitmaps) the quantization error would add up through the FFT passes to become particularly visible. them's are 24 honest mantissa bits. and if the bitmaps have 64-bit double instead of float, then forget it. the quantization noise floor is so many dB down that it will never build up to anything of consequence in the FFT and inverse FFT. – robert bristow-johnson Dec 8 '16 at 23:37
• and as best as i can tell, by looking, the otherwise unprocessed FFT/iFFT output image appears a little bluer. in the white images, lighthouse, buildings, fence, there seems to be less yellow light. the original appears brighter. it might be just a little darker, but it seems to me that the B bitmap got a slight boost or the R and G bitmaps got reduced by a very tiny amount(s). – robert bristow-johnson Dec 8 '16 at 23:47

I saw same issue, and I found the answer here.
for short, you can set imaginary part of filtering kernel zeros before you call mulSpectrums.

Mat planes[] = {Mat::zeros(dftImage1.size(), CV_32F), Mat::zeros(dftImage1.size(), CV_32F)};
Mat kernel_spec;
planes[0] = your_filter; // real
// planes[1] = your_filter;
// imaginar should be zero! Don't touch it.
merge(planes, 2, kernel_spec);
mulSpectrums(...


Hope this helps.