Contrast stretching¶
| Fact | Value |
|---|---|
| Also called | Piecewise-linear contrast stretch, Levels |
| Status | Implemented and tested |
| Family | Enhancement, illumination and color |
| Engine | Browser |
| Type | Nonlinear, point, piecewise-linear transfer |
| Output | Image |
| Operator shown as | point-transfer (exact) |
| Support radius | 0 |
| Code | web/src/core/filters/enhancement/point-ops.ts, contrastStretchFilter |
| Operator code | web/src/core/filters/enhancement/point-ops.ts, stretchTransfer |
| Channels | RGB, Brightness only |
Overview¶
Contrast stretching remaps brightness with a curve made of three straight segments. Two control points set where the segments meet. A steep middle segment spreads the values it covers, which raises their contrast. Placing both points at the same input turns the curve into a threshold.
How it works¶
- Sort the two control points by their input values.
- Join (0, 0), the two points and (255, 255) with straight lines.
- Look up each pixel value on this curve.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(I\): Input value from 0 to 255
- \((r_1, s_1), (r_2, s_2)\): The two control points, input and output
The curve runs in straight lines through (0, 0), (r1, s1), (r2, s2) and (255, 255), the contrast stretching of Gonzalez and Woods. A middle segment steeper than 45 degrees raises the contrast of the values it covers. When r1 is above r2, the two points swap. With r1 = r2, s1 = 0 and s2 = 255, the curve jumps from 0 to 255 and becomes a threshold. When r1 is 0, the value 0 maps to s1. When r2 is 255, the value 255 maps to s2.
Each segment multiplies the differences between the values it covers by its slope. A slope above 1 stretches them and raises contrast, a slope below 1 presses them together. The slopes of the three segments must balance, since the curve always starts at 0 and ends at 255.
Kernel or operator¶
The Inspector draws the transfer curve for the current settings, with the input value on the horizontal axis and the output on the vertical axis. The Point-op curve in the Learn panel draws the same curve with the histograms of the open image before and after.
Algorithm¶
- Sort the control points by their input values.
- Build the 256-entry table from the three straight segments.
- Look up every pixel value in the table.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Input 1 r1 |
64 | 0 to 255 | The input value r1 of the first control point. |
Output 1 s1 |
32 | 0 to 255 | The output value s1 of the first control point. |
Input 2 r2 |
192 | 0 to 255 | The input value r2 of the second control point. |
Output 2 s2 |
224 | 0 to 255 | The output value s2 of the second control point. |
Channels channelScope |
RGB | RGB, Brightness only | Which part of the color image the filter changes. |
Implementation¶
contrastStretchFilter in web/src/core/filters/enhancement/point-ops.ts uses stretchTransfer. The filter is a point filter: the runner builds a table of 256 outputs once and looks up each pixel. Values between two levels, as luma often is, go through the curve directly.
Worked example¶
| Input | 0 | 32 | 64 | 128 | 192 | 224 | 255 |
|---|---|---|---|---|---|---|---|
| T(I) | 0 | 16 | 32 | 128 | 224 | 239.75 | 255 |
| Pixel | 0 | 16 | 32 | 128 | 224 | 240 | 255 |
With the default control points, the middle segment has slope (224 - 32) / (192 - 64) = 1.5, so the midtones gain contrast. The outer segments have slope 0.5 and press the shadows and highlights together.
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
- The control points are fixed numbers. Percentile normalization finds them from the image.
- Steep segments merge nearby values in the flat segments.
Artifacts
- Banding where a steep segment spreads a few input levels over many output levels.
Tests¶
The reference writes the same curve with NumPy and applies it to the random test plane. The cases include swapped control points, a threshold, and control points at 0 and 255.
References¶
- Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Chapter 3.