Documentation‎ > ‎

4. Fourier Transform



#include <iostream>
#include "image/image.hpp"
struct myfilter
{

    void operator()(float& value,int* l)
    {
        if(l[0]*l[0] + l[1]*l[1] > 10000)
            value = 1.0;
    }

};

template<typename vtor_type,size_t dimension>
struct L_operator
{
    float dx[dimension];
    L_operator(const image::geometry<dimension>& geo)
    {
        std::copy(geo.begin(),geo.end(),dx);
    }
    void operator()(vtor_type& value,int* l)
    {
        float ak = (1-std::cos(4.0*3.1415926*((float)l[0])/dx[0]))*dx[0]*dx[0];
        ak += (1-std::cos(4.0*3.1415926*((float)l[1])/dx[1]))*dx[1]*dx[1];
        ak *= 0.02;
        ak += 1.0;
        ak *= ak;
        value /= ak;
    }
};

int main()
{
     // test fft
    {

        image::io::bitmap bitmap_loader;
        if(!bitmap_loader.load_from_file("fft_test_.bmp"))
        {
            std::cout << "fail to load bitmap file" << std::endl;
            return 0;
        }

        image::grayscale_image bitmap_image;
        bitmap_loader >> bitmap_image;

        image::basic_image<float,2> real(bitmap_image.geometry()),img(bitmap_image.geometry()),rreal,rimg;

        std::copy(bitmap_image.begin(),bitmap_image.end(),real.begin());

        image::fftn<2> fft(bitmap_image.geometry());
        image::realfftn<2> rfft(bitmap_image.geometry());

        img.resize(bitmap_image.geometry());
        //fft.apply_inverse(real,img);

        //image::fft(real,img,false);

        //image::apply_filter(real,img,L_operator<float,2>(real.geometry()));
        //image::apply_filter(real,img,myfilter());
        //image::fft(real,img,true);
        //image::square(img.begin(),img.end());
        rreal = real;
        fft.apply(real,img);
        rfft.apply(rreal,rimg);
        rfft.apply_inverse(rreal,rimg);

        //image::crop(real,image::geometry<2>(0,0),image::geometry<2>(real.width(),real.height()>>1));
        //image::crop(img,image::geometry<2>(0,0),image::geometry<2>(img.width(),img.height()>>1));
        real = rreal;
        // calculate dif
        if(0)
        {
            std::cout << real[0] << ":" << rreal[0] <<std::endl;
            std::cout << img[0] << ":" << rimg[0] <<std::endl;

            image::minus(real.begin()+1,real.end(),rreal.begin()+1);
            image::minus(img.begin()+1,img.end(),rimg.begin()+1);

            image::square(real.begin(),real.end());
            image::square(img.begin(),img.end());

            image::square_root(real.begin(),real.end());
            image::add_constant(real.begin(),real.end(),1.0);

            image::log(real.begin(),real.end());
        }

        //image::minus_constant(real.begin(),real.end(),*std::min_element(real.begin(),real.end()));

        image::normalize(real,255.0);

        bitmap_image.resize(real.geometry());
        std::copy(real.begin(),real.end(),bitmap_image.begin());
        bitmap_loader << bitmap_image;
        bitmap_loader.save_to_file("result_fft_test.bmp");

    }
     return 0;
}

Comments