1

How does one do the Discrete Fourier Transformation of an image using haskell. I believe the two libraries repa-devil and repa-fftw could be helpful, but I do not know how to integrate them. The reason why I would like this is so I could experiment with hybrid images (I have the wonderful idea that instead of filtering the images separately and then averaging them, I could simply do a weighted average based on frequency.)

Note: Also once one has done this, how would one reinterpret it as an image.

Note: I am not actually sure how one exactly applies dft the image processing. I have seen the equations and know how one would evaluate them, and know the "meaning" for 1D and a rough idea for 2D. I also know how one can some how use it to filter out high or low "frequencies" of an image somehow. I just am not sure exactly how complex frequency images are generated using dft.

PyRulez
  • 10,513
  • 10
  • 42
  • 87
  • 3
    What have you tried so far? Why didn't it work? At what point could you not combine your Repa `Array`s from repa-devil with repa-fftw's `fft2d`? – Cirdec Oct 11 '14 at 00:52
  • 1
    If you have a greyscale image (`Array F DIM2 Word8`), convert the Word8 to Complex and use `fft2d` directly - this much should be obvious. If you have a colour image, the usual procedure is to perform the FFT on each colour channel individually. You need to convert `Array F DIM3 Word8` to `Array F DIM2 Word8`, to do this, take a look at `Data.Array.Repa.Slice`, namely the `slice` function. – user2407038 Oct 11 '14 at 03:35
  • @user2407038 So they are just interpreted as complex numbers with imaginary part 0? Like `(:+ 0) . fromIntegral :: Word8 -> Complex Float`? – PyRulez Oct 11 '14 at 13:13

1 Answers1

1

You can transform your 2D image (matrix) also by 1D transforms

  1. first convert your image to matrix

    • FFT need complex domain
    • so for BW image set imaginary part as 0 and real part to pixel intensity
    • if you have color image then proces each color band as separate image
  2. apply 2D transform (DFT,DCT,...)

    • here is an example 2D DCT by 1D DFT transform in C++
    • at the bottom of that answer are the details for this
    • you just convert all rows of matrix
    • and then all columns
    • +/- some normalization constant scaling
  3. now do your stuff on frequency domain

  4. convert back to time domain

    • almost the same as bullet 2.
    • just use inverse transforms and inverse normalization scaling constant
  5. convert matrix back to image

[Notes]

  • FFT has complex output
  • so you need extract color from it for example as intensity = sqrt (re*re+im*im);
  • I use DCT for spectral analysis it is more intuitive
Community
  • 1
  • 1
Spektre
  • 49,595
  • 11
  • 110
  • 380