1
votes

I've deconstructed a picture of a dog into its magnitude and phase components. However when I put them back together and try to do the inverse fourier transform I do not get the original image?

img1 = img1[:,:,2] 

plt.imshow(img1, cmap='gray')

plt.show()

dft = cv2.dft(np.float32(img1), flags = cv2.DFT_COMPLEX_OUTPUT)

print(dft)

dft_shift = np.fft.fftshift(dft)

print(dft_shift)

mag, ang = cv2.cartToPolar(dft_shift[:,:,0],dft_shift[:,:,1])

plt.imshow(20*np.log(mag), cmap='gray')

plt.show()

plt.imshow(ang, cmap='gray')

plt.show()

combined = np.multiply(mag, np.exp(1j*ang))

imgCombined = np.real(np.fft.ifft2(np.fft.ifftshift(combined)))

plt.imshow(imgCombined, cmap='gray')

plt.show()

fixed!

def compute_mag_phase(toBeTransfromed):
    dft = np.fft.fft2(toBeTransfromed)
    dft_shift = np.fft.fftshift(dft)
    mag = np.abs(dft_shift)
    ang = np.angle(dft_shift)
    return mag, ang

def reconstruct(mag,ang):
    combined = np.multiply(mag, np.exp(1j*ang))
    fftx = np.fft.ifftshift(combined)
    ffty = np.fft.ifft2(fftx)
    imgCombined = np.abs(ffty)
    return imgCombined

Magnitude:

magnitude

Angle:

angle

Reconstructed:

reconstructed

1
I do not see that you did your dft_scale as mentioned in the docs link from Birol Kuyumcu. It would also be helpful if you posted your input dog image so others can test your code and show your code for reading your image. See this forums help section about providing minimally reproducible and verifiable code.fmw42

1 Answers

0
votes

fftshift inverted your third dimension of dft_shift. The following seems to work

dft_real = np.fft.fftshift(dft[:, :, 0])
dft_imag = np.fft.fftshift(dft[:, :, 1])
mag, ang = cv2.cartToPolar(dft_real, dft_imag)