0

I am attempting to convert a greyscale image to and from the frequency domain using the Fourier transform in OpenCV. However, the resulting image in very distorted even though I made no changes to the image while in frequency domain. Could anyone help me with this? I've found several other questions explaining this like the links below and I have followed them exactly, but the result always ends up like this.

Inverse fourier transformation in OpenCV https://coderedirect.com/questions/165340/inverse-fourier-transformation-in-opencv

//Make grayscale image
cvtColor(src, gray_in, COLOR_BGR2GRAY);
gray_in.convertTo(gray_in, CV_32FC1);

//Create complex output variable
//From https://docs.opencv.org/4.x/d8/d01/tutorial_discrete_fourier_transform.html
Mat planes[] = { Mat_<float>(gray_in), Mat::zeros(gray_in.size(), CV_32F) };
Mat complexI;
merge(planes, 2, complexI);

//Transform
dft(gray_in, complexI, DFT_COMPLEX_OUTPUT);


//Compute inverse transform
dft(complexI, tgt, DFT_SCALE | DFT_INVERSE | DFT_REAL_OUTPUT);

//Save file
tgt.convertTo(tgt, CV_32FC2);
imwrite(outfile, tgt);

//Display image
namedWindow(windowName);
imshow(windowName, tgt);
waitKey(0);
destroyWindow(windowName);

Original Resulting Image

  • Just a guess but check the meaning of DFT_SCALE. Looks like scaling isn't the same going to dft then back. – doug Nov 28 '21 at 23:06
  • check min and max of your result. be aware that imwrite and imshow handle float data differently from uint8 data. you will need to scale the result and/or convert to integers (uint16/32 is again differen from uint8) – Christoph Rackwitz Nov 28 '21 at 23:12

0 Answers0