要求
對一副圖像進行傅立葉變換,顯示頻譜,取其5,50,150爲截至頻率,進行頻率域平滑,銳化,顯示圖像
待處理圖像:
傅里葉變換和反變換
使用numpy包,進行二維傅里葉變換並將FFT的DC分量移到頻譜中心:
def fft(image):
f = np.fft.fft2(image)
# move to center
fshift = np.fft.fftshift(f)
return fshift
使用numpy包,先將DC分量移回,再進行二維傅里葉反變換,爲了圖像正常顯示,取了絕對值:
def ifft(fshift):
f1shift = np.fft.ifftshift(fshift)
image_back = np.fft.ifft2(f1shift)
image_back = np.abs(image_back)
return image_back
調用:
img = cv2.imread('4_29_a.jpg', 0)
plt_show_opcv("image", img)
fft_re = fft(img)
show_re = np.log(np.abs(fft_re))
plt_show_opcv("show_re", show_re)
image_back= ifft(fft_re)
plt_show_opcv("image_back", image_back)
結果:
頻率域濾波
平滑-理想低通濾波
得到理想低通濾波模板:
def get_mask(shape, r):
mask_ = np.zeros(shape, np.uint8)
cv2.circle(mask_, (int(shape[1] / 2), int(shape[0] / 2)), r, 255, -1)
return mask_
使用模板進行濾波:
img = cv2.imread('4_29_a.jpg', 0)
plt_show_opcv("image", img)
fshift_re = fft(img)
show_re = np.log(np.abs(fshift_re))
plt_show_opcv("show_re", show_re)
mask = get_mask(img.shape, 40)
plt_show_opcv("mask", mask)
re = fshift_re * mask
new_img = ifft(re)
plt_show_opcv("image_back", new_img)
半徑5:
半徑50:
可以大致看到輪廓:
半徑150:
和原圖差不多
銳化-巴特沃斯高通濾波
d爲頻率距原點的距離爲
def bhpf(image, d):
f = np.fft.fft2(image)
fshift = np.fft.fftshift(f)
transfor_matrix = np.zeros(image.shape)
M = transfor_matrix.shape[0]
N = transfor_matrix.shape[1]
for u in range(M):
for v in range(N):
D = np.sqrt((u - M / 2) ** 2 + (v - N / 2) ** 2)
filter_mat = 1 / (1 + np.power(d / D, 2))
transfor_matrix[u, v] = filter_mat
new_img = np.abs(np.fft.ifft2(np.fft.ifftshift(fshift * transfor_matrix)))
return new_img
d距離爲5:
d距離爲50:
d距離爲150: