Comparison of highpass filters in GIMP and GMIC

When it comes to sharpening an image, a popular alternative to the unsharp mask is the high pass filter. To apply this method, you create the high pass version of your image in a new layer and set its blend mode to overlay. Applying a layer mask allows you to restrict sharpening to certain regions of the image.
Unfortunately gimp does not provide the high pass filter out of the box. You either need to install gmic or the high pass plugin. They are more or less the same but show subtle differences in the implemenatation which can be summarized as follows:
  • As of today gimp still is an 8bit image processor. So you might experience 8bit quantisation effects. I never did, though.
  • With the gmic filter you will avoid any 8bit quantisation effects. On the other hand, due to the way it is implemented, the high pass filter in gmic internally applies an S-curve to the high pass filter. This increases the sharpening effect in certain regions.

For practical purposes I don't think it matters which filter you use. If you don't fear some math and should you be interested in more details the following paragraphs will provide further insight.

How does a high pass filters work?

Fourrier analysis teaches us that, every sound wave can be decomposed into high and low frequencies. This allows us for instance to eliminate constant humming in the background of a sound recording. You apply a high pass filter which blocks the low frequency of the humming but allows the higher frequencies of human speach to pass through. You can do the same to a digital image. The lower frequencies describe the course information like a big patch of color while the high frequencies describe the details in an image. In our example the texture in the color patch.
In practice the high pass information is filtered by creating a blurred copy of your image (these are the low frequencies) and substracting it from the original layer.
The GIMP high pass filter works as follows:
  • Create a copy $L_l$ of your original layer $L_o$
  • Apply a gaussian blur (Filter -> Blur) to $L_l$
    This layer now contains the low frequencies.
  • Set the layer mode of $L_l$ to "grain extract" (your image will look strange)
  • Creat a copy ($L_h$) from the strange looking image (Layer dialog -> Right click -> New from visible)
    This layer now contains the high frequencies
  • Return $L_h$ and delete the rest.
The GMIC counterpart works as follows
  • Create a copy $L_l$ of your original layer $L_o$
  • Apply a gaussian blur (Filter -> Blur) to $L_l$
  • Invert the color of $L_l$
  • Set layer mode of $L_l$ to "compose interpolation". This is a GMIC specific mode!
  • Create a copy $L_h$ from visible
  • Increase constrast of $L_h$ by factor 2
  • Return $L_h$ and delete the rest

To understand the differences we compare both filters on a mathematical basis. For simplicity I apply the calculations to a single greyscale pixel with values restricted from 0 to 255. The formulas for the blend modes were taken from the GMIC overview page. Thus the individual steps for the GIMP plugin can be written as:

  • Gaussian blur: $I\textrm{gauss}$
  • Grain extract: $I_a - I_b + 128$

Using layer variables introduced above, the high pass layer is given by:
$$L_h = L_o - (L_o  \textrm{gauss}) + 128$$

$L_o*\textrm{gauss}$ describes the gaussian blur applied to the copy of the original image. Substracting this from the original layer and adding 128 is what the grain extract is doing.
Note: Without grain extract adding 128, we will loose half of the highpass information as some values become negative and would be clipped to 0.

The individual steps for the GMIC filter are:
  • Gaussian blur: $I\textrm{gauss}$
  • Color Inversion: $255-I$
  • Compose interpolation: $\frac{255}{2}-\frac{255}{4}\cos(\frac{I_a\pi}{255})-\frac{255}{4}\cos(\frac{I_b\pi}{255})$
  • Contrast increase by factor c: $128+(I-128)*c$

Written in a single equation gives:

$$L_h = 128+(\frac{255}{2}-\frac{255}{4}\cos(\frac{(255-L_o\textrm{gauss})\pi}{255})-\frac{255}{4}\cos(\frac{L_o\pi}{255})-128)c$$
Using the standard value c=2 given in GMIC and simplifing returns
$$L_h = -128\cos(\frac{L_o\pi}{255})+128\cos(\frac{L_o\textrm{gauss}\pi}{255})+128$$
Even with the plus and minus interchanged, this is exactly the same result as given by the GIMP plugin except for the cosine which applies an S-curve onto the high pass filter.
Using the "average" blending mode instead of the "interpolation" mode omits the S-curve. As seeing is believing, just run this algorithm manually on a gradient image and compare the results.