我正在嘗試使用 Pytorch 在頻域中對 RGB 影像進行上采樣。我正在使用
放大后的影像:

另一個需要注意的有趣的事情是執行IFFT后影像像素的最大值和最小值:它們分別是2.2729和-1.8376。理想情況下,它們應該是 1.0 和 0.0。
有人可以解釋這里有什么問題嗎?
uj5u.com熱心網友回復:
DFT 的通常約定是將第一個樣本視為 0Hz 分量。但是您需要在中心放置 0Hz 分量才能使填充有意義。大多數 FFT 工具都提供了一個移位函式來回圈移位您的結果,使 0Hz 分量位于中心。在 pytorch 中,您需要torch.fft.fftshift在 FFT 之后和torch.fft.ifftshift進行逆 FFT 之前執行,以將 0Hz 分量放回左上角。
import torch
import torch.nn.functional as F
import cv2
import numpy as np
img = src = cv2.imread('orig.png')
torch_img = torch.from_numpy(img).to(torch.float32).permute(2, 0, 1) / 255.
# note the fftshift
fft = torch.fft.fftshift(torch.fft.fft2(torch_img, norm="forward"))
fr = fft.real
fi = fft.imag
fr = F.pad(fr, (fft.shape[-1]//2, fft.shape[-1]//2, fft.shape[-2]//2, fft.shape[-2]//2), mode='constant', value=0)
fi = F.pad(fi, (fft.shape[-1]//2, fft.shape[-1]//2, fft.shape[-2]//2, fft.shape[-2]//2), mode='constant', value=0)
# note the ifftshift
fft_hires = torch.fft.ifftshift(torch.complex(fr, fi))
inv = torch.fft.ifft2(fft_hires, norm="forward").real
print(inv.max(), inv.min())
img = (inv.permute(1, 2, 0).detach()).clamp(0, 1)
img = (255 * img).numpy().astype(np.uint8)
cv2.imwrite('hires.png', img)
產生以下hires.png

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/425466.html
