opencv应用自定义过滤器后如何使用IDFT

时间:2018-11-17 13:04:39

标签: opencv

我的任务是使用opencv去除图像中的噪声

  • 第1步:加载带有噪点的图像
  • 第2步:使用离散傅立叶变换来查看应去除的频率
  • 第3步:创建一个自定义滤镜,并将该滤镜的傅立叶光谱逐像素相乘以消除噪声
  • Step4:逆离散傅立叶逆变换以获得无噪声的原始图像

对于step2,我正在使用opencv doc提供的此功能:

void fourier_transform(const Mat& I, Mat& dst) {
 Mat padded;                            //expand input image to optimal size
 int m = getOptimalDFTSize( I.rows );
 int n = getOptimalDFTSize( I.cols ); // on the border add zero values
 copyMakeBorder(I, padded, 0, m - I.rows, 0, n - I.cols, BORDER_CONSTANT, Scalar::all(0));
 Mat planes[2] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)};
 Mat complexI;
 merge(planes, 2, complexI);         // Add to the expanded another plane with zeros

 dft(complexI, complexI);            // this way the result may fit in the source matrix

 // compute the magnitude and switch to logarithmic scale
 // => log(1 + sqrt(Re(DFT(I))^2 + Im(DFT(I))^2))
 split(complexI, planes);                   // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))
 magnitude(planes[0], planes[1], planes[0]);// planes[0] = magnitude
 Mat magI = planes[0];

 magI += Scalar::all(1);                    // switch to logarithmic scale
 log(magI, magI);

 // crop the spectrum, if it has an odd number of rows or columns
 magI = magI(Rect(0, 0, magI.cols & -2, magI.rows & -2));

 // rearrange the quadrants of Fourier image  so that the origin is at the image center
 int cx = magI.cols/2;
 int cy = magI.rows/2;

 Mat q0(magI, Rect(0, 0, cx, cy));   // Top-Left - Create a ROI per quadrant
 Mat q1(magI, Rect(cx, 0, cx, cy));  // Top-Right
 Mat q2(magI, Rect(0, cy, cx, cy));  // Bottom-Left
 Mat q3(magI, Rect(cx, cy, cx, cy)); // Bottom-Right

 Mat tmp;                           // swap quadrants (Top-Left with Bottom-Right)
 q0.copyTo(tmp);
 q3.copyTo(q0);
 tmp.copyTo(q3);

 q1.copyTo(tmp);                    // swap quadrant (Top-Right with Bottom-Left)
 q2.copyTo(q1);
 tmp.copyTo(q2);

 normalize(magI, magI, 0, 1, NORM_MINMAX); // Transform the matrix with float values into a
                                         // viewable image form (float between values 0 and 1).
 dst = magI.clone();
}

对于step3,我使用移位的对数图像(在Fourier_transform函数最底部的“ dst”)将其与我的滤镜图像相乘。现在,对于步骤4,我想做这样的事情:

void inverse_fourier_transform(Mat& src, Mat& dst) {
   // stuff to do?
   idft(src, dst);
   imshow(dst);
}

src =傅立叶变换后的图像*过滤器

当我尝试运行此代码时,结果只是黑色图像。问题可能是“ src”仍处于对数比例,象限在Fourier_transform函数中可见,但象限已切换,但我不知道如何处理此问题。您能告诉我在使用idft()之前该怎么做吗?

0 个答案:

没有答案