【python】頻域濾波

頻域濾波主要分爲四個步驟
1)計算源圖像的傅里葉變換結果
2)選擇並計算濾波器
3)將1得到的結果和2的結果相乘
4)對3的結果進行逆傅里葉變換

本文提供所有資源下載(自帶圖片):下載地址

結果展示:
低通濾波器
高通濾波器
高斯高通濾波器
代碼1:計算濾波器

import numpy as np

def high_pass_filter(img, radius=80):
    r = radius

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

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

def low_pass_filter(img, radius=100):
    r = radius

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

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

def bandreject_filters(img, r_out=300, r_in=35):
    rows, cols = img.shape
    crow, ccol = int(rows / 2), int(cols / 2)

    radius_out = r_out
    radius_in = r_in

    mask = np.zeros((rows, cols, 2), np.uint8)
    center = [crow, ccol]
    x, y = np.ogrid[:rows, :cols]
    mask_area = np.logical_and(((x - center[0]) ** 2 + (y - center[1]) ** 2 >= r_in ** 2),
                               ((x - center[0]) ** 2 + (y - center[1]) ** 2 <= r_out ** 2))
    mask[mask_area] = 1
    mask = 1 - mask
    return mask

def guais_low_pass(img, radius=10):
    rows, cols = img.shape
    center = int(rows / 2), int(cols / 2)

    mask = np.zeros((rows, cols, 2), np.float32)
    x, y = np.ogrid[:rows, :cols]
    for i in range(rows):
        for j in range(cols):
            distance_u_v = (i - center[0]) ** 2 + (j - center[1]) ** 2
            mask[i, j] = np.exp(-0.5 *  distance_u_v / (radius ** 2))
    return mask


def guais_high_pass(img, radius=10):
    rows, cols = img.shape
    center = int(rows / 2), int(cols / 2)

    mask = np.zeros((rows, cols, 2), np.float32)
    x, y = np.ogrid[:rows, :cols]
    for i in range(rows):
        for j in range(cols):
            distance_u_v = (i - center[0]) ** 2 + (j - center[1]) ** 2
            mask[i, j] = 1 - np.exp(-0.5 *  distance_u_v / (radius ** 2))
    return mask


def laplacian_filter(img, radius=10):
    rows, cols = img.shape
    center = int(rows / 2), int(cols / 2)

    mask = np.zeros((rows, cols, 2), np.float32)
    x, y = np.ogrid[:rows, :cols]
    for i in range(rows):
        for j in range(cols):
            distance_u_v = (i - center[0]) ** 2 + (j - center[1]) ** 2
            mask[i, j] = -4 * np.pi ** 2 * distance_u_v
    return mask

代碼2:將傅里葉變換結果或頻域結果以圖像形式顯示出來

import numpy as np
import cv2


def log_magnitude(img):
    magnitude_spectrum = 20 * np.log(cv2.magnitude(img[:, :, 0], img[:, :, 1]))
    return magnitude_spectrum

代碼3:主函數,計算濾波結果並展示

import numpy as np
import matplotlib.pyplot as plt
from scipy import fftpack
import cv2
import function_filters
import display

img = cv2.imread('5.tif', 0)


################################
#   first step: compute the FFT of original images
################################
img_dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)
img_dft_shift = np.fft.fftshift(img_dft)

# do log function to display some details
original_spectrum = display.log_magnitude(img_dft_shift)


################################
#   second step: compute the mask
################################
mask = function_filters.bandreject_filters(img, r_out=90, r_in=40)
# mask = function_filters.high_pass_filter(img, radius=100)
# mask = function_filters.low_pass_filter(img, radius=100)
# mask = function_filters.guais_low_pass(img, radius=30)
# mask = function_filters.guais_high_pass(img, radius=50)
# mask = function_filters.laplacian_filter(img, radius=10)

################################
#   third step: fft of original images multiply the filter
################################
fshift = img_dft_shift * mask

# do log to minize the region
spectrum_after_filtering = display.log_magnitude(fshift)


################################
#   Fourth step: IFFT
################################
f_ishift = np.fft.ifftshift(fshift)
img_after_filtering = cv2.idft(f_ishift)
img_after_filtering = display.log_magnitude(img_after_filtering)


################################
#   figure
################################
plt.subplot(2, 2, 1), plt.imshow(img, cmap='gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])

plt.subplot(2, 2, 2), plt.imshow(original_spectrum, cmap='gray')
plt.title('Spectrum of original image'), plt.xticks([]), plt.yticks([])

plt.subplot(2, 2, 3), plt.imshow(spectrum_after_filtering, cmap='gray')
plt.title('Spectrum after filtering'), plt.xticks([]), plt.yticks([])

plt.subplot(2, 2, 4), plt.imshow(img_after_filtering, cmap='gray')
plt.title('After filter or IFFT'), plt.xticks([]), plt.yticks([])

plt.show()


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