數字圖像處理:頻域濾波

要求

對一副圖像進行傅立葉變換,顯示頻譜,取其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:
在這裏插入圖片描述

參考

圖像頻域濾波與傅里葉變換
理想濾波 巴特沃茲濾波 高斯濾波

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章