问题
I implemented 2D DFT and IDFT using equation from this site http://homepages.inf.ed.ac.uk/rbf/HIPR2/fourier.htm I think these are correct and nicely explained. Implementation looks like that:
for(int i=0;i<inImage.width;i++)
{
for(int j=0;j<inImage.height;j++)
{
float ak=0;
float bk=0;
for(int ii=0;ii<inImage.width;ii++)
{
for(int jj=0;jj<inImage.height;jj++)
{
float x=-2.0*PI*i*ii/(float)inImage.width;
float y=-2.0*PI*j*jj/(float)inImage.height;
// ak+=inImage.pixels[i][j]*(cos(x)*cos(y)-sin(x)*sin(y));
// bk+=inImage.pixels[i][j]*(sin(x)*cos(y)+sin(y)*cos(x));
ak+=inImage.pixels[i][j]*cos(x+y);
bk+=inImage.pixels[i][j]*1.0*sin(x+y);
}
}
DFTImageRE.pixels[i][j]=ak;
DFTImageIM.pixels[i][j]=bk;
}
}
The frequency domain (sqrt(ak * ak+bk * bk)) doesnt look as it should, and the image reconstruction (ignoring the imaginary parts) doesnt make anything near the original image. What is more pixel at [0][0] have extremely high value and no pixels range from 0 to 255 as the original one. What am i doing wrong?
Extra information:
- inImage and DFTImages are just struct from which oridinary *.pgm image are construted, saving and loading images works,
- i cant use any classes (like imaginary numbers) because this implementation will be on GPU side,
Thanks
回答1:
I found a solution for my problem. It was just indexing problem. Use ii and jj in sum to find a Fourier Transform
for(int i=0;i<inImage.width;i++)
{
for(int j=0;j<inImage.height;j++)
{
float ak=0;
float bk=0;
for(int ii=0;ii<inImage.width;ii++)
{
for(int jj=0;jj<inImage.height;jj++)
{
float x=-2.0*PI*i*ii/(float)inImage.width;
float y=-2.0*PI*j*jj/(float)inImage.height;
ak+=inImage.pixels[ii][jj]*cos(x+y);
bk+=inImage.pixels[ii][jj]*1.0*sin(x+y);
}
}
DFTImageRE.pixels[i][j]=ak;
DFTImageIM.pixels[i][j]=bk;
}
}
来源:https://stackoverflow.com/questions/38542412/2d-fourier-transformation-in-c