废话不多说,直接上代码,代码中我都注明了注释,有些讲不清楚的,会在代码的后面专门拿出来讲。
下面这个cpp文件不是我自己写的程序,是opencv提供的关于dft变换的例程,文件一般会包含在你的opencv路径下opencv\sources\samples\cpp下面,可以自行查找。
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdio.h>
using namespace cv;
using namespace std;
static void help()
{
printf("\nThis program demonstrated the use of the discrete Fourier transform (dft)\n"
"The dft of an image is taken and it's power spectrum is displayed.\n"
"Usage:\n"
"./dft [image_name -- default lena.jpg]\n");
}
const char* keys =
{
"{1| |lena.jpg|input image file}"
};
int main(int argc, const char ** argv)
{
help();
CommandLineParser parser(argc, argv, keys);
string filename = parser.get<string>("1");
Mat img = imread(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
if( img.empty() )
{
help();
printf("Cannot read image file: %s\n", filename.c_str());
return -1;
}
//这里获取符合快速傅里叶变换(FFT)的大小,m和n都可以分解为2、3、5相乘的组合,参见 注1
int M = getOptimalDFTSize( img.rows );
int N = getOptimalDFTSize( img.cols );
Mat padded;
//将原图像的大小