Image Low-pass Filtering Algorithms Ideal/Butterworth/Gaussian (PyTorch Implementation) 图像低通滤波算法PyTorch实现

Contents

  1. 1. Convert to Frequency Domain 转换到频域
  2. 2. Ideal 理想低通滤波器
  3. 3. Gaussian 高斯滤波器
  4. 4. Butterworth 布特沃斯滤波器
  • Usage Example
  • Reference
  • Codes: https://github.com/CassiniHuy/image-low-pass-filters-pytorch

    The low-pass filters include ideal, Butterworth, Gaussian filters and implemented by PyTorch. Yes, it’s differentiable.

    The detailed description as follows:

    Convert to Frequency Domain 转换到频域

    Assume there is an image in spatial domain ,

    and convert it from spatial domain to frequency domain (shifted) ,

    Therefore the low-pass filtering is , where is the transfer function from as well.
    Finally the filtered image is obtained by inverse FFT (shifted).

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    def _to_freq(image: Tensor) -> Tensor:
    """Convert from spatial domain to frequency domain.
    Args:
    image (Tensor): [B, C, H, W].
    Returns:
    Tensor: [B, C, H, W]
    """
    img_fft = torch.fft.fft2(image)
    img_fft_shift = torch.fft.fftshift(img_fft)
    return img_fft_shift

    def _to_space(image_fft: Tensor) -> Tensor:
    """Convert from frequency domain to spatial domain.
    Args:
    image_fft (Tensor): [B, C, H, W].
    Returns:
    Tensor: [B, C, H, W].
    """
    img_ifft_shift = torch.fft.ifftshift(image_fft)
    img_ifft = torch.fft.ifft2(img_ifft_shift)
    img = torch.abs(img_ifft).clamp(0, 1)
    return img

    NOTE: The fft2 result has to be shifted to ensure the lower frequency components are placed around the center of the matrix. Otherwise, they exist at four corners, which is tricky to handle.

    Ideal 理想低通滤波器

    where is the distance to the matrix center for each pixel, and is the cutoff frequency.
    With a greater , more high frequencies are retained and therefore less information lost.

    Ideal Low-pass Filter

    Gaussian 高斯滤波器

    Gaussian Low-pass Filter

    Butterworth 布特沃斯滤波器

    where is the order of the Butterworth filter.

    With a higher , the filter is sharper, which approachs more to the ideal filter; otherwise it approaches to the gaussian filter.

    Butterworth Low-pass Filter

    Usage Example

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    import torch
    from preprocess import ideal_bandpass, butterworth, gaussian

    cutoff = 20 # D0

    img_tensor = torch.randn((1, 3, 224, 224)) # change it to your image tensor

    img_lowpass = ideal_bandpass(img_tensor, cutoff) # lowpass=False for high-pass.

    img_lowpass = butterworth(img_tensor, cutoff, 10)

    img_lowpass = gaussian(img_tensor, cutoff)

    Reference

    Gonzalez R C. Digital image processing[M]. Pearson education india, 2009.