DFT implementation in C ++


I want to implement DFT in C ++ for image processing.
formula for 1D DFT
When I studied this theory, I found out that I can split 2D DFT into two parts of 1D DFT . First, for each row, I do 1D DFT, then I do it for each column. Of course, I have to do operatioins on complex numbers .
Some problems arise here, because I'm not sure where to use the real, and where the imaginary part of the complex number. I somewhere found that the pixel values โ€‹โ€‹of the input image I should consider as the real part, and the imaginary part as 0.
I made an implementation of this, but I believe that the image of the result is incorrect.
lenaclenac_dft
I would appreciate it if someone would help me with this. To read and save images, I use the CImg library.

void DFT (CImg<unsigned char> image) { int w=512; int h=512; int rgb=3; complex <double> ***obrazek=new complex <double>**[w]; for (int b=0;b<w;b++) //making 3-dimensional table to store DFT values { obrazek[b]=new complex <double>*[h]; for (int a=0;a<h;a++) { obrazek[b][a]=new complex <double>[rgb]; } } CImg<unsigned char> kopia(image.width(),image.height(),1,3,0); complex<double> sum=0; complex<double> sum2=0; double pi = 3.14; for (int i=0; i<512; i++){ for (int j=0; j<512; j++){ for (int c=0; c<3; c++){ complex<double> cplx(image(i,j,c), 0); obrazek[i][j][c]=cplx; }}} for (int c=0; c<3; c++) //for rows { for (int y=0; y<512; y++) { sum=0; for (int x=0; x<512; x++) { sum+=(obrazek[x][y][c].real())*cos((2*pi*x*y)/512)-(obrazek[x][y][c].imag())*sin((2*pi*x*y)/512); obrazek[x][y][c]=sum; } } } for (int c=0; c<3; c++) //for columns { for (int y=0; y<512; y++)//r { sum2=0; for (int x=0; x<512; x++) { sum2+=(obrazek[y][x][c].real())*cos((2*pi*x*y)/512)-(obrazek[y][x][c].imag())*sin((2*pi*x*y)/512); obrazek[y][x][c]=sum2; } } } for (int i=0; i<512; i++){ for (int j=0; j<512; j++){ for (int c=0; c<3; c++){ kopia(i,j,c)=obrazek[i][j][c].real(); }}} CImgDisplay image_disp(kopia,"dft"); while (!image_disp.is_closed() ) { image_disp.wait(); } saving(kopia); } 
+4
source share
2 answers

Take a look at this page. This may be useful:
http://paulbourke.net/miscellaneous/dft/

There is a DFT implementation (about 40 lines).

0
source

I suggest using the Ooura FFT package:

http://www.kurims.kyoto-u.ac.jp/~ooura/fft.html

This has been proven over the years of use in my audio applications and is fast as hell!

0
source

Source: https://habr.com/ru/post/1390105/


All Articles