如何从 4D 图像中提取 FFT 的第一个分量

How to extract first component of FFT from 4D Image

This link to the image which I'm dealing with

import nibabel as nib
import numpy as np
import matplotlib.pyplot as plt

img = nib.load('patient057_4d.nii.gz')
f = np.fft.fft2(img)
#  Move the DC component of the FFT output to the center of the spectrum
fshift = np.fft.fftshift(f)
fshift_orig = fshift.copy()
# logarithmic transformation
 magnitude_spectrum = 20*np.log(np.abs(fshift))
# Create mask
rows, cols = img.shape
crow, ccol = int(rows/2), int(cols/2)
# Use mask to remove low frequency components
dist1 = 20
dist2 = 10
fshift[crow-dist1:crow+dist1, ccol-dist1:ccol+dist1] = 0
#fshift[crow-dist2:crow+dist2, ccol-dist2:ccol+dist2] = fshift_orig[crow-dist2:crow+dist2, ccol-dist2:ccol+dist2] 

# logarithmic transformation
magnitude_spectrum1 = 20*np.log(np.abs(fshift)) 
f_ishift = np.fft.ifftshift(fshift)
# inverse Fourier transform
img_back = np.fft.ifft2(f_ishift)
# get rid of imaginary part by abs
img_back = np.abs(img_back)
plt.figure(num = 'Im_Back')
plt.imshow(abs(fshift[:,:,2,2]).astype('uint8'),cmap='gray')
plt.show()

  • 解决方案是分别对每个切片进行傅里叶变换 3D,然后仅选择变换的第二个分量将其变换回空间 space,仅此而已。
  • 这样做的好处是检测是否有东西沿着第三轴(在我的例子中是时间)移动。
for sl in range(img.shape[2]):
   #-----Fourier--H1-----------------------------------------
   # ff1[:, :, 1] H1 compnent 1, if 0 then DC
   ff1 = FFT.fftn(img[:,:,sl,:])
   fh = np.absolute(FFT.ifftn(ff1[:, :, 1])) 

   #-----Fourier--H1-----------------------------------------