2021-05-30 傅里叶python

# -*- coding: utf-8 -*-
"""
Created on Sun May 30 00:08:40 2021

@author: Quan Wang(Sunny)
"""

# -*- coding: utf-8 -*-


import cv2 
import matplotlib.pyplot as plt
import numpy as np 

img = cv2.imread('E:/My photo/2222222k.jpg',0)
dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)

dft_shift = np.fft.fftshift(dft)   # shift to center 

magnitude_spectrum = 20*np.log(cv2.magnitude(dft_shift[:,:,0], dft_shift[:,:,1]))

# high pass filter mask 

rows, cols= img.shape
crow, ccol = int(rows/2),int(cols/2)

mask =np.ones((rows, cols,2 ), np.uint8)
r =80
center = [crow,ccol]
x,y =np.ogrid[:rows, :cols]
mask_area = (x-center[0])**2 + (y-center[1])**2 <=r*r
mask[mask_area]=0

fshift = dft_shift*mask
fshift_mask_mag = 2000*np.log(cv2.magnitude(fshift[:,:,0], fshift[:,:,1]))

f_ishift = np.fft.ifftshift(fshift)
img_back = cv2.idft(fshift)
img_back =cv2.magnitude(img_back[:,:,0],img_back[:,:,1])

fig = plt.figure(figsize=(12,12))
ax1= fig.add_subplot(2,2,1)
ax1.imshow(img,cmap='hot')
ax1.title.set_text('Input Iamge')

ax2= fig.add_subplot(2,2,2)
ax2.imshow(magnitude_spectrum,cmap='gray')
ax2.title.set_text('FFT of Iamge')
ax3= fig.add_subplot(2,2,3)
ax3.imshow(fshift_mask_mag,cmap='hot')
ax3.title.set_text('FFT + mask')
ax4= fig.add_subplot(2,2,4)
ax4.imshow(img_back,cmap='hot')
ax4.title.set_text('After inverse FFT')
plt.show()
plt.colorbar()
image.png

你可能感兴趣的:(2021-05-30 傅里叶python)