巴特沃斯滤波器#

巴特沃斯滤波器在频域中实现,旨在没有通带或阻带纹波。它可以用于低通或高通变体。cutoff_frequency_ratio 参数用于将截止频率设置为采样频率的分数。鉴于奈奎斯特频率是采样频率的一半,这意味着此参数应为小于 0.5 的正浮点值。可以调整滤波器的阶数以控制过渡宽度,值越高,通带和阻带之间的过渡越清晰。

巴特沃斯滤波示例#

在这里,我们定义了一个 get_filtered 辅助函数,以在指定的截止频率系列中重复低通和高通滤波。

import matplotlib.pyplot as plt

from skimage import data, filters

image = data.camera()

# cutoff frequencies as a fraction of the maximum frequency
cutoffs = [0.02, 0.08, 0.16]


def get_filtered(image, cutoffs, squared_butterworth=True, order=3.0, npad=0):
    """Lowpass and highpass butterworth filtering at all specified cutoffs.

    Parameters
    ----------
    image : ndarray
        The image to be filtered.
    cutoffs : sequence of int
        Both lowpass and highpass filtering will be performed for each cutoff
        frequency in `cutoffs`.
    squared_butterworth : bool, optional
        Whether the traditional Butterworth filter or its square is used.
    order : float, optional
        The order of the Butterworth filter

    Returns
    -------
    lowpass_filtered : list of ndarray
        List of images lowpass filtered at the frequencies in `cutoffs`.
    highpass_filtered : list of ndarray
        List of images highpass filtered at the frequencies in `cutoffs`.
    """

    lowpass_filtered = []
    highpass_filtered = []
    for cutoff in cutoffs:
        lowpass_filtered.append(
            filters.butterworth(
                image,
                cutoff_frequency_ratio=cutoff,
                order=order,
                high_pass=False,
                squared_butterworth=squared_butterworth,
                npad=npad,
            )
        )
        highpass_filtered.append(
            filters.butterworth(
                image,
                cutoff_frequency_ratio=cutoff,
                order=order,
                high_pass=True,
                squared_butterworth=squared_butterworth,
                npad=npad,
            )
        )
    return lowpass_filtered, highpass_filtered


def plot_filtered(lowpass_filtered, highpass_filtered, cutoffs):
    """Generate plots for paired lists of lowpass and highpass images."""
    fig, axes = plt.subplots(2, 1 + len(cutoffs), figsize=(12, 8))
    fontdict = dict(fontsize=14, fontweight='bold')

    axes[0, 0].imshow(image, cmap='gray')
    axes[0, 0].set_title('original', fontdict=fontdict)
    axes[1, 0].set_axis_off()

    for i, c in enumerate(cutoffs):
        axes[0, i + 1].imshow(lowpass_filtered[i], cmap='gray')
        axes[0, i + 1].set_title(f'lowpass, c={c}', fontdict=fontdict)
        axes[1, i + 1].imshow(highpass_filtered[i], cmap='gray')
        axes[1, i + 1].set_title(f'highpass, c={c}', fontdict=fontdict)

    for ax in axes.ravel():
        ax.set_xticks([])
        ax.set_yticks([])
    plt.tight_layout()
    return fig, axes


# Perform filtering with the (squared) Butterworth filter at a range of
# cutoffs.
lowpasses, highpasses = get_filtered(image, cutoffs, squared_butterworth=True)

fig, axes = plot_filtered(lowpasses, highpasses, cutoffs)
titledict = dict(fontsize=18, fontweight='bold')
fig.text(
    0.5,
    0.95,
    '(squared) Butterworth filtering (order=3.0, npad=0)',
    fontdict=titledict,
    horizontalalignment='center',
)
original, lowpass, c=0.02, lowpass, c=0.08, lowpass, c=0.16, highpass, c=0.02, highpass, c=0.08, highpass, c=0.16
Text(0.5, 0.95, '(squared) Butterworth filtering (order=3.0, npad=0)')

避免边界伪影#

在上面的图像中可以看到,图像边缘附近存在伪影(特别是对于较小的截止值)。这是由于 DFT 的周期性造成的,可以通过在滤波之前向边缘应用一定量的填充来减少伪影,从而使图像的周期性扩展中没有明显的边缘。这可以通过 butterworthnpad 参数来完成。

请注意,使用填充后,图像边缘处不需要的阴影会大大减少。

lowpasses, highpasses = get_filtered(image, cutoffs, squared_butterworth=True, npad=32)

fig, axes = plot_filtered(lowpasses, highpasses, cutoffs)
fig.text(
    0.5,
    0.95,
    '(squared) Butterworth filtering (order=3.0, npad=32)',
    fontdict=titledict,
    horizontalalignment='center',
)
original, lowpass, c=0.02, lowpass, c=0.08, lowpass, c=0.16, highpass, c=0.02, highpass, c=0.08, highpass, c=0.16
Text(0.5, 0.95, '(squared) Butterworth filtering (order=3.0, npad=32)')

真正的巴特沃斯滤波器#

要使用巴特沃斯滤波器的传统信号处理定义,请设置 squared_butterworth=False。此变体在频域中的幅度轮廓是默认情况的平方根。这会导致在任何给定的阶数下,从通带到阻带的过渡更加渐进。这可以在以下图像中看到,这些图像在低通情况下比上面的平方巴特沃斯图像看起来更清晰一些。

lowpasses, highpasses = get_filtered(image, cutoffs, squared_butterworth=False, npad=32)

fig, axes = plot_filtered(lowpasses, highpasses, cutoffs)
fig.text(
    0.5,
    0.95,
    'Butterworth filtering (order=3.0, npad=32)',
    fontdict=titledict,
    horizontalalignment='center',
)

plt.show()
original, lowpass, c=0.02, lowpass, c=0.08, lowpass, c=0.16, highpass, c=0.02, highpass, c=0.08, highpass, c=0.16

脚本的总运行时间:(0 分钟 3.383 秒)

由 Sphinx-Gallery 生成的图库