OpenCV離散傅里葉變換

2018-08-30 11:25 更新

目標(biāo)

我們會(huì)為以下問題尋求答案:

源代碼

您可以從這里下載,也可以在samples/cpp/tutorial_code/core/discrete_fourier_transform/discrete_fourier_transform.cppOpenCV源代碼庫中找到它。

以下是cv :: dft()的示例用法:

 #include "opencv2/core.hpp"
    2 #include "opencv2/imgproc.hpp"
    3 #include "opencv2/imgcodecs.hpp"
    4 #include "opencv2/highgui.hpp"
    5 
    6 #include <iostream>
    7 
    8 using namespace cv;
    9 using namespace std;
   10 
   11 static void help(char* progName)
   12 {
   13     cout << endl
   14         <<  "This program demonstrated the use of the discrete Fourier transform (DFT). " << endl
   15         <<  "The dft of an image is taken and it's power spectrum is displayed."          << endl
   16         <<  "Usage:"                                                                      << endl
   17         << progName << " [image_name -- default ../data/lena.jpg] "               << endl << endl;
   18 }
   19 
   20 int main(int argc, char ** argv)
   21 {
   22     help(argv[0]);
   23 
   24     const char* filename = argc >=2 ? argv[1] : "../data/lena.jpg";
   25 
   26     Mat I = imread(filename, IMREAD_GRAYSCALE);
   27     if( I.empty())
   28         return -1;
   29 
   30     Mat padded;                            //expand input image to optimal size
   31     int m = getOptimalDFTSize( I.rows );
   32     int n = getOptimalDFTSize( I.cols ); // on the border add zero values
   33     copyMakeBorder(I, padded, 0, m - I.rows, 0, n - I.cols, BORDER_CONSTANT, Scalar::all(0));
   34 
   35     Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)};
   36     Mat complexI;
   37     merge(planes, 2, complexI);         // Add to the expanded another plane with zeros
   38 
   39     dft(complexI, complexI);            // this way the result may fit in the source matrix
   40 
   41     // compute the magnitude and switch to logarithmic scale
   42     // => log(1 + sqrt(Re(DFT(I))^2 + Im(DFT(I))^2))
   43     split(complexI, planes);                   // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))
   44     magnitude(planes[0], planes[1], planes[0]);// planes[0] = magnitude
   45     Mat magI = planes[0];
   46 
   47     magI += Scalar::all(1);                    // switch to logarithmic scale
   48     log(magI, magI);
   49 
   50     // crop the spectrum, if it has an odd number of rows or columns
   51     magI = magI(Rect(0, 0, magI.cols & -2, magI.rows & -2));
   52 
   53     // rearrange the quadrants of Fourier image  so that the origin is at the image center
   54     int cx = magI.cols/2;
   55     int cy = magI.rows/2;
   56 
   57     Mat q0(magI, Rect(0, 0, cx, cy));   // Top-Left - Create a ROI per quadrant
   58     Mat q1(magI, Rect(cx, 0, cx, cy));  // Top-Right
   59     Mat q2(magI, Rect(0, cy, cx, cy));  // Bottom-Left
   60     Mat q3(magI, Rect(cx, cy, cx, cy)); // Bottom-Right
   61 
   62     Mat tmp;                           // swap quadrants (Top-Left with Bottom-Right)
   63     q0.copyTo(tmp);
   64     q3.copyTo(q0);
   65     tmp.copyTo(q3);
   66 
   67     q1.copyTo(tmp);                    // swap quadrant (Top-Right with Bottom-Left)
   68     q2.copyTo(q1);
   69     tmp.copyTo(q2);
   70 
   71     normalize(magI, magI, 0, 1, NORM_MINMAX); // Transform the matrix with float values into a
   72                                             // viewable image form (float between values 0 and 1).
   73 
   74     imshow("Input Image"       , I   );    // Show the result
   75     imshow("spectrum magnitude", magI);
   76     waitKey();
   77 
   78     return 0;
   79 }

說明

傅立葉變換將圖像分解為其竇道和余弦分量。換句話說,它會(huì)將圖像從其空間域轉(zhuǎn)換到其頻域。這個(gè)想法是任何函數(shù)可以用無限竇和余弦函數(shù)的和來精確地近似。傅立葉變換是一種如何做到這一點(diǎn)的方法。數(shù)學(xué)上二維圖像傅立葉變換是:

OpenCV離散傅里葉變換

這里f是其空間域中的圖像值,在其頻域中是F。轉(zhuǎn)換的結(jié)果是復(fù)數(shù)。可以通過實(shí)際圖像和復(fù)雜圖像或通過幅度和相位圖像來顯示這一點(diǎn)。然而,在整個(gè)圖像處理算法中,只有幅面圖像很有趣,因?yàn)樗宋覀兯枰年P(guān)于圖像幾何結(jié)構(gòu)的所有信息。然而,如果您打算以這些形式對(duì)圖像進(jìn)行一些修改,那么您需要重新轉(zhuǎn)換它,您將需要保留這兩種形式。

