具體代碼如下:
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等分,互相對調,使低頻部分位於圖像中心,也就是讓頻域原點位於中心;
中心的亮點反映的是圖像的低頻信息,也就是圖像的平滑部分,因爲平滑的部分所佔圖像的比例較高,故能量較高,而圖像的邊緣的信息,也就是高頻信息,即圖像突變比較多的地方相當較少,因此能量低,而能量越高,而在頻譜圖中所表現出來的就越亮。