在opencv官网上有傅立叶变换的实例,但其没写怎么变换回来,所以这里将其逆表示出来
代码如下:
// opencv_dft.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <OpenCV245.h>
using namespace std;
using namespace cv;
int _tmain(int argc, _TCHAR* argv[])
{
Mat src;
Mat Image = imread("C:\\Users\\sony\\Desktop\\111.png", 0);
if (Image.empty())
return -1;
Mat padded;
int m = getOptimalDFTSize(Image.rows);
int n = getOptimalDFTSize(Image.cols);
copyMakeBorder(Image, padded, 0, m - Image.rows, 0, n - Image.cols, BORDER_CONSTANT, Scalar::all(0));
Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)};
Mat iplanes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)};
Mat complexI;
merge(planes, 2, complexI);
dft(complexI, complexI);
dft(complexI, src, DFT_SCALE|DFT_INVERSE);
split(complexI, planes);
magnitude(planes[0], planes[1], planes[0]);
Mat magI = planes[0];
split(src, iplanes);
magnitude(iplanes[0], iplanes[1], iplanes[0]);
Mat imagI = iplanes[0];
magI += Scalar::all(1);
log(magI, magI);
magI = magI(Rect(0, 0, magI.cols & -2, magI.rows & -2));
Mat O = magI.clone();
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, CV_MINMAX);
normalize(O, O, 0, 1, CV_MINMAX);
normalize(imagI, imagI, 0, 1, CV_MINMAX);
imshow("input Image", Image);
imshow("spectrum magnitude", magI);
imshow("0", O);
/*cout<<src<<endl;*/
imshow("src",imagI);
waitKey(0);
return 0;
}