사용할 이미지
Load image
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('dog.jpg', cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (256, 256))
print(img.shape)
plt.imshow(img, cmap='gray')
plt.colorbar()
(256, 256)
FFT and IFFT using numpy
import numpy as np
fft_img = np.fft.fftshift(np.fft.fft2(img, norm='ortho'))
print(fft_img.shape, fft_img.dtype)
plt.imshow(np.abs(fft_img), cmap='gray')
plt.colorbar()
(256, 256) complex128
ifft_img = np.fft.ifft2(np.fft.ifftshift(fft_img), norm='ortho')
print(ifft_img.shape, ifft_img.dtype)
plt.imshow(np.abs(ifft_img), cmap='gray')
plt.colorbar()
(256, 256) complex 128
FFT and IFFT using PyTorch
import torch
tensor = torch.from_numpy(img)
fft_tensor = torch.fft.fftshift(torch.fft.fft2(tensor, norm='ortho'))
print(fft_tensor.shape, fft_tensor.dtype)
plt.imshow(torch.abs(fft_tensor), cmap='gray')
plt.colorbar()
torch.size([256, 256]) torch.complex64
ifft_tensor = torch.fft.ifft2(torch.fft.ifftshift(fft_tensor), norm='ortho')
print(ifft_tensor.shape, ifft_tensor.dtype)
plt.imshow(torch.abs(ifft_tensor), cmap='gray')
plt.colorbar()
torch.size([256, 256]) torch.complex64
반응형
'🐍 Python & library > PyTorch' 카테고리의 다른 글
[PyTorch] tensor.detach()의 기능과 예시 코드 (0) | 2022.10.30 |
---|---|
[PyTorch] nn.Embedding 초기화하기 (initialization) (0) | 2022.10.27 |
[PyTorch] make_grid로 여러 개의 이미지 한번에 plot하기 (0) | 2022.07.29 |
[PyTorch] model weight 값 조정하기 / weight normalization (0) | 2022.04.22 |
[PyTorch] nn.Conv의 padding과 padding_mode (2) | 2022.03.24 |