C ++图像2D快速傅里叶变换

时间:2017-12-31 01:06:35

标签: c++ fft ifft cimg

我必须在图像上实现2D FFT变换(我不能使用库来为我做这些 - 课程的一部分)。我使用CImg来加载和保存图像。我制作了以下代码:

CImg<Complex> FastFourier(CImg<unsigned char> &originalImage)
{
    //check size in the main.cpp
    CImg<Complex> resultantImage = TransformToComplex(originalImage);
    vector< vector< vector< Complex > > > vectorImage = imageToVector(resultantImage);
    //cout << "Transform to complex" << endl;
    int size = originalImage.width();

    for(int i = 0; i < size; i++)
        FastFourier1D(vectorImage[i], false);

    vectorImage = rotateVector(vectorImage);

    for(int i = 0; i < size; i++)
       FastFourier1D(vectorImage[i], false);

    vectorImage = rotateVector(vectorImage);

    resultantImage = vectorToImage(vectorImage);

    return resultantImage;
}

void FastFourier1D(vector< vector< Complex > > &input, bool inverse)
{
    int size = input.size();
    double angle;

    if(size <= 1)
        return;

    int channels = input[0].size();
    vector< vector< Complex > > even;
    vector< vector< Complex > > odd;

    for(int i = 0; i < size; i+=2)
    {
        vector< Complex > tempEven;
        vector< Complex > tempOdd;
        for(int channelIterator = 0; channelIterator < channels; channelIterator++)
        {
            tempEven.push_back(input[i][channelIterator]);
            tempOdd.push_back(input[i + 1][channelIterator]);
        }

        even.push_back(tempEven);
        odd.push_back(tempOdd);
    }

    FastFourier1D(even, inverse);
    FastFourier1D(odd, inverse);

    for(int channelIterator = 0; channelIterator < channels; channelIterator++)
    {
        for(int i = 0; i < size / 2; i++)
        {
           if(inverse == false)
               angle = -2.0 * (double)PI * (double)i / (double)size;
           else
               angle = 2.0 * (double)PI * (double)i / (double)size;

           double real = cos(angle);
           double imaginary = sin(angle);

           Complex W;
           W.setRP(real);
           W.setIP(imaginary);

           W = W * odd[i][channelIterator];

           input[i][channelIterator] = even[i][channelIterator] + W;
           input[(size / 2) + i][channelIterator] = even[i][channelIterator] - W;
       }
    }
}

然而结果并不好。输入图片: Input image

FFT(没有任何变换): FFT (without any transform)

逆FFT:

Inverse FFT

正如你所看到的,它有lena的颜色,但看起来不像lena。你可以帮帮我吗?有什么错吗?

1 个答案:

答案 0 :(得分:0)

我发现答案是我的Complex类中的乘法运算符的错误实现。

Complex Complex::operator*(const Complex& a)
{
    Complex number;
    double RP = realPart * a.getRP() - imaginaryPart * a.getIP(); // this line was wrong
    double IP = realPart * a.getIP() + imaginaryPart * a.getRP();
    number.setRP(RP);
    number.setIP(IP);
    return number;
}

real part我忘记了minus。现在整个实现工作正常,并且成功地将图像转换为频域,并将其反转为空间域。