天天看點

數字圖像處理:頻域濾波

要求

對一副圖像進行傅立葉變換,顯示頻譜,取其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:

數字圖像處理:頻域濾波

參考

圖像頻域濾波與傅裡葉變換

理想濾波 巴特沃茲濾波 高斯濾波