1. 程式人生 > >快速傅立葉反變換恢復原影象(python2.7)

快速傅立葉反變換恢復原影象(python2.7)

快速傅立葉反變換恢復原影象

定義

If it varies slowly, it is a low frequency signal. You can extend the same idea to images. Where does the amplitude varies drastically in images ? At the edge points, or noises. So we can say, edges and noises are high frequency contents in an image. If there is no much changes in amplitude, it is a low frequency component.

程式碼塊

import sys
reload(sys)
sys.setdefaultencoding('utf8')
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt

img = cv.imread('noise.jpg', 0)
dft = cv.dft(np.float32(img),flags = cv.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)

rows, cols = img.shape
crow,ccol = rows/2
, cols/2 # create a mask first, center square is 1, remaining all zeros mask = np.zeros((rows,cols,2),np.uint8) mask[crow-30:crow+30, ccol-30:ccol+30] = 1 # apply mask and inverse DFT fshift = dft_shift*mask f_ishift = np.fft.ifftshift(fshift) img_back = cv.idft(f_ishift) img_back = cv.magnitude(img_back[:,:,0
],img_back[:,:,1]) plt.subplot(121),plt.imshow(img, cmap = 'gray') plt.title('Input Image'), plt.xticks([]), plt.yticks([]) plt.subplot(122),plt.imshow(img_back, cmap = 'gray') plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([]) plt.show()

實驗結果