Skip to content

Gamma correction

Fact Value
Also called Power-law transformation, Gamma adjustment
Status Implemented and tested
Family Enhancement, illumination and color
Engine Browser
Type Nonlinear, point, power-law transfer
Output Image
Operator shown as point-transfer (exact)
Support radius 0
Code web/src/core/filters/enhancement/gamma.ts, gammaFilter
Operator code web/src/core/filters/enhancement/gamma.ts, gammaTransfer
Channels RGB, Brightness only

Overview

Gamma correction bends the brightness curve with a power function. The middle tones get brighter or darker, while black and white stay where they are.

A plot of output value against input value for gamma 0.5, 1 and 2.2. A plot of output value against input value for gamma 0.5, 1 and 2.2.
Gamma below 1 lifts the midtones, gamma above 1 lowers them. 0 and 255 stay fixed.

How it works

  1. Turn the pixel value into a number from 0 to 1.
  2. Raise it to the power gamma.
  3. Turn it back into a value from 0 to 255 and round it.

Mathematical foundation

\[ I_{\text{out}} = 255 \left( \frac{I}{255} \right)^{\gamma}, \qquad \gamma = 0.6 \]

The formula shows the default parameter values.

where

  • \(I\): Input value from 0 to 255
  • \(I_{\text{out}}\): Output value before rounding
  • \(\gamma\): The exponent

The value is scaled to the range 0 to 1, raised to the power gamma and scaled back. 0 and 255 map to themselves. Gamma below 1 lifts every value in between, gamma above 1 lowers it. This is the power law used by Gonzalez and Woods and by skimage.exposure.adjust_gamma.

Raising a number between 0 and 1 to a power keeps 0 and 1 in place. A power below 1 moves every value in between up, a power above 1 moves it down. The curve is steepest where the tones are stretched, which can show banding in smooth gradients.

Kernel or operator

Gamma is a point operation, so it has no kernel. The Inspector shows the transfer curve: the output for each of the 256 input values, from the same function the filter runs.

Algorithm

  1. Compute the curve for all 256 input values once.
  2. Look up the output for each pixel and channel. Brightness values that are not whole numbers use the formula directly.
  3. Round to 8 bits. Keep alpha unchanged.

Parameters

Parameter Default Values Meaning
Gamma gamma 0.6 0.1 to 5 The exponent of the power curve.
Channels channelScope RGB RGB, Brightness only Which part of the color image the filter changes.

Implementation

gammaTransfer gives the curve. The filter computes it once for the 256 input values and then looks up each pixel. Brightness values that are not whole numbers use the formula directly. The filter runs in the browser engine, in Web Workers. The studio splits the image into strips. Each strip gets a halo as wide as the support radius, so the strips give exactly the pixels of a whole-image run. The shared filter framework in web/src/core/filters/run.ts handles the border mode, the channel scope, alpha and the final rounding to 8 bits.

Worked example

Input 0 32 64 128 192 255
255 (I/255)^0.5 0 90.33 127.75 180.67 221.27 255
Pixel 0 90 128 181 221 255

With gamma 0.5, a value of 64 becomes about 128. 0 and 255 do not move.

Complexity

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

N is the number of pixels. The 256-entry table makes each pixel a single lookup.

Artifacts and limitations

Limitations

  • Changes all midtones together. It cannot brighten one area and darken another.
  • Strong values compress the tones at one end, which can show banding.

Artifacts

  • Banding in smooth gradients after strong corrections, because 8-bit values get merged.

Tests

The curve is compared with skimage.exposure.adjust_gamma for gamma 0.45, 1 and 2.2 at all 256 input values, within 1e-6.

References