Gamma, Tonal Response Curve, and related concepts
- 1 Introduction
- 2 Why use logarithms?
- 3 How gamma encoding increases Dynamic Range
- 4 What gamma (and Tonal Response Curve) should I expect? And what is good?
- 5 What effect does entering gamma have on Imatest measurements?
- 6 Gamma and MTF measurement
- 7 How strongly does gamma affect MTF? i.e., if the estimate of gamma is incorrect, how are far off is the MTF?
- 8 Which patches are used to calculate gamma in Imatest?
- 9 History: where did gamma ≅ 2.2 come from?
- 10 Why are images converted from raw files often darker than JPEGs from the same capture?
- 11 Tone mapping
- 12 Contrast definitions
- 13 Logarithmic encoding
- 14 Appendix I: Monitor gamma
- 15 Appendix II: Tonal response, gamma, and related quantities
- 16 Appendix III: New linearization methods introduced in Imatest 23.2
- 16.1 Feature summary
- 16.2 INI settings
- 16.3 Outputs
Introduction
In this post we discuss a number of concepts related to tonal response and gamma that are scattered around the Imatest website, making them hard to find. They are assembled here in anticipation of questions about gamma and related concepts, which keep arising.
Gamma (γ) is the average slope of the function that relates the logarithm of pixel levels (in an image file) to the logarithm of exposure (in the scene).
This relationship is called the Tonal Response Curve, (It’s also called the OECF = Opto-Electrical Conversion Function). The average is typically taken over a range of pixel levels from light to dark gray.
Tonal Response Curves (TRCs) have been around since the nineteenth century, when they were developed by Hurter and Driffield— a brilliant accomplishment because they lacked modern equipment. They are widely used for film. The Tonal Response Curve for Fujichrome Provia 100F film is shown on the right. Note that the y-axis is reversed from the digital plot shown below, where log(normalized pixel level) corresponds to
Equivalently, gamma can be thought of as the exponent of the curve that relates pixel level to scene luminance.
There are actually two gammas:
encoding gamma, which relates scene luminance to image file pixel levels, and
display gamma, which relates image file pixel levels to display luminance. It is typically found in equations of the form,
The equation, (above and in most references to gamma on this page), refers to encoding gamma. Exceptions are for characterizing color spaces (such as sRGB, where display gamma is approximately 2.2) and when display gamma is explicitly referenced, as in the Appendix on Monitor gamma.
The overall system contrast is the product of the encoding and decoding gammas. More generally, we think of gamma as contrast.
Encoding gamma is applied in the image processing pipeline because the output of image sensors, which is linear for most standard (non-HDR) image sensors, is never gamma-encoded. Encoding gamma is typically measured from the tonal response curve, which can be obtained photographing a grayscale test chart and running Imatest’s Color/Tone module (or the legacy Stepchart and Colorcheck modules).
Display gamma is typically specified by the color space of the file. For the most common color space in Windows and the internet, sRGB, display gamma is approximately 2.2. (It actually consists of a linear segment followed by a gamma = 2.4 segment, which together approximate gamma = 2.2.) For virtually all computer systems, display gamma is set to correctly display images encoded in standard (gamma = 2.2) color spaces.
Here is an example of a tonal response curve for an in-camera JPEG for a high quality consumer camera, measured with Imatest Color/Tone.
from the bottom row of the 24-patch Colorchecker,
Note that this curve is not a straight line. It’s slope is reduced on the right, for the brightest scene luminance. This area of reduced slope is called the “shoulder”. It improves the perceived quality of pictorial images (family snapshots, etc.) by reducing saturation or clipping (“burnout”) of highlights, thus making the response more “film-like”. A shoulder is plainly visible in the lower-right of the Fujichorme Provia 100F curve, above. Shoulders are almost universally applied in consumer cameras; they’re less common in medical or machine vision cameras. [A region of reduced slope in dark regions is called a “toe”. It is common in film, but relatively rare in digital images.]
Because the tonal response is not a straight line, gamma has to be derived from the average (mean) value of a portion of the tonal response curve.
Why use logarithms?
Logarithmic curves have been used to express the relationship between illumination and response since the nineteenth century because the eye’s response to light is logarithmic. This is a result of the Weber-Fechner law, which states that the perceived change dp to a change dS in an initial stimulus S is
Applying a little math to this curve, we arrive at where ln is the natural logarithm (loge).
From G. Wyszecki & W. S. Stiles, “Color Science,” Wiley, 1982, pp. 567-570, the minimum light difference ΔL that can be perceived by the human eye is approximately
. This number may be too stringent for real scenes, where ΔL/L may be closer to 2%.
The brighter areas take up far too much space in linearly-displayed tonal response curves, making it difficult to see the response in darker areas.
How gamma encoding increases Dynamic Range
In photography, we often talk about zones (derived from Ansel Adams’ zone system). A zone is a range of illumination L that varies by a factor of two, i.e., if the lower and upper boundaries of a zone are L1 and L2, then L2 /L1 = 2 or log2(L2) – log2(L1) = 1.
The steps between each zone has equal visual weight, except for very dark zones where the differences are hard to see.
For a set of zones z = {1, 2, 3, ..} the relative illumination of zone n is 2-(z-1) = {1, 0.5, 0.25, 0.125, …}. The illumination difference between zones is {0.5, 0.25, 0.125, …}. The scene illumination ratio between zones is always 2:1.
Geek alert: a lot of numbers follow— all of which are intended to show how gamma encoding increases the effective dynamic range.
The total number of available pixel levels B is a function of the bit depth, B = 2(bit depth). For bit depth = 8, B = 256; for bit depth = 16, B = 65536. The relative pixel level of zone n for encoding gamma γe is bn = B × 2-(n-1)γe. For linear gamma (γe = 1), bn= {1.0, 0.5, 0.25, 0.125, …} (the same as L). For γe= 1/2.2 = 0.4545, bn = {1.0000, 0.7297, 0.5325, 0.3886, 0.2836, 0.2069, 0.1510 ,0.1102, …}. The relative pixel levels bn decrease much more slowly than for γe = 1.
The relative number of pixel levels in each zone is npγ(i) = bn(i) – bn(i+1), which brings us to the heart of the issue. For a maximum pixel level of 2(bit depth)-1 = 255 for widely-used files with bit depth = 8 (24-bit color), the total number of pixels in each zone is Npγ(i) = 2(bit depth)npγ(i).
For linear images (γe = 1), npγ(i) = {0.5, 0.25, 0.125, 0.0625, …}, i.e., half the pixel levels would be in the first zone, a quarter would be in the second zone, etc. For files with bit depth = 8, the zones starting from the brightest would have Npγ(i) = {128, 64, 32, 16, 8, 4, 2, 1 …} pixel levels of 256 (0-255). By the time you reached the 5th or 6th zone, the spacing between pixel levels would be small enough to cause significant “banding”, limiting the dynamic range.
For images encoded with γe = 1/2.2 = 0.4545, the relative number of pixels in each zone would be bn(i) = {0.2703, 0.1972, 0.1439, 0.1050, 0.0766, 0.0559, 0.0408, 0.0298, 0.0217, 0.0159…}, and the total number Npγ(i) would be {69.2, 50.5, 36.8, 26.9, 19.6, 14.3, 10.4, 7.63, 5.56, 4.06, …} of 256. The sixth zone, which has only 4 levels for γe = 1, has 14.3 levels. The 10th gamma-encoded zone has the same number of levels (4) as the 6th linear zone, i.e., an effective dynamic range increase of up to four zones (f-stops; factors of 2).
To summarize:
These numbers show how gamma-encoding greatly improves the effective dynamic range of images with limited bit depth by redistributing pixel levels so there are more in darker zones and fewer in lighter zones (which have way more than needed in linear systems).
Of course, the improvement will be less if flare light in dark regions is the limiting factor.
What gamma (and Tonal Response Curve) should I expect?
And what is good?
JPEG images from consumer cameras typically have complex tonal response curves (with shoulders), with gamma (average slope) in the range of 0.45 to 0.6. This varies considerably for different manufacturers and models. The shoulder on the tonal response curve allows the the slope in the middle tones to be increased without worsening highlight saturation. This increases the apparent visual contrast, resulting in “snappier” more pleasing images.
RAW images from consumer cameras have to be decoded using LibRaw/dcraw. Their gamma depends on the Output gamma and Output color space settings (display gamma is shown in the settings window). Typical results are
Encoding gamma ≅ 0.4545 with a straight line TRC (no shoulder) if conversion to a color space (usually sRGB or Adobe RGB) is selected;
Encoding gamma ≅ 1.0 if a minimally processed image is selected.
RAW images from binary files (usually from development systems) have straight line gamma of 1.0, unless the Read Raw Gamma setting (which defaults to 1) is set to a different value.
Flare light can reduce measured gamma by fogging shadows, flattening the Tonal Response Curve for dark regions. Care should be taken to minimize flare light when measuring gamma.
That said, we often see values of gamma that differ significantly from the expected values of ≅0.45-0.6 (for color space images) or 1.0 (for raw images without gamma-encoding). It’s difficult to know why without a hands-on examination of the system. Perhaps the images are intended for special proprietary purposes (for example, for making low contrast documents more legible by increasing gamma); perhaps there is a software bug.
What effect does entering gamma have on Imatest measurements?
Gamma can be entered into imatest in several places. Here is what it does, and equally important, what it does not do.
Gamma can be measured from grayscale test charts. Entered values of gamma do not affect grayscale measurements.
Gamma is used to linearize images for MTF calculations. The entered value of gamma can be overridden for slanted-edge patterns if the pattern contrast ratio (often 4) has been entered and Use for MTF has been checked.
In Flatfield, gamma is used to linearize ISO 18844 flare light measurements and also to calculate F-stop contour plots (not one of Imatest’s major displays; luminance (pixel) contour plots are usually preferred.)
In Color/Tone, gamma is used to linearize the image for calculating the Color Correction Matrix (important) and for correcting illumination nonuniformity (an infrequently-used feature that requires a second flat image).
Gamma and MTF measurement
MTF (Modulation Transfer Function, which is equivalent to Spatial Frequency Response), which is used to quantify image sharpness, is calculated assuming that the signal is linear. For this reason, gamma-encoded files must be linearized, i.e., the gamma encoding must be removed. The linearization doesn’t have to be perfect, i.e., it doesn’t have be the exact inverse of the tonal response curve. For most images (especially where the chart contrast is not too high), a reasonable estimate of gamma is sufficient for stable, reliable MTF measurements. The settings window for most MTF calculation has a box for entering gamma (or setting gamma to be calculated from the chart contrast).
Gamma is entered in the Setup or More settings window for each MTF module. They are described in the documentation for individual Rescharts modules. For Slanted-edge modules, they appear in the Setup window (crop shown on the right) and in the More settings window (crop shown below).
Gamma (input) defaults to 0.5, which is a reasonable approximation for color space files (sRGB, etc.), but is incorrect for raw files, where gamma ≅ 1. Where possible we recommend entering a measurement-based estimate of gamma.
Determine gamma from a grayscale pattern
Gamma is often measured from grayscale (OECF) patterns, which can be in separate charts or included in any of several sharpness charts— SFRplus, eSFR ISO, Star, Log F-Contrast, and Random (but not Checkerboard). The grayscale pattern from the eSFR ISO and SFRplus chart is particularly interesting because it shows the levels of the light and dark portions of the slanted-edge patters used to measure MTF.
Gamma = 0.588 here: close to the value (shown above) measured from the X-Rite Colorchecker for the same camera. The interesting thing about this plot is the pale horizontal bars, which represent the pixel levels of the light and dark portions of the selected slanted-edge ROIs (Regions of Interest). This lines let you see if the slanted-edge regions are saturating or clipping. This image shows that there will be no issue.
Determine gamma from each individual slanted-edge
Enter chart contrast and check Use for MTF. Only available for slanted-edge MTF modules. When Use for MTF is checked, the gamma (input) box is disabled.
This setting uses the measured of contrast of flat areas P1 and P2 (light and dark portions of each individual slanted-edge Region of Interest (ROI), away from the edge itself) to calculate the gamma for each edge. It is easy to use and quite robust. The printed chart contrast ratio (<=10) must be entered and the OECF curve should be close to