Sync with mango ho Ming
Discrete Fourier tranform, discrete Fourier transform//header file #include "opencv2/core.hpp"//core functionality, core function related #include "opencv2/ IMGPROC.HPP "//image processing, image processing related #include" opencv2/imgcodecs.hpp "//image file reading and writing, image loading and writing related #inc Lude "Opencv2/highgui.hpp"//high-level GUI, GUI related #include <iostream>/** * Program flow * 1, load image, format as grayscale * 2, get picture D FT Transform the best size * 3, the border plus 0 fill the picture, that is, 0 part of the DFT transformation of the best size * 4, create an array to store image real part of the imaginary part, and merge into Complexi * 5, Fourier transform DfT (Complexi, Complexi) * 6, re-separation of the real part
Imaginary part, and calculates the amplitude * 7, maps the amplitude to the log field * 8, divides the quadrant with the image Center as the origin, each quadrant creates an ROI * 9, Diagonal Quadrant Interchange * 10, displays the result *///namespace using namespace CV;
using namespace Std; Help function, output program information for static void Help (void) {cout << Endl << ' This programs demonstrated the use of the Discrete Fourier transform (DFT). "<< Endl//Discrete Fourier transform example <<" The DfT of an image is taken and it's power spectrum is displayed. " << Endl//discrete Fourier transform display power spectrum << "Usage:" << Endl << "./discrete_fourier_transform [Image_name--default. /data/lena.jpg] "<< Endl;
Default load Picture path} int main (int argc, char * * argv) {help (); Get Image Path (file name), command line input otherwise the default const char* filename = argc >=2? ARGV[1]: ".
/data/lena.jpg ";
Load the image by loading the grayscale graph Mat I = imread (filename, imread_grayscale);
Check if the successfully loaded if (I.empty ()) {cout << "Error opening image" << Endl;
return-1; }
//!
[Expand]
Mat padded;
Expand input image to optimal size, expands the inputs to an optimal size of int m = getoptimaldftsize (i.rows);
int n = getoptimaldftsize (i.cols); On the border add zero values, adding a value of 0 on the border, using the Copymakeborder function Copymakeborder (I, padded, 0, m-i.rows, 0, N-i.cols, B
Order_constant, Scalar::all (0)); //! [Expand]//! [Complex_and_real] Real and imaginary part Mat planes[] = {mat_<float> (padded), Mat::zeros (Padded.size (), cv_32f)}; Mat Array Store imageThe real part and the imaginary part Mat Complexi; Merge (Planes, 2, Complexi); Add to the expanded another plane with zeros, adding another plane to the extension with 0//! [Complex_and_real]//! [DFT]//discrete Fourier transform DFT (Complexi, Complexi); This means the result may fit in the source matrix, and the results of this method might be suitable for//! [DFT]//COMPUTE the magnitude and switch to logarithmic scales, calculate amplitude and map to logarithmic scale//formula = log (1 + sqrt (Re (DFT (I)) ^2 + Im (DFT (I)) ^2))//! [magnitude] amplitude split (complexi, planes); Planes[0] = Re (DFT (i), planes[1] = Im (DFT (i)), separation of real and imaginary parts magnitude (planes[0], planes[1], planes[0]);//planes[0] = Mag Nitude, calculated amplitude and deposited to planes[0] Mat MagI = planes[0]; Amplitude//! [Magnitude]//! [log] MagI + = Scalar::all (1);
Switch to logarithmic scales, mapped to logarithmic scale log (MagI, MagI); //! [Log]//! [Crop_rearrange] Crop reorder//crop the spectrum, if it has an odd number of rows or columns, clipping spectrum if it has odd rows or columns MagI = m AgI (Rect (0, 0, Magi.cols &-2, Magi.rows & 2)); Rearrange the quadrants of Fourier image so then the origin is at the image Center//re-arrange the quadrant of the Fourier image so the origin is in the center I
NT CX = MAGI.COLS/2;
int cy = MAGI.ROWS/2; Create a new ROI Mat q0 (MagI, Rect (0, 0, CX, CY) in each quadrant); Top-left-create a ROI per quadrant, upper left, second quadrant Mat Q1 (MagI, Rect (CX, 0, CX, CY)); Top-right, upper right, first quadrant Mat Q2 (MagI, Rect (0, CY, CX, CY)); Bottom-left, lower left, third quadrant Mat Q3 (MagI, Rect (CX, CY, CX, CY)); Bottom-right, lower right, fourth quadrant Mat tmp;
Swap quadrants (top-left with bottom-right), swap the upper left and lower right quadrant Q0.copyto (TMP);
Q3.copyto (q0);
Tmp.copyto (Q3); Q1.copyto (TMP);
Swap quadrant (Top-right with Bottom-left), swap right upper and lower left quadrant Q2.copyto (Q1);
Tmp.copyto (Q2); //! [Crop_rearrange]//! [Normalize]//normalization, pixel values are mapped to [0,1] between 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]//display result Imshow ("Input Image", I);
Show The result imshow ("Spectrum magnitude", magI);
Waitkey ();
return 0; }/** * Essentials Summary: * Load picture format for grayscale * getoptimaldftsize () function get optimal size * copymakeborder () Add box function * Real part virtual part * Merge () Merge function * DFT () function * Amplitude Formula sqrt (Re (DFT (i)) ^2 + Im (DFT (i)) ^2) * Split () separation function * magnitude () calculated amplitude * log () logarithmic function * normalize () normalization function */