Two-dimensional FFT (FFT2)

Image FFT
Reading time: about 7 minutes

This page is about the 2D FFT, also called FFT2 in certain libraries and programming environments.

In [2]:
img = Image.open("/home/leo/kekw.jpg").convert('L')
#img = img.resize((int(img.width / 4), int(img.height / 4)))
In [3]:
npimg = np.zeros((img.height, img.width))

for y in range(img.height):
    for x in range(img.width):
        npimg[y][x] = img.getpixel((x, y))

npimg = npimg / 256
img = npimg
plt.imshow(img, cmap="gray")
plt.colorbar()
Out [3]:
Out [3]:
Out:
<Figure size 900x600 with 2 Axes>
In [4]:
def fft_rows(img):
    fft_img = np.zeros(img.shape, dtype=np.complex_)
    
    for y in range(img.shape[0]):
        row = [img[y][x] for x in range(img.shape[1])]
        
        row_fft = np.fft.fft(row)
        
        for x, col in enumerate(row_fft):
            fft_img[y][x] = col
        
    return fft_img

img = fft_rows(img)

plt.imshow(np.abs(img), vmax=1)
plt.colorbar()
Out [4]:
Out [4]:
Out:
<Figure size 900x600 with 2 Axes>
In [5]:
def fft_cols(img):
    fft_img = np.zeros(img.shape, dtype=np.complex_)
    
    for x in range(img.shape[1]):
        row = [img[y][x] for y in range(img.shape[0])]
        
        row_fft = np.fft.fft(row)
        
        for y, col in enumerate(row_fft):
            fft_img[y][x] = col
        
    return fft_img

img = fft_cols(img)

plt.imshow(np.abs(img), vmax=2)
plt.colorbar()
Out [5]:
Out [5]:
Out:
<Figure size 900x600 with 2 Axes>
In [6]:
plt.imshow(np.fft.ifft2(img).real, cmap="gray")
plt.colorbar()
Out [6]:
Out [6]:
Out:
<Figure size 900x600 with 2 Axes>
In [7]:
shp = img.shape
img = img.flatten()

decim = 50

for i, x in enumerate(img):
    img[i] = int(x.real / decim) * decim + int(x.imag / decim) * decim * 1j

img = img.reshape(shp)

plt.imshow(np.fft.ifft2(img).real, cmap="gray")
plt.colorbar()
Out [7]:
Out [7]:
Out:
<Figure size 900x600 with 2 Axes>
In [8]:
plt.imshow(np.abs(img), vmax=5)
plt.colorbar()
Out [8]:
Out [8]:
Out:
<Figure size 900x600 with 2 Axes>
In [9]:
np.max(img.real)/decim, np.min(img.real)/decim
np.max(img.imag)/decim, np.min(img.imag)/decim

#from collections import Counter
#Counter(img.real.flatten()/decim)
#Counter(img.imag.flatten()/decim)
Out [9]:
(4590.0, -1974.0)
Out [9]:
(645.0, -645.0)

The following pages link here

Citation

If you find this work useful, please cite it as:
@article{yaltirakliwikifft2twodimensionalfft,
  title   = "Two-dimensional FFT (FFT2)",
  author  = "Yaltirakli, Gokberk",
  journal = "gkbrk.com",
  year    = "2024",
  url     = "https://www.gkbrk.com/wiki/fft2-two-dimensional-fft/"
}
Not using BibTeX? Click here for more citation styles.
IEEE Citation
Gokberk Yaltirakli, "Two-dimensional FFT (FFT2)", December, 2024. [Online]. Available: https://www.gkbrk.com/wiki/fft2-two-dimensional-fft/. [Accessed Dec. 30, 2024].
APA Style
Yaltirakli, G. (2024, December 30). Two-dimensional FFT (FFT2). https://www.gkbrk.com/wiki/fft2-two-dimensional-fft/
Bluebook Style
Gokberk Yaltirakli, Two-dimensional FFT (FFT2), GKBRK.COM (Dec. 30, 2024), https://www.gkbrk.com/wiki/fft2-two-dimensional-fft/

Comments

© 2024 Gokberk Yaltirakli