0

While reading the image with IMREAD_COLOR, 'dft' function throws the error: Error

DFT function works just fine when reading an image with IMREAD_GRAYSCALE. But I want to read the image with IMREAD_COLOR.

main function

const char* filename = "face.jpg";
Mat I = imread(filename, IMREAD_COLOR);
if(I.empty()) return 0;
Mat padded;

I.convertTo(padded, CV_32F);

Mat fft;
Mat planes[2];
dft(padded, fft, DFT_SCALE|DFT_COMPLEX_OUTPUT);

Mat fftBlur = fft.clone();

fftBlur *= 0.5;

split(fftBlur, planes);

Mat ph, mag;

mag.zeros(planes[0].rows, planes[0].cols, CV_32F);
ph.zeros(planes[0].rows, planes[0].cols, CV_32F);
cartToPolar(planes[0], planes[1], mag, ph);

merge(planes, 2, fftBlur);

//inverse
Mat invfft;

dft(fftBlur, invfft, DFT_INVERSE|DFT_REAL_OUTPUT);

Mat result;

invfft.convertTo(result, CV_8U);
Mat image;
cvtColor(result, image, COLOR_GRAY2RGB);

imshow("Output", result);
imshow("Image", image);


waitKey();
Community
  • 1
  • 1
Nyamkhuu Buyanjargal
  • 621
  • 2
  • 11
  • 29

2 Answers2

1

The message you receive is an assertion it tells you DFT function only takes single precision floating point image with one or two channels (CV_32FC1, CV_32FC2, the letter C at the end of the flag mean channel) or double precision floating point images with one or two channels (CV_64FC1, CV_64FC2). The two channel case is actually the representation of complex image in OpenCV data storage. If you want you can split you image to std::vector<cv::Mat> where each element does represent one channel, using cv::split apply the DFT on each channels do the processing you want on it and recreate an multichannel image thanks to cv::merge.

John_Sharp1318
  • 939
  • 8
  • 19
0

From Learning OpenCV (about dft function):

The input array must be of floating-point type and may be single- or double-channel. In the single-channel case, the entries are assumed to be real numbers, and the output will be packed in a special space-saving format called complex conjugate symmetrical.

The same question is mentioned here in terms of matlab image processing. You can check out cv::split function if you want to separate channels of your initial image.