这次介绍下opencv中DFT的使用,对应的例程是(EXAMPLE) dft。在图像处理领域,通过DFT可以将图像转换到频域,实现高通和低通滤波;还可以利用矩阵的卷积运算等同于其在频域的乘法运算从而优化算法降低运算量, 即先将图像转换到频域,然后做完乘法运算后,再转换到图像域,opencv中的模板匹配就利用了这一特性降低运算量。
下面是dft例程的源码
#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); // opencv中用来处理命令行参数的类
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;
}
int M = getOptimalDFTSize( img.rows ); // 获得最佳DFT尺寸,为2的次方
i