Joshu Leonardy
階 · 調 · 変 · 換

Intensity & Histograms Lab

Every image processing course starts with operations that look at one pixel at a time. This lab covers the four ideas the rest of the subject builds on: the histogram, point transformations, histogram equalization and Otsu's threshold. Each one is on screen as an equation and as a result, and you can use your own photograph for all of them.

— 01 —
度 · 数 · 分 · 布

A histogram is a count

Gonzalez & Woods, 4th ed. §3.3 Histogram Processing, eq. 3-7 (p. 133)  ·  Burger & Burge — Histograms and image statistics

Below is an 8×8 image with only eight grey levels (3 bits). The bars count how many pixels hold each level, and that is the whole idea. Click a pixel to raise its value and right-click to lower it. Point at a bar to see which pixels it is counting.

pr(rk) = nk / MN

nk is the number of pixels with grey level rk. M×N is the size of the image, so dividing by it turns counts into fractions. That lets you compare images of different sizes, and the fractions always add up to exactly 1. Read pr(rk) as the probability that a randomly chosen pixel has level rk.

Image · 8 × 8, L = 8
Histogram · nk
Start from
Positions

The same count, on a real photograph

A photograph uses 256 levels instead of 8, but it is counted the same way. The tall bars show which levels dominate the picture, and the width of the histogram shows how much of the 0–255 range the picture actually uses.

Histogram
A histogram keeps how many pixels have each level and throws away where they are. Shuffling the pixels destroys the picture but leaves the histogram, the mean and the standard deviation unchanged. Every global method in the rest of this lab works from this count alone, so none of them can tell a face from the same pixels scrambled.
— 02 —
点 · 処 · 理

Point transformations: s = T(r)

Gonzalez & Woods, 4th ed. §3.2 Some Basic Intensity Transformation Functions (pp. 122–132)  ·  Burger & Burge — Point operations

A point transformation replaces each grey level r with a new level s. The output depends only on the pixel's own value, never on its neighbours or its position. That means the whole operation is one curve, drawn below. Point at any pixel in the image and its path through the curve is traced for you.

Point at the image to put a real pixel through the equation.
Transformation
Transfer function s = T(r)
Histogram before Histogram after
Look at levels used. A point transformation can merge grey levels, but it can never split one level into two, so this number can stay the same or fall but never rise. Whatever detail was lost when two levels were merged cannot be brought back by any later curve.
— 03 —
平 · 坦 · 化

Histogram equalization

Gonzalez & Woods, 4th ed. §3.3: Histogram Equalization (p. 134), Local Histogram Processing (p. 149)  ·  Burger & Burge — Point operations: histogram equalization

In section 02 you chose the curve yourself. Equalization builds the curve from the image's own histogram: the transformation is the cumulative histogram, scaled up to the full range. Levels that many pixels share get pulled apart, and levels that few pixels use get squeezed together.

sk = T(rk) = (L − 1)kΣj = 0pr(rj)

The sum is the fraction of pixels at or below level k. That fraction is always between 0 and 1 and never decreases as k rises. Multiplying by L − 1 stretches it to the full grey scale, and rounding gives an integer output level. Below, the table works through the 8×8 grid from section 01 one step at a time. Edit that grid and the table recalculates.

Before
After
Start from
The result is flatter but not flat. With discrete levels, equalization can only move whole bars, and it cannot split one level's pixels across two output levels. Several input levels often round to the same output, which is why the after column has empty rows.

Global versus local (CLAHE)

Global equalization uses one curve for the entire image, so a bright window and a dark corner get the same treatment. CLAHE (contrast-limited adaptive histogram equalization) computes a separate curve for each tile and blends neighbouring curves together so no seams show. Its clip limit caps how tall any one bar can grow before its curve is built. This stops a nearly uniform region, such as clear sky, from having its faint noise stretched into strong texture.

Method
Tiles
Clip limit
Before · line = cumulative histogram = T(r)
After · line = its cumulative histogram
— 04 —
二 · 値 · 化

Choosing a threshold: Otsu's method

Gonzalez & Woods, 4th ed. §10.3: Optimum Global Thresholding Using Otsu’s Method (p. 747), eqs. 10-61 to 10-64  ·  Burger & Burge — Automatic thresholding

Thresholding is the most extreme point transformation: every level at or below T becomes 0, and every level above it becomes 255. The hard part is choosing T. Otsu's method tries every possible threshold. For each one it measures how far apart the means of the two resulting classes are, weighted by how many pixels fall in each class. It keeps the threshold where that separation is largest, and it needs nothing except the histogram.

σB2(k) = [mGP1(k) − m(k)]2 / P1(k)[1 − P1(k)]

P1(k) is the fraction of pixels at or below k. m(k) is the cumulative mean up to k, and mG is the mean of the whole image. The between-class variance σB2 is large when the two classes sit far apart. Dividing it by the image's total variance σG2 gives η, a score from 0 to 1 for how well the image splits into two groups at all.

Threshold T
Automatic
Pre-process
Histogram · two classes
Between-class variance σ²B(k)
Try the uneven page. Its histogram has no clean valley, because the text in the shadow is as dark as the paper in the shadow. No single T can separate them. The real fix is a threshold that changes across the image (local or adaptive thresholding), covered by the moving-average method in lab 07.