C 中的 2D 傅立叶变换 [英] 2D Fourier Transformation in C
问题描述
我使用此站点的公式实现了 2D DFT 和 IDFThttp://homepages.inf.ed.ac.uk/rbf/HIPR2/fourier.htm我认为这些是正确的并且很好地解释了.实现看起来像这样:
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;
}
}
频域 (sqrt(ak * ak+bk * bk)) 看起来不像它应该的那样,并且图像重建(忽略虚部)没有使原始图像附近有任何东西.[0][0] 处的更多像素具有极高的值,并且没有像素范围从 0 到 255 作为原始像素.我做错了什么?
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?
额外信息:
- inImage 和 DFTImages 只是构造原始 *.pgm 图像的结构,保存和加载图像有效,
- 我不能使用任何类(比如虚数),因为这个实现将在 GPU 端,
谢谢
推荐答案
我找到了解决问题的方法.这只是索引问题.将 ii 和 jj 相加求出傅立叶变换
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;
}
}
这篇关于C 中的 2D 傅立叶变换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!