Skip to content

Ideal high-pass filter

Fact Value
Also called Ideal high pass filter, Ideal HPF
Status Implemented and tested
Family Frequency-domain filters
Engine Browser
Type Linear, frequency, frequency response
Output Image
Operator shown as frequency-response (exact)
Support radius global
Code web/src/core/filters/frequency/passband.ts, idealHighpassFilter
Operator code web/src/core/filters/frequency/passband.ts, passResponse
Channels Brightness only, RGB

Overview

The ideal high-pass filter removes the low frequencies of the image and keeps the high ones. Flat areas become 0, and edges and fine detail remain. The ideal filter keeps or removes each frequency completely, with a sharp cutoff. The sharp cutoff makes ripples, called ringing, next to strong edges.

Three panels: a zone plate whose rings get finer away from the center, the response H as a gray image, and the high-pass result, which is flat gray in the center and keeps the outer rings. Three panels: a zone plate whose rings get finer away from the center, the response H as a gray image, and the high-pass result, which is flat gray in the center and keeps the outer rings.
The Ideal high-pass filter removes the rings near the center of the zone plate and keeps the outer ones.

How it works

  1. Extend the image with the border mode.
  2. Split it into waves of many frequencies with the 2D FFT.
  3. Multiply each frequency by H, which is 0 near the center of the spectrum and rises to 1 beyond the cutoff.
  4. Add the waves back together with the inverse FFT and crop the extension.

Mathematical foundation

\[ H = \begin{cases} 0 & D \le D_0 \\ 1 & D > D_0 \end{cases},\quad D = \sqrt{f_x^2 + f_y^2},\ D_0 = 0.1 \]

The formula shows the default parameter values.

where

  • \(D\): The distance from the center of the spectrum in cycles per pixel
  • \(f_x, f_y\): The frequency along x and y in cycles per pixel
  • \(D_0\): The cutoff frequency

D is the distance of a frequency from the center of the spectrum, in cycles per pixel, so the filter does the same thing on any image size. Gonzalez and Woods measure D in samples of the padded spectrum. Divide their value by the padded size to get the one here. The high-pass filter is 1 minus the Ideal low-pass filter. Ringing near edges comes from the impulse response of H, which is wider the sharper the cutoff.

The filter works on the spectrum of the image. D is the distance of a frequency from the center of the spectrum, in cycles per pixel, so the same settings do the same thing on any image size. A frequency of 0.5 cycles per pixel, a stripe pattern with a period of 2 pixels, is the finest detail a pixel grid can hold. Gonzalez and Woods measure D in samples of the padded spectrum instead. The high-pass response is 1 minus the Ideal low-pass response, so the two results add up to the image. The response jumps from 1 to 0, so its impulse response, the inverse transform of H, is a wide function with side lobes like a sinc. Those side lobes are the ringing next to edges.

Kernel or operator

The operator is the response H over the spectrum. The Inspector draws it as a gray image, with frequency 0 in the center, and as a profile along the f_x axis. The Frequency explorer in the Learn panel shows the spectrum of the image, H, their product and the result.

Algorithm

  1. Extend the image with the border mode by half its size, at most 256 pixels, on each side.
  2. Take the 2D FFT.
  3. Multiply each frequency by H.
  4. Take the inverse FFT and keep the real part.
  5. Crop the extension.

Parameters

Parameter Default Values Meaning
Cutoff cutoff 0.1 0.005 to 0.5 cycles/px The cutoff frequency D_0, in cycles per pixel. 0.5 is the finest detail a pixel grid can hold.
Output mapping outputMapping Offset by 128 Absolute value, Offset by 128, Stretch to full range, Clip to 0 to 255 How signed results, such as gradients, become 8-bit pixel values.
Border mode border Reflect Reflect 101 (default), Reflect, Replicate, Constant (zero), Wrap How pixels outside the image are filled when the window reaches past the edge.
Channels channelScope Brightness only RGB, Brightness only Which part of the color image the filter changes.

Implementation

idealHighpassFilter in web/src/core/filters/frequency/passband.ts builds H with passResponse. frequencyFilter in web/src/core/filters/frequency/spectral.ts does the rest. The image is extended with the border mode by half its size, at most 256 pixels, on each side. It gets a little more to reach a length whose only prime factors are 2, 3 and 5. The FFT handles such lengths fast. This is the padding step of Gonzalez and Woods, with the border mode in place of zeros. Without it, the circular wrap of the FFT would mix opposite edges of the image. The filter needs the whole spectrum, so it always runs on the whole image, and a selection is cut out of the result. The result is signed, so the output mapping turns it into pixel values. The default offsets 0 to gray 128.

Worked example

D (cycles per pixel) 0 0.05 0.1 0.15 0.2 0.25 0.3 0.5
H 0 0 0 1 1 1 1 1

The response at a few distances from the center of the spectrum, for D_0 = 0.1. The ideal filter switches between 1 and 0 at once. The high-pass response is 1 minus the low-pass response. In the zone plate of the figure, distance r from its center has frequency r / 160, so D = 0.1 lies 16 pixels from the center.

Complexity

  • Time: \(O(N log N)\)
  • Memory: \(O(N)\)

N is the number of pixels of the extended image, about 4 times the image for images up to 512 pixels on a side.

Artifacts and limitations

Limitations

  • The sharp cutoff makes strong ringing next to edges.
  • Amplifies noise, which lives at high frequencies.

Artifacts

  • Ringing, ripples that run parallel to strong edges.

Tests

The reference extends the plane with numpy.pad, filters it with numpy.fft and crops it, with the formulas of Gonzalez and Woods. Before the cases are written, the script checks the Gaussian low-pass filter against scipy.ndimage.gaussian_filter on a plane whose extension is an exact mirror. It also checks that each low-pass and high-pass pair, and each band-reject and band-pass pair, adds up to the image. The unit tests check the values at the cutoff and that a flat image passes a low-pass filter unchanged.

References

  • Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Chapter 4, filtering in the frequency domain.