Skip to content

Instantly share code, notes, and snippets.

@JustinSDK
Last active May 24, 2021 04:26
Show Gist options
  • Save JustinSDK/8e2e8e1e27549748f8847041d402f786 to your computer and use it in GitHub Desktop.
Save JustinSDK/8e2e8e1e27549748f8847041d402f786 to your computer and use it in GitHub Desktop.
OpenCV傅立葉轉換與逆轉換
import cv2
import numpy as np
# 圖片來源:https://openhome.cc/Gossip/DCHardWay/images/Fourier4-1.JPG
img = cv2.imread('Fourier4-1.JPG', cv2.IMREAD_GRAYSCALE)
f = cv2.dft(np.float32(img))
shifted = np.fft.fftshift(f) # 將頻率 (0, 0) 位移至中心
freq = cv2.normalize( # 標準化
cv2.magnitude(shifted[:,:,0], shifted[:,:,1]), # 取振幅絕對值
None,
0, 255,
cv2.NORM_MINMAX)
cv2.imshow('FFT 2D', freq) # 頻域表示
inversed = cv2.dft(f, flags = cv2.DFT_INVERSE) # 逆轉換,也可以使用 cv2.idft(f)
inversed_img = cv2.normalize(
cv2.magnitude(inversed[:,:,0], inversed[:,:,1]),
None,
0, 255,
cv2.NORM_MINMAX
).astype('uint8')
cv2.imshow('INVERSE FFT 2D', inversed_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment