6.2 频率域滤波之高通滤波

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

上一章我们讲到频率低通滤波,简而言之就是让图像的低频信号通过,过滤或者衰减高频信号。频率高通滤波刚好相反,让图像的高频信号通过,过滤或者衰减低频信号。


1. 理论基础

由于频率域低通滤波域高通滤波的原理一样,知识滤波器不同,因此原理部分此处不做讲解,各位可以查看上一章的内容
在这里插入图片描述
从上图可以看出频率域高通滤波器的传递函数都是基于对应的低通滤波器传递函数推导出来的。各位可以对下面的两幅图片进行对比分析:

频率域低通滤波器
频率域高通滤波器

2. 示例分析

2.1 示例代码

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt


def genIdealHighpassFilters(D0, shape):
    """
    Generate an ideal high pass filter
    Those inside the circle are marked 1 and those outside are marker 0.
    :param D0: The radius of the circle.
    :param shape: The shape of the mask (the mask is also an image)
    :return:
    """
    mask = np.ones(shape, np.uint8)

    center = np.array([shape[0] // 2, shape[1] // 2])
    for i in range(shape[0]):
        for j in range(shape[1]):
            point = np.array([i, j])
            distance = np.linalg.norm(point - center)
            if distance <= D0:
                # print(distance, radius)
                mask[i][j] = 0

    return mask


def genGaussianHighpassFilters(sigma, shape):
    """
    Generate a gaussian high pass filter
    :param sigma: The standard deviation of the gaussian filter.
    :param shape: The shape of the mask (the mask is also an image)
    :return: Returns a gaussian high pass filter.
    """

    mask = np.ones(shape, np.float32)
    rows, cols = shape[0], shape[1]
    center_x, center_y = rows // 2, cols // 2
    for i in range(rows):
        for j in range(cols):
            distance = (i - center_x) ** 2 + (j - center_y) ** 2
            mask[i][j] = 1 - np.exp(-distance / (2 * sigma ** 2))
    return mask


def genButterworthHighpassFilters(n, D0, shape):
    """
    Generate a butterworth high pass filter
    :param n: The param of the transfer function
    :param D0: The cut-off frequency of the low pass filter
    :param shape: image shape
    :return: A butterworth high pass filter
    """
    mask = np.ones(shape, np.float32)
    rows, cols = shape[0], shape[1]
    center_x, center_y = rows // 2, cols // 2
    epsilon = 1e-5  # 添加一个小值以避免除以零
    for i in range(rows):
        for j in range(cols):
            distance = np.sqrt((i - center_x) ** 2 + (j - center_y) ** 2) + epsilon
            # mask[i, j] = 1 / (1 + (distance / D0) ** (2 * n))  This is low pass
            mask[i, j] = 1 / (1 + (D0 / distance) ** (2 * n))
    return mask


def imageSmoothing(image, mask):
    """
     Image smoothing in the frequency domain using low pass filters.
    :param image: raw image
    :param mask: the high pass filters.
    :return: smoothed image
    """

    # 对原图像进行傅里叶变换,生成频谱图
    dft = cv.dft(np.float32(image), flags=cv.DFT_COMPLEX_OUTPUT)

    # 将频谱图上的低频移至中心
    f_shift = np.fft.fftshift(dft)

    # 将滤波器与傅里叶变化后的图像相乘,保留四周部分,即保留低频部分
    f = f_shift * mask

    # 将低频从中心移回原处
    i_shift = np.fft.ifftshift(f)

    # 进行计算傅里叶逆变换,将频谱图还原回空间域图像
    dst = cv.idft(i_shift)

    # 计算还原图像的幅度,并进行归一化处理
    dst = cv.magnitude(dst[:, :, 0], dst[:, :, 1])

    return dst


if __name__ == '__main__':
    img = cv.imread('Image/Fig0601.tif', 0)

    # 生成低频滤波器
    D0_1 = 60
    D0_2 = 160

    mask1 = genIdealHighpassFilters(D0_1, (img.shape[0], img.shape[1], 2))
    img1 = imageSmoothing(img, mask1)

    mask2 = genGaussianHighpassFilters(D0_1, (img.shape[0], img.shape[1], 2))
    img2 = imageSmoothing(img, mask2)

    mask3 = genButterworthHighpassFilters(1, D0_1, (img.shape[0], img.shape[1], 2))
    img3 = imageSmoothing(img, mask3)

    mask4 = genIdealHighpassFilters(D0_2, (img.shape[0], img.shape[1], 2))
    img4 = imageSmoothing(img, mask4)

    mask5 = genGaussianHighpassFilters(D0_2, (img.shape[0], img.shape[1], 2))
    img5 = imageSmoothing(img, mask5)

    mask6 = genButterworthHighpassFilters(1, D0_2, (img.shape[0], img.shape[1], 2))
    img6 = imageSmoothing(img, mask6)

    # 图像显示
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.rcParams['font.size'] = 30
    plt.figure(figsize=(30, 20))
    imagesLst = [img1, img2, img3, img4, img5, img6]
    labelLst = ["Ideal D0 = 60", "Gaussian D0 = 60", "Butterworth D0 = 60"
        , "Ideal D0 = 160", "Gaussian D0 = 160", "Butterworth D0 = 160"]

    for i in range(6):
        plt.subplot(2, 3, i + 1), plt.title(labelLst[i]), plt.axis('off')
        plt.imshow(imagesLst[i], cmap='gray')
    plt.tight_layout()
    plt.savefig("Image/tmp.png")
    plt.show()

2.2 示例效果分析

在这里插入图片描述
分析上图可得:

  • 如上图中的(图1)所示:理想高通滤波器产生了严重的失真,这些失真是由振铃效应导致的,相比之下(图2)和(图3)都没有这样的失真。
  • D 0 = 60 D_0=60 D0=60 时(第一行),滤波器去除或衰减了约 95 % 95\% 95% 的图像能量,留下的 5 % 5\% 5% 大部分都是边缘信息和其它急剧过度。
  • D 0 = 160 D_0=160 D0=160时(第二行),图像剩余的能量仅为第一行的一般,约 2.5 % 2.5\% 2.5% 然而细节上的差别却非常惊人,例如字母 a a a 的边界非常干净,特别是 G H P F GHPF GHPF B H P F BHPF BHPF滤波。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值