在本例中,我將展示如何計(jì)算和顯示傅立葉變換的幅度圖像。在數(shù)字圖像是離散的情況下。這意味著它們可以占用給定域值的值。例如,在基本的灰度圖像中,值通常在0和255之間。因此,傅里葉變換也需要是離散傅里葉變換,導(dǎo)致離散傅里葉變換(DFT)。當(dāng)您需要從幾何角度確定圖像的結(jié)構(gòu)時(shí),您將需要使用它。以下是要遵循的步驟(在灰度輸入圖像I的情況下):


  • 將圖像展開至最佳尺寸。DFT的性能取決于圖像大小。對(duì)于數(shù)字二,三和五的倍數(shù),圖像尺寸趨向于最快。因此,為了獲得最大的性能,通常最好將邊框值填充到圖像以獲得具有這種特征的大小。該品種:: getOptimalDFTSize()返回這個(gè)最佳規(guī)模,我們可以使用CV :: copyMakeBorder()函數(shù)來擴(kuò)大圖像的邊界:
Mat padded;                            //expand input image to optimal size
int m = getOptimalDFTSize( I.rows );
int n = getOptimalDFTSize( I.cols ); // on the border add zero pixels
copyMakeBorder(I, padded, 0, m - I.rows, 0, n - I.cols, BORDER_CONSTANT, Scalar::all(0));

附加的像素被初始化為零。

  • 為復(fù)雜和真實(shí)的價(jià)值取得成就。傅里葉變換的結(jié)果是復(fù)雜的。這意味著對(duì)于每個(gè)圖像值,結(jié)果是兩個(gè)圖像值(每個(gè)分量一個(gè))。此外,頻域范圍遠(yuǎn)遠(yuǎn)大于其空間對(duì)應(yīng)物。因此,我們通常至少以浮動(dòng)格式存儲(chǔ)它們。因此,我們將把我們的輸入圖像轉(zhuǎn)換為這種類型,并用另一個(gè)通道來展開,以保持復(fù)雜的值:

Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)};
Mat complexI;
merge(planes, 2, complexI);         // Add to the expanded another plane with zeros

  • 進(jìn)行離散傅里葉變換??赡艿木偷赜?jì)算(與輸出相同的輸入):

dft(complexI, complexI);            // this way the result may fit in the source matrix

  • 將真實(shí)和復(fù)雜的值轉(zhuǎn)化為大小。復(fù)數(shù)具有真實(shí)(Re)和復(fù)數(shù)(虛數(shù)Im)部分。DFT的結(jié)果是復(fù)數(shù)。DFT的大小是:

OpenCV離散傅里葉變換

翻譯為OpenCV代碼:

split(complexI, planes);                   // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))
magnitude(planes[0], planes[1], planes[0]);// planes[0] = magnitude
Mat magI = planes[0];

  • 切換到對(duì)數(shù)刻度。原來,傅里葉系數(shù)的動(dòng)態(tài)范圍太大,無法顯示在屏幕上。我們有一些小而高的變化值,我們不能這樣觀察。因此,高價(jià)值將全部作為白點(diǎn),而小的則為黑色。為了將灰度值用于可視化,我們可以將我們的線性比例變換為對(duì)數(shù):

OpenCV離散傅里葉變換

翻譯為OpenCV代碼:

magI += Scalar::all(1);                    // switch to logarithmic scale
log(magI, magI);

  • 作物和重新排列。記住,在第一步,我們擴(kuò)大了形象?那么現(xiàn)在是摒棄新推出的價(jià)值觀的時(shí)候了。為了可視化的目的,我們還可以重新排列結(jié)果的象限,使原點(diǎn)(零,零)對(duì)應(yīng)于圖像中心。

magI = magI(Rect(0, 0, magI.cols & -2, magI.rows & -2));
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);

  • 規(guī)范化。這再次為可視化目的而完成。我們現(xiàn)在有這樣的大小,但是這仍然是我們的圖像顯示范圍從零到一。我們使用cv :: normalize()函數(shù)將值歸一化到此范圍。

normalize(magI, magI, 0, 1, NORM_MINMAX); // Transform the matrix with float values into a
                                          // viewable image form (float between values 0 and 1).

結(jié)果

應(yīng)用程序的想法將是確定圖像中存在的幾何取向。例如,讓我們來看一下文本是否是橫向的?看一些文字你會(huì)注意到,文本行的形式也是水平線,字母形成垂直線。在傅里葉變換的情況下,也可以看到文本片段的這兩個(gè)主要組成部分。讓我們使用這個(gè)水平這個(gè)旋轉(zhuǎn)的圖像關(guān)于一個(gè)文本。

在水平文本的情況下:

OpenCV離散傅里葉變換

在旋轉(zhuǎn)文本的情況下:

OpenCV離散傅里葉變換

您可以看到頻域中最有影響力的組件(幅度圖像上最亮點(diǎn))遵循圖像上對(duì)象的幾何旋轉(zhuǎn)。由此,我們可以計(jì)算偏移量并執(zhí)行圖像旋轉(zhuǎn)以糾正最終的錯(cuò)誤對(duì)準(zhǔn)。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)