1

I am trying to implement, in Python, some functions that transform images to their Fourier domain and vice-versa, for image processing tasks.

I implemented the 2D-DFT using repeated 1D-DFT, and it worked fine, but when I tried to implement 2D inverse DFT using repeated inverse 1D-DFT, some weird problem occurred: when I transform an image to its Fourier domain and then back to the image domain, it looks like the image was reflected and merged with its reflection, as can be seen here:

This is the input:

img

and this is the output

img

This is the function that is responsible for the mess:

def IDFT2(fourier_image):
    image = np.zeros(fourier_image.shape)
    for col in range(image.shape[1]):
        image[:, col] = IDFT1(fourier_image[:, col])

    for row in range(image.shape[0]):
        image[row, :] = IDFT1(image[row,:])

    return image

What did I do wrong? I am pretty sure that IDFT1 works fine, and so is the regular 2D-DFT.

nbro
  • 15,395
  • 32
  • 113
  • 196
monte carlo
  • 95
  • 2
  • 8
  • Why do you have IDFT1(fourier_image[:, col]) for columns and IDFT1(image[row,:]) for rows and not IDFT1(fourier_image[row, :])? Please provide your code for the IDFT1 as well. – DimKoim Nov 25 '16 at 23:45
  • As I understood it, at the second pass (the rows) I need to operate over the output of the previous stage (the pass over the colums). Am I wrong? – monte carlo Nov 26 '16 at 00:06
  • @montecarlo you have edit out the images (I add them back) most likely by mistake. That `![bla bla][1]` in mark-down code means first link to image from list of links at the end. the `!` means image without it is just link – Spektre Dec 05 '16 at 08:52

1 Answers1

3

I do not use Python so I am not confident to analyze your code but my bet is that you most likely forget to implement complex values at some stage....

it should be:

  1. DFT rows from real to complex domain
  2. DFT columns of result from complex to complex domain
  3. apply normalization if needed
  4. any or none processing ...
  5. iDFT rows from complex to complex domain
  6. iDFT columns of result from complex to real domain
  7. apply normalization if needed

if you use just real to complex domain DFT/iDFT in the second passes (bullets #2,#6) then it would create the mirroring because DFT of real values is a mirrored sequence ... Btw. it does not matter if you process rows or columns first ... also you can process rows first in DFT and columns first in iDFT the result should be the same +/- floating errors ...

for more info see

and all the sub-links there especially the 2D FFT and wrapping example so you can compare your results with something working

Community
  • 1
  • 1
Spektre
  • 49,595
  • 11
  • 110
  • 380