Skip to content

Instantly share code, notes, and snippets.

@shrimo
Last active March 26, 2023 22:55
Show Gist options
  • Save shrimo/65bb9bd3233b75ab694518212c823878 to your computer and use it in GitHub Desktop.
Save shrimo/65bb9bd3233b75ab694518212c823878 to your computer and use it in GitHub Desktop.
FourierTransform
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('image_big.png')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
rows, colss = img.shape
crow, ccol = rows//2 , colss//2
print(crow, ccol)
img_tmp = cv2.resize(img, (int(colss*0.2), int(rows*0.2)))
cv2.imshow("Image", img_tmp)
cv2.waitKey(-1)
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
MT = 20*np.log(np.abs(fshift))
MT *= 0.004
MT_tmp = cv2.resize(MT, (int(colss*0.2), int(rows*0.2)))
cv2.imshow("FFT1", MT_tmp)
cv2.waitKey(-1)
# fshift[crow-30:crow+31, ccol-30:ccol+31] = 0
# fshift[crow-10:crow+10, ccol-10:ccol+10] = 0
fshift[0:rows-450, 0:colss] = 0
fshift[0:rows, 0:colss-720] = 0
fshift[rows-500:rows+300, colss-540:colss+300] = 0
fshift[rows-240:rows+100, colss-1000:colss+200] = 0
MT = 20*np.log(np.abs(fshift))
MT=MT*0.004
MT = cv2.resize(MT, (int(colss*0.2), int(rows*0.2)))
cv2.imshow("FFT2", MT)
cv2.waitKey(-1)
f_ishift = np.fft.ifftshift(fshift)
img_back = np.fft.ifft2(f_ishift)
img_back = np.real(img_back)
img_back = img_back * 0.004
img_back = cv2.resize(img_back, (int(colss*0.2), int(rows*0.2)))
cv2.imshow("unpacked FFT3", img_back)
cv2.waitKey(-1)
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment