W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
我們會為以下問題尋求答案:
您可以從這里下載,也可以在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 }
傅立葉變換將圖像分解為其竇道和余弦分量。換句話說,它會將圖像從其空間域轉(zhuǎn)換到其頻域。這個想法是任何函數(shù)可以用無限竇和余弦函數(shù)的和來精確地近似。傅立葉變換是一種如何做到這一點的方法。數(shù)學上二維圖像傅立葉變換是:
這里f是其空間域中的圖像值,在其頻域中是F。轉(zhuǎn)換的結(jié)果是復數(shù)??梢酝ㄟ^實際圖像和復雜圖像或通過幅度和相位圖像來顯示這一點。然而,在整個圖像處理算法中,只有幅面圖像很有趣,因為它包含了我們所需要的關(guān)于圖像幾何結(jié)構(gòu)的所有信息。然而,如果您打算以這些形式對圖像進行一些修改,那么您需要重新轉(zhuǎn)換它,您將需要保留這兩種形式。
在本例中,我將展示如何計算和顯示傅立葉變換的幅度圖像。在數(shù)字圖像是離散的情況下。這意味著它們可以占用給定域值的值。例如,在基本的灰度圖像中,值通常在0和255之間。因此,傅里葉變換也需要是離散傅里葉變換,導致離散傅里葉變換(DFT)。當您需要從幾何角度確定圖像的結(jié)構(gòu)時,您將需要使用它。以下是要遵循的步驟(在灰度輸入圖像I的情況下):
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));
附加的像素被初始化為零。
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
dft(complexI, complexI); // this way the result may fit in the source matrix
翻譯為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];
翻譯為OpenCV代碼:
magI += Scalar::all(1); // switch to logarithmic scale
log(magI, magI);
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);
normalize(magI, magI, 0, 1, NORM_MINMAX); // Transform the matrix with float values into a
// viewable image form (float between values 0 and 1).
應(yīng)用程序的想法將是確定圖像中存在的幾何取向。例如,讓我們來看一下文本是否是橫向的?看一些文字你會注意到,文本行的形式也是水平線,字母形成垂直線。在傅里葉變換的情況下,也可以看到文本片段的這兩個主要組成部分。讓我們使用這個水平和這個旋轉(zhuǎn)的圖像關(guān)于一個文本。
在水平文本的情況下:
在旋轉(zhuǎn)文本的情況下:
您可以看到頻域中最有影響力的組件(幅度圖像上最亮點)遵循圖像上對象的幾何旋轉(zhuǎn)。由此,我們可以計算偏移量并執(zhí)行圖像旋轉(zhuǎn)以糾正最終的錯誤對準。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: