我试着让傅里叶变换起作用,我必须做一个赋值,我想我已经做到了,我不确定为什么它不起作用。我认为这与复数有关,因为涉及到了'i‘。我看过很多参考资料,我理解这个公式,但我在编程时遇到了问题。这是我到目前为止所拥有的
void NaiveDFT::Apply( Image & img )
{
//make the fourier transform using the naive method and set that to the image.
Image dft(img);
Pixel ** dftData = dft.GetImageData();
Pixel ** imgData = img.GetImageData();
for(unsigned u = 0; u < img.GetWidth(); ++u)
{
for(unsigned v = 0; v < img.GetHeight(); ++v)
{
std::complex<double> sum = 0;
for(unsigned x = 0; x < img.GetWidth(); ++x)
{
for(unsigned y = 0; y < img.GetHeight(); ++y)
{
std::complex<double> i = sqrt(std::complex<double>(-1));
std::complex<double> theta = 2 * M_PI * (((u * x) / img.GetWidth()) + ((v * y) / img.GetHeight()));
sum += std::complex<double>(imgData[x][y]._red) * cos(theta) + (-i * sin(theta));
//sum += std::complex<double>(std::complex<double>(imgData[x][y]._red) * pow(EULER, -i * theta));
}
}
dftData[u][v] = (sum.imag() / (img.GetWidth() * img.GetHeight()));
}
}
img = dft;
}我有一些测试图像,我正在测试它,我要么得到一个全黑的图像,要么像一个全灰色的图像。
我还尝试了求和e^(-i*2*PI*(x*u*width + y*v* height ) * 1/width *height,虽然它仍然不是期望的输出,但得到的结果与预期相同。
我也尝试过sum.real()方法,但看起来也不太对劲
如果有人有任何建议或者可以给我指明正确的方向,那就太好了,在这一点上,我只是不断尝试不同的东西,并检查输出,直到我得到我应该得到的东西。
谢谢。
发布于 2013-11-18 14:17:02
我认为在复数项的乘法过程中可能会出现问题。这行代码:
sum += std::complex<double>(imgData[x][y]._red) * cos(theta) + (-i * sin(theta));应该是:
sum += std::complex<double>(imgData[x][y]._red) * ( cos(theta) + -i * sin(theta));此外,在计算theta时,您需要使用双精度:
std::complex<double> theta = 2 * M_PI * ((((double)u * x) / (double)(img.GetWidth())) + (((double)v * y) / (double)(img.GetHeight())));https://stackoverflow.com/questions/20040982
复制相似问题