# How to remove repititve pattern from an image using FFT

Here is a simple and effective linear filtering strategy to remove the horizontal line artifact:

Outline:

1. Estimate the frequency of the distortion by looking for a peak in the image’s power spectrum in the vertical dimension. The function scipy.signal.welch is useful for this.

2. Design two filters: a highpass filter with cutoff just below the distortion frequency and a lowpass filter with cutoff near DC. We’ll apply the highpass filter vertically and the lowpass filter horizontally to try to isolate the distortion. We’ll use scipy.signal.firwin to design these filters, though there are many ways this could be done.

3. Compute the restored image as “image − (hpf ⊗ lpf) ∗ image”.

Code:

# Copyright 2021 Google LLC.

import numpy as np
from scipy.ndimage import convolve1d
from scipy.signal import firwin, welch

def remove_lines(image, distortion_freq=None, num_taps=65, eps=0.025):
"""Removes horizontal line artifacts from scanned image.
Args:
image: 2D or 3D array.
distortion_freq: Float, distortion frequency in cycles/pixel, or
None to estimate from spectrum.
num_taps: Integer, number of filter taps to use in each dimension.
eps: Small positive param to adjust filters cutoffs (cycles/pixel).
Returns:
Denoised image.
"""
image = np.asarray(image, float)
if distortion_freq is None:
distortion_freq = estimate_distortion_freq(image)

hpf = firwin(num_taps, distortion_freq - eps,
pass_zero='highpass', fs=1)
lpf = firwin(num_taps, eps, pass_zero='lowpass', fs=1)
return image - convolve1d(convolve1d(image, hpf, axis=0), lpf, axis=1)

def estimate_distortion_freq(image, min_frequency=1/25):
"""Estimates distortion frequency as spectral peak in vertical dim."""
f, pxx = welch(np.reshape(image, (len(image), -1), 'C').sum(axis=1))
pxx[f < min_frequency] = 0.0
return f[pxx.argmax()]


Examples:

On the portrait image, estimate_distortion_freq estimates that the frequency of the distortion is 0.1094 cycles/pixel (period of 9.14 pixels). The transfer function of the filtering “image − (hpf ⊗ lpf) ∗ image” looks like this:

Here is the filtered output from remove_lines:

On the skin image, estimate_distortion_freq estimates that the frequency of the distortion is 0.08333 cycles/pixel (period of 12.0 pixels). Filtered output from remove_lines:

The distortion is mostly removed on both examples. It isn’t perfect: on the portrait image, a couple ripples are still visible near the top and bottom borders, a typical defect when using large filters or Fourier methods. Still, it’s a good improvement over the original images.