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.

different filters and see their effect. The full code can be downloaded here: fourier2d.cpp.This code doesn't contain much new, it's much more interesting to run it and study the results of thefilters.Once again, first come all the declarations of functions and variables. f2 and F2 will become the buffersfor the filtered versions of the original image and spectrum.//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 FFT2D(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);The main function loads a PNG image first, and will then start the first loop, the loop that lets you tryout different modified versions of the PNG image and some other generated signals:int main(int /*argc*/, char */*argv*/[]){screen(3 * N,4 * M + 8, 0,"2D FFT and Filters");std::vector img;unsigned long dummyw, dummyh;if(loadImage(img, dummyw, dummyh, "pics/test.png")){print("image pics/test.png not found");redraw();sleep();cls();}//set signal to the imagefor(int x = 0; x < N; x++)for(int y = 0; y < M; y++){fRe[x][y][0] = img[N * y + x].r;fRe[x][y][1] = img[N * y + x].g;fRe[x][y][2] = img[N * y + x].b;}int ytrans=8; //translate everything a bit down to put the text on top//set new FT buffersfor(int x = 0; x < N; x++)for(int y = 0; y < M; y++)for(int c = 0; c < 3; c++){FRe2[x][y][c] = FRe[x][y][c];FIm2[x][y][c] = FIm[x][y][c];}bool changed = 1, endloop = 0;while(!endloop){if(done()) end();readKeys();Here's a small part of the input part of the first loop, it looks pretty messy here, only a few keys are

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

Saved successfully!

Ooh no, something went wrong!