Difference in brightnes after DFT (Fourier)

Hello!

I would like to try to filter out periodic noise from some images using Fourier Transform. The resulting images have different brightness levels after dft > idft even if i dont filter anything out. I asked my teacher from university about that and he says this shouldn’ happen and i probably have something wrong with my code.

Simple code:

#include <opencv2/opencv.hpp>

int main() {
cv::Mat image = cv::imread(“0.BMP”, cv::IMREAD_GRAYSCALE);

image.convertTo(image, CV_32F);   // or CV_64F , same result

cv::Mat fourierTransform;
cv::dft(image, fourierTransform, cv::DFT_COMPLEX_OUTPUT);

cv::Mat inverseTransform;
cv::dft(fourierTransform, inverseTransform, cv::DFT_INVERSE | cv::DFT_REAL_OUTPUT);

cv::imshow("Original Image", image / 255.0);
normalize(inverseTransform, inverseTransform, 0, 1, NORM_MINMAX);
cv::imshow("Reconstructed Image", inverseTransform); 
cv::waitKey(0);

return 0;

}

Some images get darker, some lighter and some don’t change their brightness.

I have also tried that with FFTW library and it does the same but i was using cv::normalize() there too.

The brightness can be restored by means of other opencv functions but the images i would like to filter out are for phase calculation (fringe patterns) and any change in brightness results in faulty phase so i don’t want to mess with them too much.

So is this how DFT works or maybe something with the normalize() function?

did you show that line to your teacher?

did he actually look at your code, or did he just hypothesize?

what do you think that line does?

why do you do it?

send him my regards. I want five minutes’ worth of his salary, or a beer, if I ever find out who he is.

to answer the question: roundtripping with fourier transforms will need scaling. given the right coefficients, the whole thing roundtrips nicely. OpenCV’s functions have a flag for this scaling. please skim the docs for these functions.

Thanks for the reply.

No I didn’t show him my code as he is an optics teacher not IT. He only proposed Fourier transform as a filtering method and all the wrong-doing is on my side.

As in the first post i have suspected the whole problem may be connected to the normalize function.

Anyway should i not use the normalize() at all and find another flags for the forward and backward Fourier transform or use the normalize() but just with another flags?

I will search through the docs again.

Thank you

EDIT: after reading your answer 10 more times i think i dont need normalize() at all, just different DFT flags.

Aahhh yes its DFT_SCALE i was looking for and now I don’t need normalize() .

So for anyone in the future looking for a solution to this:

for forward transform i used

cv::dft(image, fourierTransform, cv::DFT_SCALE | cv::DFT_COMPLEX_OUTPUT);

then for inverse transform

cv::dft(fourierTransform, inverseTransform, cv::DFT_INVERSE | cv::DFT_REAL_OUTPUT);

and this yields an image in 0 - 255 range without the need of any normalization AND most importantly the same brightness!!!

Thank you @crackwitz , if we ever meet i will buy you this beer you asked for :wink:

correct.

NORM_MINMAX simply finds the min and max of the data and scales those to the limits. that will cause the entire operation to depend strongly on the data, which is not what you want. you want it to operate independently of the data.

yes, that’s the one. I’m glad it works now.