Khalil.h
Khalil.h

Reputation: 55

How to draw on a Fourier transform numpy array Opencv

I want to draw black rectangles on an FFT magnitude image and apply the IFFT. I tried calculating the FFT I get 2 arrays one for the magnitude and the other for the phase after editing the magnitude image (I converted it to uint8) I don't know how to convert to the right input format for the IFFT. This the FFT code I'm using:

dft = np.fft.fft2(img)
dft_shift = np.fft.fftshift(dft)
mag = np.abs(dft_shift)
ang = np.angle(dft_shift)

That's the code I use to reconstruct the image:

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

Upvotes: 1

Views: 1189

Answers (1)

user8190410
user8190410

Reputation: 1314

  • Before computing the Fourier transform, convert the image to floating point data type.
  • Create a mask and multiply it with the magnitude.
  • combine the magnitude and phase, and apply inverse transform

Code:

import cv2
import numpy as np
import matplotlib.pyplot as plt


def fourier(img):
    dft = np.fft.fft2(np.float32(img))
    dft_shift = np.fft.fftshift(dft)
    mag = np.abs(dft_shift)
    ang = np.angle(dft_shift)
    return mag, ang

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

img = cv2.imread('./cameraman.tif', 0)
mag, ang = fourier(img)
mask = np.zeros(img.shape, dtype=img.dtype)
y,x = mask.shape[0], mask.shape[1]
cx = x//2
cy = y//2

mask[cy-50 : cy+50, cx-50 : cx+50] = 1.
mag = mag * mask
res = inverse_fourier(mag, ang)

mx = np.amax(np.log(mag+0.001))
tmp = np.uint8(255*(mag/mx))

_, ax = plt.subplots(2,2)
ax[0,0].imshow(img, cmap='gray')
ax[0,1].imshow(tmp, cmap='gray')
ax[1,0].imshow(mask, cmap='gray')
ax[1,1].imshow(res, cmap='gray')
plt.show()

Resultenter image description here

Upvotes: 3

Related Questions