Tutorial Discrete Fourier Transform
This commit is contained in:
+20
-4
@@ -8,45 +8,58 @@
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
static void help(char* progName)
|
||||
static void help(void)
|
||||
{
|
||||
cout << endl
|
||||
<< "This program demonstrated the use of the discrete Fourier transform (DFT). " << endl
|
||||
<< "The dft of an image is taken and it's power spectrum is displayed." << endl
|
||||
<< "Usage:" << endl
|
||||
<< progName << " [image_name -- default ../data/lena.jpg] " << endl << endl;
|
||||
<< "./discrete_fourier_transform [image_name -- default ../data/lena.jpg]" << endl;
|
||||
}
|
||||
|
||||
int main(int argc, char ** argv)
|
||||
{
|
||||
help(argv[0]);
|
||||
help();
|
||||
|
||||
const char* filename = argc >=2 ? argv[1] : "../data/lena.jpg";
|
||||
|
||||
Mat I = imread(filename, IMREAD_GRAYSCALE);
|
||||
if( I.empty())
|
||||
if( I.empty()){
|
||||
cout << "Error opening image" << endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
//! [expand]
|
||||
Mat padded; //expand input image to optimal size
|
||||
int m = getOptimalDFTSize( I.rows );
|
||||
int n = getOptimalDFTSize( I.cols ); // on the border add zero values
|
||||
copyMakeBorder(I, padded, 0, m - I.rows, 0, n - I.cols, BORDER_CONSTANT, Scalar::all(0));
|
||||
//! [expand]
|
||||
|
||||
//! [complex_and_real]
|
||||
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
|
||||
//! [complex_and_real]
|
||||
|
||||
//! [dft]
|
||||
dft(complexI, complexI); // this way the result may fit in the source matrix
|
||||
//! [dft]
|
||||
|
||||
// compute the magnitude and switch to logarithmic scale
|
||||
// => log(1 + sqrt(Re(DFT(I))^2 + Im(DFT(I))^2))
|
||||
//! [magnitude]
|
||||
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];
|
||||
//! [magnitude]
|
||||
|
||||
//! [log]
|
||||
magI += Scalar::all(1); // switch to logarithmic scale
|
||||
log(magI, magI);
|
||||
//! [log]
|
||||
|
||||
//! [crop_rearrange]
|
||||
// crop the spectrum, if it has an odd number of rows or columns
|
||||
magI = magI(Rect(0, 0, magI.cols & -2, magI.rows & -2));
|
||||
|
||||
@@ -67,9 +80,12 @@ int main(int argc, char ** argv)
|
||||
q1.copyTo(tmp); // swap quadrant (Top-Right with Bottom-Left)
|
||||
q2.copyTo(q1);
|
||||
tmp.copyTo(q2);
|
||||
//! [crop_rearrange]
|
||||
|
||||
//! [normalize]
|
||||
normalize(magI, magI, 0, 1, NORM_MINMAX); // Transform the matrix with float values into a
|
||||
// viewable image form (float between values 0 and 1).
|
||||
//! [normalize]
|
||||
|
||||
imshow("Input Image" , I ); // Show the result
|
||||
imshow("spectrum magnitude", magI);
|
||||
|
||||
Reference in New Issue
Block a user