12.07.2015 Views

Lode's Computer Graphics Tutorial Fourier Transform

Lode's Computer Graphics Tutorial Fourier Transform

Lode's Computer Graphics Tutorial Fourier Transform

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

in the signal:2D <strong>Fourier</strong> <strong>Transform</strong> on ImagesThe extension of the <strong>Fourier</strong> <strong>Transform</strong> to 2D is actually pretty simple. First you take the 1D FT ofevery row of the image, and then on this result you take the 1D FT of every column.Here follows the code of a program that does exactly that, and two alternative functions that do thesame are given in it: one will use the 2D version of the slow DFT, and the other the 2D version of theFast <strong>Fourier</strong> <strong>Transform</strong> (FFT). The program will first calculate the FT of the image, and then calculatethe Inverse FT of the result, to check if the formula is working correctly: if it gives back the original itworks. You can freely change between calls to DFT2D() and FFT2D() in the main function, andyou'll notice that the DFT2D() function is very slow now, while the FFT2D() function works veryfast.Because an RGB image has 3 color channels, the FT is calculated for each color channel separately, soin fact 3 greyscale FT's are calculated.The program requires that there's a 24-bit color, 128*128 bitmap image pics/test.png (path relative tothe program).First again all the variables and functions are declared.N and M are the width and height of the image in pixels.The arrays for the signal and it's spectrum are now 3-dimensional: 2 dimensions for the size, and 1dimension for the RGB color components. The class ColorRGB can't be used here because moreprecision is required for FT, that's why the color components are put in a double array instead; index 0represents red, index 1 green, and index 2 is blue.There are two versions of the FT functions here, the slow DFT2D and the fast FFT2D.//yeah, we're working with fixed sizes again...const int N = 128; //the width of the imageconst int M = 128; //the height of the imagedouble fRe[N][M][3], fIm[N][M][3], fAmp[N][M][3]; //the signal's real part, imaginary part, and amplitudedouble FRe[N][M][3], FIm[N][M][3], FAmp[N][M][3]; //the FT's real part, imaginary part and amplitudedouble fRe2[N][M][3], fIm2[N][M][3], fAmp2[N][M][3]; //will become the signal again after IDFT of the spectrumdouble FRe2[N][M][3], FIm2[N][M][3], FAmp2[N][M][3]; //filtered spectrumdouble pi = 3.1415926535897932384626433832795;void draw(int xpos, int yPos, int n, int m, double *g, bool shift, bool neg128);void DFT2D(int n, int m, bool inverse, double *gRe, double *gIm, double *GRe, double *GIm);void calculateAmp(int n, int m, double *ga, double *gRe, double *gIm);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!