具体代码如下:
int main(int argc,char *argv[])
{
Mat image = imread("/Users/hanoi/Desktop/lena.bmp",0);
printf("width=%d,height=%d\n",image.rows,image.cols);
Mat padded;
int m = getOptimalDFTSize(image.rows); // Return size of 2^x that suite for FFT
int n = getOptimalDFTSize(image.cols);
// Padding 0, result is @padded
copyMakeBorder(image, padded, 0, m-image.rows, 0, n-image.cols, BORDER_CONSTANT, Scalar::all(0));
// Create planes to storage REAL part and IMAGE part, IMAGE part init are 0
Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F) };
Mat complexI;
merge(planes, 2, complexI);
dft(complexI, complexI);
// compute the magnitude and switch to logarithmic scale
split(complexI, planes);
magnitude(planes[0], planes[0], planes[1]);
Mat magI = planes[0];
// => log(1+sqrt(Re(DFT(I))^2+Im(DFT(I))^2))
magI += Scalar::all(1);
log(magI, magI);
// crop the spectrum
magI = magI(Rect(0, 0, magI.cols & (-2), magI.rows & (-2)));
Mat _magI = magI.clone();
normalize(_magI, _magI, 0, 1, CV_MINMAX);
// 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
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
// exchange Top-Left and Bottom-Right
Mat tmp;
q0.copyTo(tmp);
q3.copyTo(q0);
tmp.copyTo(q3);
// exchange Top-Right and Bottom-Left
q1.copyTo(tmp);
q2.copyTo(q1);
tmp.copyTo(q2);
normalize(magI, magI, 0, 1, CV_MINMAX);
Mat magImg(magI.size(),CV_8UC1);
magI.convertTo(magImg, CV_8UC1,255,0);
imwrite("/Users/hanoi/Desktop/lena11.bmp", magImg);
/*
imshow("Input image", image);
imshow("Spectrum magnitude before shift frequency", _magI);
imshow("Spectrum magnitude after shift frequency", magI);
Mat myImage;
convertScaleAbs(magI, myImage);
double min = 0.0;
double max = 0.0;
minMaxLoc(magI, &min, &max);
double scale = 255.0 / (max - min);
double shift = -min * scale;
convertScaleAbs(magI, myImage,scale,shift);
imwrite("/Users/hanoi/Desktop/lena11.bmp", myImage);
waitKey();
*/
return 0;
}
图像的频率是表征图像中灰度变化剧烈的程度的指标,是灰度在平面空间上的梯度,图像的边缘部分是突变部分,变化较快,因此反应在频域上的是高频分量,图像的噪声大部分情况下是高频部分,图像大部分平缓的灰度变化部分则为低频分量,也就是说,傅立叶变换提供了另外一个角度观察图像,可以将图像从灰度部分转化到频率分布来观察图像的特征。
频域在图像处理中,就我目前知道的用途,是图像的压缩和图像的去燥;
DFT算法的原理要求输入信号的长度是2^n,这样就可以以快速使用傅立叶变化算法(FFT)进行加速,所以程序中使用:
copyMakeBorder(image,
padded,
0, m-image.rows,
0, n-image.cols,
BORDER_CONSTANT,
Scalar::all(0));
该函数完成对src边缘的扩充,将图像变大,然后自动填充图像的边界。
对于一维信号,原DFT直接运算的复杂度是O(N^2),而快速傅立叶变换的复杂度是O(Nlog2(N));
由DFT的性质知道,输入为实信号(图像)的时候,频域输出为复数,因此将频域信息分为幅值和相位,而频域的幅值高的代表高频分量,幅值低的代表低频分量。
DFT要分别计算实部和虚部,把要处理的图像作为输入的实部,一个全零的图像作为输入的虚部,dft()输入和输出应该分别为单张图像,所以要先用merge()把实部和徐部图像合并,分别处于图像的2个通道内,计算得到的实部和虚部仍然保存在2个通道之内;
一般都会用幅度图像来表示图像傅立叶变换的结果(傅立叶谱)
幅度的计算公式: magnitude = sqrt(Re(DFT)^2 + Im(DFT)^2);
由于幅度的变化范围很大,而一般的图像亮度范围只有[0,255],容易造成一大片漆黑,只有几个点很亮,所以要使用log函数把数值的范围缩小;
dft()直接获取的结果中,低频部分位于四角,高频部分位于中间,习惯上会把图像做成4等分,互相对调,使低频部分位于图像中心,也就是让频域原点位于中心;
中心的亮点反映的是图像的低频信息,也就是图像的平滑部分,因为平滑的部分所占图像的比例较高,故能量较高,而图像的边缘的信息,也就是高频信息,即图像突变比较多的地方相当较少,因此能量低,而能量越高,而在频谱图中所表现出来的就越亮。