How to use this lab
Load or choose a grayscale image, select a transformation, and compare the source histogram with the transformed result. Use the controls to change the intensity range, then inspect how the mapping changes individual pixel values. A static example is the linear mapping from an input range of 50–150 to 0–255: an input value of 100 maps to roughly 128 after scaling and rounding.
The lab teaches point transformations, histogram reasoning, and cumulative distributions. It runs in the browser over canvas data; it does not upload images to a server, replace a production computer-vision pipeline, or guarantee that a visually stronger image is semantically better. For related practice, see image-processing practice and the Python and SQL guides.
How to use this tool
Use the lab to see how a grayscale pixel value is transformed. Choose an input image or preset, select a transformation, and compare the before-and-after canvas and histograms.
Worked example
Input pixels: [50, 100, 150]
Mapping: 50–150 -> 0–255
Static result: The mapped pixels are [0, 128, 255] after rounding. The histogram moves toward the selected output range.
Controls
The preset or upload control selects the source; transformation tabs choose contrast stretching, equalization, or matching; sliders change the range; Apply runs the selected mapping.
Honest limits
Processing runs in the browser on canvas pixel data. Images are not uploaded by this lab, and a stronger histogram does not guarantee a better semantic image.
Continue with image-processing practice.
Grayscale Intensity Transformations
In digital image processing, a grayscale image is represented mathematically as a two-dimensional discrete matrix f(x, y), where each spatial coordinate (x, y) corresponds to a quantized intensity level r lying within the bounded interval [0, L - 1] (typically [0, 255] for standard 8-bit unsigned integer channels). Point processing transformations modify individual pixel values through a transfer mapping function s = T(r) without taking spatial neighborhood kernels into account.
Mathematical Foundations of Contrast Stretching
Low-contrast images frequently result from poor illumination conditions, dynamic range limitations of image sensors, or incorrect aperture settings during photographic acquisition. Linear contrast stretching expands the narrow dynamic range [r_min, r_max] of an input image across the entire available intensity spectrum [s_min, s_max] (typically [0, 255]):
s = ((r - r_min) / (r_max - r_min)) * (s_max - s_min) + s_min
To prevent extreme outlier pixels from compressing the useful visual range, percentile-based histogram stretching computes low (e.g., 2nd percentile) and high (e.g., 98th percentile) cutoff thresholds. All pixel intensities below the low threshold or above the high threshold are clipped prior to linear remapping.
Cumulative Distribution Function (CDF) Equalization
Histogram equalization is an automated point-processing technique designed to linearize the cumulative distribution function (CDF) of an image, producing an output image with an approximately uniform probability density function (PDF). For a discrete image containing N total pixels and L = 256 discrete intensity bins:
p_r(r_k) = n_k / N, for k = 0, 1, ..., L - 1
s_k = T(r_k) = (L - 1) * SUM(p_r(r_j)) from j = 0 to k
Because digital intensities are integer-quantized, the discrete transformation maps each input level r_k to the nearest integer round((L - 1) * CDF(r_k)). This redistributes clumped frequency clusters across the dynamic range, revealing fine shadow and highlight details.
Histogram Matching Algorithms
While standard histogram equalization yields a uniform histogram, many computer vision and medical imaging applications require an image to conform to a specific non-uniform target distribution (such as Gaussian bell curves or multi-modal target profiles). Histogram specification (matching) establishes a monotonic mapping between the source cumulative distribution CDF_src(r) and the target reference distribution CDF_tgt(z):
1. Compute normalized CDF_src(r) for all r in [0, 255]
2. Compute normalized CDF_tgt(z) for the target reference image
3. For each intensity level r, find the smallest z such that CDF_tgt(z) >= CDF_src(r)
4. Remap pixels using lookup table: s(x, y) = mapping[r(x, y)]
This method aligns illumination profiles between multiple sensor feeds, normalizes training data distributions for machine learning pipelines, and standardizes multi-spectral satellite imagery.