CSE 4227 · Chapter 3 · Deck 05

Histogram Processing, from zero

Every slide you need, in plain words, with every histogram and curve drawn, and the past-paper questions solved the way you write them in the answer script.

★ Must study on this page

Start here: what a histogram is

slides 1–5Came in past papers
In plain wordsA histogram is a head count of an image. For each gray level, count how many pixels have it, then draw one bar per level. Tall bar = many pixels at that brightness. That's all a histogram is.

Slide 3. Image A looks foggy and gray; image B looks sharp and punchy. They show the same pollen. The difference is in how the gray levels are used, and the histogram shows that difference at a glance.

Slide 4. Definition. An image histogram is a graphical representation of the distribution of pixel intensity values in a digital image.

Applications: image enhancement, compression, segmentation. Very popular tool for real-time image processing.

Slide 5. The histogram function. For gray levels in [0, L−1], the histogram is the discrete function

h(rk) = nk

where rk is the k-th gray level and nk is the number of pixels with gray level rk.

How to make a histogram, and what it tells you

Must studyslides 6–17Came in past papers
Slides 6–7 · 3-bit 4×4 exampleworked

Given: 3-bit image, so L = 23 = 8 levels (0–7). One bin per level: h(0) … h(7).

5533
4333
0672
1722
image f

Count each value: 0 appears once, 1 once, 2 three times, 3 five times, 4 once, 5 twice, 6 once, 7 twice.

rk01234567
h(rk)11351212
2.551011325314251627intensity levelnk
Histogram: one bar per gray level

Sum of all bars = 16 = number of pixels. The slide writes this as Σ h(x) = area of the image.

Try it, step by step: count each gray level of the slide 7 image and watch its bar grow.

Slide 8 · Class practicesolved

sample.bmp is a 4×4 image with 6-bit gray values. Calculate its histogram and sketch it.

20211560
60156015
15602115
15201520
sample.bmp (6-bit, levels 0–63)

n = 16 pixels. Only four values occur:

rk15202160all other levels
h(rk)73240
3.57715320221460input intensitynk
Sketch: bars at 15, 20, 21, 60; every other level 0 to 63 has height 0.

Try it, step by step: the 6-bit sample.bmp: only 4 levels occur, all other bins stay 0.

Slide 9. What a histogram says. In the example h(2) = 10: ten pixels have intensity 2. A histogram gives only statistical information. It gives no indication of the location of the pixels.

Slides 10–13. Can two images have the same histogram? Can you rebuild an image from its histogram?

Slide 14. Colour image histogram, two types: (1) intensity histogram: convert the colour image to gray scale and show that histogram; (2) individual colour channel histograms: 3 histograms, one each for R, G, B.

Slide 15. Four basic types of image and their histograms

dark → brightn
dark
dark → brightn
bright
dark → brightn
low contrast
dark → brightn
high contrast

Slide 16. Why the histogram is important. Its shape gives key information on the overall quality of the image and the possibility for enhancement. Looking at it is a fast way to judge quality. Processing the histogram improves an image quickly and automatically.

Slide 17. Uses of the histogram:

Normalized histogram (PDF)

slides 18–20On study list
In plain wordsA count like '4 pixels' means little unless you know the image size. Divide every count by the total number of pixels and you get a fraction: 'one quarter of the image has level 4'. Those fractions are the normalized histogram. They always add up to 1.
p(rk) = nk / n,   where n = Σ nk
Slide 19 · workedworked
9898
2747
6461
4074
image (cropped), n = 16
bin0123456789
h(rk)1110402322
p(rk)1/161/161/1604/1602/163/162/162/16
0.1250.250.062500.062510.0625230.25450.12560.18870.12580.1259gray levelp(rk)
Normalized histogram; bars add to 1

Try it, step by step: count, then divide each count by N = 16.

Slide 20 · Class worksolved

sample.bmp is a 4×4 image with 8-bit gray values. Calculate its normalized histogram (PDF).

2021015060
6015060150
15060210150
1502015020
sample.bmp (8-bit)

n = 16. Counts: 20 → 3, 60 → 4, 150 → 7, 210 → 2; all other levels 0.

rknkp(rk) = nk/16
2033/16 = 0.1875
6044/16 = 0.2500
15077/16 = 0.4375
21022/16 = 0.1250
sum161.0000
0.21880.43750.1875200.25600.43751500.125210gray levelp(rk)
PDF of sample.bmp

Histogram sliding and stretching

slides 21–27Know in 2 lines

Slide 21. Histogram processing is an enhancement technique: a transformation T is applied to the input f(x, y) to give the output g(x, y) = T(f(x, y)). T can be (i) histogram sliding, (ii) histogram stretching, (iii) histogram equalization.

Slide 22. Sliding: the whole histogram is simply shifted right or left, so you see a clear change in the brightness of the image. Shift right = brighter, shift left = darker.

4.59609142134567gray levelnk
Before: 3-bit dark image
4.59012639445167gray levelnk
After adding 3 to every pixel: same shape, moved right, brighter

Slides 23–24. Stretching increases the contrast (contrast = max − min pixel value). The histogram is pulled out to fill the whole range:

g = 255 (f − MIN) / (MAX − MIN)

Three steps hidden in the formula: (1) slide the histogram down to 0 (f − MIN); (2) normalize it to [0, 1] (÷ (MAX − MIN)); (3) rescale to [0, 255] (× 255).

Mini illustration (3-bit, so 7 replaces 255)worked

Levels used: 2 to 4. g = 7(f − 2)/(4 − 2): 2 → 0, 3 → 3.5 → 4, 4 → 7.

4.5901529364567gray levelnk
Before: narrow, low contrast
4.5950123945667gray levelnk
After stretching: spread over 0–7, but the same 3 bars with gaps between

Slides 25–26. Examples: a gray, low-contrast face becomes clearer. Stretching works better if extreme intensities are clipped first (city photo).

Slide 27. Limitation of stretching:

Histogram equalization: the idea and the algorithm

Must studyslides 28–35Came in past papers
In plain wordsEqualization tries to give every gray level roughly the same number of pixels, so the histogram becomes flat. Crowded levels get pulled apart and spread over the whole range 0 to L−1. You don't choose any setting; the image's own counts decide the mapping. That's why it's called automatic.

Slide 28. Equalization stretches the histogram to fill the dynamic range and at the same time tries to keep the histogram uniform. Linear stretching only stretches; the bars keep their heights.

Slides 29–30. Case A (satellite image): after equalization the result looks high-contrast with a large variety of gray tones, but the histogram is not perfectly flat.

Slide 31. Histogram equalization is used to enhance contrast. Usually it can't perfectly equalize a digital image's histogram. That's only possible with continuous intensity values; real intensity values are discrete, so perfectly flat histograms are rare in practice.

Slide 32. It redistributes pixel intensities so they span the full range more evenly. Original: intensities clustered in a narrow range (too dark, too bright or low contrast). After: spread out, details more visible, contrast improved. It tends to enhance contrast and can be performed automatically.

Slide 34. The algorithm (memorize these 5 steps)

Input: a discrete image f with L intensity levels.

  1. Step 1: compute the histogram of f: h(rk) = nk.
  2. Step 2: compute the probability distribution (PDF/PMF): p(rk) = nk / N.
  3. Step 3: compute the cumulative distribution (CDF): CDF(i) = running sum of the PDF up to i.
  4. Step 4: calculate the mapping: si = CDF(i) × (L − 1).
  5. Step 5: round sk to get the new intensity values.

Why the CDF? It counts the fraction of pixels darker than or equal to r. If 35% of pixels are at or below r, r goes to 35% of the way up the gray scale. Crowded levels make the CDF jump, which pushes neighbouring levels apart.

Slides 36–40 · 3-bit 4×5 exampleworked

Given: 3-bit image, so L = 23 = 8 and L − 1 = 7. Total pixels N = 20.

Step 1, histogram h(rk) = nk: count the pixels at each gray level.

rk01234567
nk16323212

Check: Σ nk = 1 + 6 + 3 + 2 + 3 + 2 + 1 + 2 = 20.

361061322334251627rknk
Original histogram (nk against rk)
0.150.300.0500.3010.1520.1030.1540.1050.0560.107rkp(rk)
Normalized histogram p(rk) = nk/N

Steps 2–5. PDF p(rk) = nk/N; CDF = running sum of the PDF; multiply by (L − 1) = 7; round off (.5 goes up).

rkStep 1: nkStep 2: PDFStep 3: CDFStep 4: (L−1)·CDFStep 5: sk
010.05000.05007 × 0.0500 = 0.35000
160.30000.35007 × 0.3500 = 2.45002
230.15000.50007 × 0.5000 = 3.50004
320.10000.60007 × 0.6000 = 4.20004
430.15000.75007 × 0.7500 = 5.25005
520.10000.85007 × 0.8500 = 5.95006
610.05000.90007 × 0.9000 = 6.30006
720.10001.00007 × 1.0000 = 7.00007

Mapping the new gray levels into number of pixels:

gray level rknew gray level skno. of pixels nk
001
126
243
342
453
562
661
772

Equalized histogram: add the nk that land on the same sk. Levels not listed get 0 pixels.

skpixels addedn(sk)
011
266
43 + 2 = 55
533
62 + 1 = 33
722
3610162354353627sknk
Equalized histogram (n against new level sk)
0123456701234567input rkoutput sk
Transformation function sk = T(rk) drawn as a staircase (slide 40 style). Empty levels keep the previous step.

Try it, step by step: the whole slide 36–40 example: count, the 5 table steps per level, the equalized histogram, then the output image pixel by pixel.

Slides 41–45. The four basic image types (dark, bright, low-contrast, high-contrast) all look similar after equalization, with histograms spread over the full range. Each one gets its own transformation curve (Figure 3.21).

Equalization past questions, solved in full

Must studyslides 33–40Came in past papers

Every equalization question has the same skeleton: histogram → PDF → CDF → (L−1)·CDF → round → mapping table → equalized histogram → staircase curve → output image. Learn it once; the solutions below repeat it with different numbers.

SP25 Q2(a) · SP24 Q2(a)8–9 marks

For the 5×5, 3-bit input image, find a transformation function that will approximately equalize its histogram. (i) Illustrate in detail (ii) give the equalized output image (iii) sketch the transformation curve (iv) draw the input and output histograms.

Given: 3-bit image, so L = 23 = 8 and L − 1 = 7. Total pixels N = 25.

00112
01124
11245
13456
33567
input image

Step 1, histogram h(rk) = nk: count the pixels at each gray level.

rk01234567
nk37333321

Check: Σ nk = 3 + 7 + 3 + 3 + 3 + 3 + 2 + 1 = 25.

3.573071323334352617rknk
Original histogram (nk against rk)
0.140.280.1200.2810.1220.1230.1240.1250.0860.047rkp(rk)
Normalized histogram p(rk) = nk/N

Steps 2–5. PDF p(rk) = nk/N; CDF = running sum of the PDF; multiply by (L − 1) = 7; round off (.5 goes up).

rkStep 1: nkStep 2: PDFStep 3: CDFStep 4: (L−1)·CDFStep 5: sk
030.12000.12007 × 0.1200 = 0.84001
170.28000.40007 × 0.4000 = 2.80003
230.12000.52007 × 0.5200 = 3.64004
330.12000.64007 × 0.6400 = 4.48004
430.12000.76007 × 0.7600 = 5.32005
530.12000.88007 × 0.8800 = 6.16006
620.08000.96007 × 0.9600 = 6.72007
710.04001.00007 × 1.0000 = 7.00007

Mapping the new gray levels into number of pixels:

gray level rknew gray level skno. of pixels nk
013
137
243
343
453
563
672
771

Equalized histogram: add the nk that land on the same sk. Levels not listed get 0 pixels.

skpixels addedn(sk)
133
377
43 + 3 = 66
533
633
72 + 1 = 33
3.5703127364353637sknk
Equalized histogram (n against new level sk)
0123456701234567input rkoutput sk
Transformation function sk = T(rk) drawn as a staircase (slide 40 style). Empty levels keep the previous step.

Equalized output image: replace every pixel r by its s.

00112
01124
11245
13456
33567
input
11334
13345
33456
34567
44677
equalized output

Try it, step by step: SP25 / SP24, every step.

Fall 25 Quiz (set C) Q214 marks

A 4×5, 3-bit image [1,2,4,7,0; 2,1,3,1,5; 5,2,1,4,1; 7,6,3,1,4]. (i) Draw the histogram (ii) sketch the normalized histogram (iii) compute and sketch the equalized histogram (iv) sketch the transformation function curve (v) show the equalized output image.

Given: 3-bit image, so L = 23 = 8 and L − 1 = 7. Total pixels N = 20.

12470
21315
52141
76314
input image

Step 1, histogram h(rk) = nk: count the pixels at each gray level.

rk01234567
nk16323212

Check: Σ nk = 1 + 6 + 3 + 2 + 3 + 2 + 1 + 2 = 20.

361061322334251627rknk
Original histogram (nk against rk)
0.150.300.0500.3010.1520.1030.1540.1050.0560.107rkp(rk)
Normalized histogram p(rk) = nk/N

Steps 2–5. PDF p(rk) = nk/N; CDF = running sum of the PDF; multiply by (L − 1) = 7; round off (.5 goes up).

rkStep 1: nkStep 2: PDFStep 3: CDFStep 4: (L−1)·CDFStep 5: sk
010.05000.05007 × 0.0500 = 0.35000
160.30000.35007 × 0.3500 = 2.45002
230.15000.50007 × 0.5000 = 3.50004
320.10000.60007 × 0.6000 = 4.20004
430.15000.75007 × 0.7500 = 5.25005
520.10000.85007 × 0.8500 = 5.95006
610.05000.90007 × 0.9000 = 6.30006
720.10001.00007 × 1.0000 = 7.00007

Mapping the new gray levels into number of pixels:

gray level rknew gray level skno. of pixels nk
001
126
243
342
453
562
661
772

Equalized histogram: add the nk that land on the same sk. Levels not listed get 0 pixels.

skpixels addedn(sk)
011
266
43 + 2 = 55
533
62 + 1 = 33
722
3610162354353627sknk
Equalized histogram (n against new level sk)
0123456701234567input rkoutput sk
Transformation function sk = T(rk) drawn as a staircase (slide 40 style). Empty levels keep the previous step.

Equalized output image: replace every pixel r by its s.

12470
21315
52141
76314
input
24570
42426
64252
76425
equalized output

Try it, step by step: Fall 25 quiz set C, every step.

Fall 25 Quiz (set D) Q2(b)10 marks

A 3-bit 8×8 image has histogram counts {{0:2, 1:3, 2:5, 3:6, 4:9, 5:12, 6:14, 7:13}}. (i) Sketch the normalized histogram (ii) compute and sketch the equalized histogram (iii) sketch the transformation function curve.

Given: 3-bit image, so L = 23 = 8 and L − 1 = 7. Total pixels N = 64.

Step 1, histogram h(rk) = nk: count the pixels at each gray level.

rk01234567
nk23569121413

Check: Σ nk = 2 + 3 + 5 + 6 + 9 + 12 + 14 + 13 = 64.

7142031526394125146137rknk
Original histogram (nk against rk)
0.110.220.0300.0510.0820.0930.1440.1950.2260.207rkp(rk)
Normalized histogram p(rk) = nk/N

Steps 2–5. PDF p(rk) = nk/N; CDF = running sum of the PDF; multiply by (L − 1) = 7; round off (.5 goes up).

rkStep 1: nkStep 2: PDFStep 3: CDFStep 4: (L−1)·CDFStep 5: sk
020.03120.03127 × 0.0312 = 0.21880
130.04690.07817 × 0.0781 = 0.54691
250.07810.15627 × 0.1562 = 1.09381
360.09380.25007 × 0.2500 = 1.75002
490.14060.39067 × 0.3906 = 2.73443
5120.18750.57817 × 0.5781 = 4.04694
6140.21880.79697 × 0.7969 = 5.57816
7130.20311.00007 × 1.0000 = 7.00007

Mapping the new gray levels into number of pixels:

gray level rknew gray level skno. of pixels nk
002
113
215
326
439
5412
6614
7713

Equalized histogram: add the nk that land on the same sk. Levels not listed get 0 pixels.

skpixels addedn(sk)
022
13 + 5 = 88
266
399
41212
61414
71313
714208162931245146137sknk
Equalized histogram (n against new level sk)
0123456701234567input rkoutput sk
Transformation function sk = T(rk) drawn as a staircase (slide 40 style). Empty levels keep the previous step.

Try it, step by step: Fall 25 quiz set D: counts are given, so it starts at the table.

F22 A3(b) · same counts as slide 498 marks

Histogram r = 0..7, n = [790, 1023, 850, 656, 329, 245, 122, 81]. (i) normalized histogram (ii) equalized histogram (iii) mapping of new gray levels into number of pixels (iv) transformation curve.

Here, as on slide 49, the PDF is rounded to 2 decimal places before the CDF.

Given: 3-bit image, so L = 23 = 8 and L − 1 = 7. Total pixels N = 4096.

Step 1, histogram h(rk) = nk: count the pixels at each gray level.

rk01234567
nk790102385065632924512281

Check: Σ nk = 790 + 1023 + 850 + 656 + 329 + 245 + 122 + 81 = 4096.

511.5102379001023185026563329424551226817rknk
Original histogram (nk against rk)
0.120.250.1900.2510.2120.1630.0840.0650.0360.027rkp(rk)
Normalized histogram p(rk) = nk/N

Steps 2–5. PDF p(rk) = nk/N; CDF = running sum of the PDF; multiply by (L − 1) = 7; round off (.5 goes up).

rkStep 1: nkStep 2: PDFStep 3: CDFStep 4: (L−1)·CDFStep 5: sk
07900.190.197 × 0.19 = 1.331
110230.250.447 × 0.44 = 3.083
28500.210.657 × 0.65 = 4.555
36560.160.817 × 0.81 = 5.676
43290.080.897 × 0.89 = 6.236
52450.060.957 × 0.95 = 6.657
61220.030.987 × 0.98 = 6.867
7810.021.007 × 1.00 = 7.007

Mapping the new gray levels into number of pixels:

gray level rknew gray level skno. of pixels nk
01790
131023
25850
36656
46329
57245
67122
7781

Equalized histogram: add the nk that land on the same sk. Levels not listed get 0 pixels.

skpixels addedn(sk)
1790790
310231023
5850850
6656 + 329 = 985985
7245 + 122 + 81 = 448448
511.51023079012102334850598564487sknk
Equalized histogram (n against new level sk)
0123456701234567input rkoutput sk
Transformation function sk = T(rk) drawn as a staircase (slide 40 style). Empty levels keep the previous step.

Try it, step by step: F22 with the PDF rounded to 2 places, as on slide 49.

F23 Q2(a) · Qubits.png6 marks

4×4, 5-bit image. (i) histogram (ii) compute and sketch the normalized histogram (iii) compute and sketch the equalized histogram (iv) sketch the transformation curve.

Given: 5-bit image, so L = 25 = 32 and L − 1 = 31. Total pixels N = 16.

16301018
251012
161395
1914280
input image

Step 1, histogram h(rk) = nk: count the pixels at each gray level.

rk012345678910111213141516171819202122232425262728293031
nk21000100011011102011000001001010

Check: Σ nk = 2 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 2 + 1 + 1 + 1 + 1 + 1 = 16.

122011151911011211311421611811912512813031rknk
Original histogram (nk against rk)
0.060.120.1200.0610.0650.0690.06100.06120.06130.06140.12160.06180.06190.06250.06280.063031rkp(rk)
Normalized histogram p(rk) = nk/N

Steps 2–5. PDF p(rk) = nk/N; CDF = running sum of the PDF; multiply by (L − 1) = 31; round off (.5 goes up). Only the intensities with non-zero occurrence are listed, as the slide says.

rkStep 1: nkStep 2: PDFStep 3: CDFStep 4: (L−1)·CDFStep 5: sk
020.12500.125031 × 0.1250 = 3.87504
110.06250.187531 × 0.1875 = 5.81256
510.06250.250031 × 0.2500 = 7.75008
910.06250.312531 × 0.3125 = 9.687510
1010.06250.375031 × 0.3750 = 11.625012
1210.06250.437531 × 0.4375 = 13.562514
1310.06250.500031 × 0.5000 = 15.500016
1410.06250.562531 × 0.5625 = 17.437517
1620.12500.687531 × 0.6875 = 21.312521
1810.06250.750031 × 0.7500 = 23.250023
1910.06250.812531 × 0.8125 = 25.187525
2510.06250.875031 × 0.8750 = 27.125027
2810.06250.937531 × 0.9375 = 29.062529
3010.06251.000031 × 1.0000 = 31.000031

Mapping the new gray levels into number of pixels:

gray level rknew gray level skno. of pixels nk
042
161
581
9101
10121
12141
13161
14171
16212
18231
19251
25271
28291
30311

Equalized histogram: add the nk that land on the same sk. Levels not listed get 0 pixels.

skpixels addedn(sk)
422
611
811
1011
1211
1411
1611
1711
2122
2311
2511
2711
2911
3111
120241618110112114116117221123125127129131sknk
Equalized histogram (n against new level sk)
048121620242831048121620242831input rkoutput sk
Transformation function sk = T(rk) drawn as a staircase (slide 40 style). Empty levels keep the previous step.

Equalized output image: replace every pixel r by its s.

16301018
251012
161395
1914280
input
21311223
276414
2116108
2517294
equalized output

Try it, step by step: F23 (5-bit): only the levels that occur get a table row.

SP22 Q2(b)8 marks

4×4, 5-bit image. (i) histogram (ii) normalized histogram (iii) equalized histogram (iv) transformation curve.

Given: 5-bit image, so L = 25 = 32 and L − 1 = 31. Total pixels N = 16.

1110127
6877
561111
9977
input image

Step 1, histogram h(rk) = nk: count the pixels at each gray level.

rk012345678910111213141516171819202122232425262728293031
nk00000125121310000000000000000000

Check: Σ nk = 1 + 2 + 5 + 1 + 2 + 1 + 3 + 1 = 16.

2.550152657182911031111231rknk
Original histogram (nk against rk)
0.160.3100.0650.1260.3170.0680.1290.06100.19110.061231rkp(rk)
Normalized histogram p(rk) = nk/N

Steps 2–5. PDF p(rk) = nk/N; CDF = running sum of the PDF; multiply by (L − 1) = 31; round off (.5 goes up). Only the intensities with non-zero occurrence are listed, as the slide says.

rkStep 1: nkStep 2: PDFStep 3: CDFStep 4: (L−1)·CDFStep 5: sk
510.06250.062531 × 0.0625 = 1.93752
620.12500.187531 × 0.1875 = 5.81256
750.31250.500031 × 0.5000 = 15.500016
810.06250.562531 × 0.5625 = 17.437517
920.12500.687531 × 0.6875 = 21.312521
1010.06250.750031 × 0.7500 = 23.250023
1130.18750.937531 × 0.9375 = 29.062529
1210.06251.000031 × 1.0000 = 31.000031

Mapping the new gray levels into number of pixels:

gray level rknew gray level skno. of pixels nk
521
662
7165
8171
9212
10231
11293
12311

Equalized histogram: add the nk that land on the same sk. Levels not listed get 0 pixels.

skpixels addedn(sk)
211
622
1655
1711
2122
2311
2933
3111
2.5501226516117221123329131sknk
Equalized histogram (n against new level sk)
048121620242831048121620242831input rkoutput sk
Transformation function sk = T(rk) drawn as a staircase (slide 40 style). Empty levels keep the previous step.

Equalized output image: replace every pixel r by its s.

1110127
6877
561111
9977
input
29233116
6171616
262929
21211616
equalized output

Try it, step by step: SP22 (5-bit), every step.

F21 B3(a) · Enigma.png8 marks

(i) What is a histogram? Calculate it. (ii) normalized histogram (iii) equalized histogram (iv) transformation curve. 4×4, 5-bit image.

Histogram: a graphical representation of the distribution of pixel intensity values, h(rk) = nk.

Given: 5-bit image, so L = 25 = 32 and L − 1 = 31. Total pixels N = 16.

1630810
251120
61395
1914294
input image

Step 1, histogram h(rk) = nk: count the pixels at each gray level.

rk012345678910111213141516171819202122232425262728293031
nk11001110111011101001000001000110

Check: Σ nk = 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 = 16.

0.511011141516181911011211311411611912512913031rknk
Original histogram (nk against rk)
0.030.060.0600.0610.0640.0650.0660.0680.0690.06100.06120.06130.06140.06160.06190.06250.06290.063031rkp(rk)
Normalized histogram p(rk) = nk/N

Steps 2–5. PDF p(rk) = nk/N; CDF = running sum of the PDF; multiply by (L − 1) = 31; round off (.5 goes up). Only the intensities with non-zero occurrence are listed, as the slide says.

rkStep 1: nkStep 2: PDFStep 3: CDFStep 4: (L−1)·CDFStep 5: sk
010.06250.062531 × 0.0625 = 1.93752
110.06250.125031 × 0.1250 = 3.87504
410.06250.187531 × 0.1875 = 5.81256
510.06250.250031 × 0.2500 = 7.75008
610.06250.312531 × 0.3125 = 9.687510
810.06250.375031 × 0.3750 = 11.625012
910.06250.437531 × 0.4375 = 13.562514
1010.06250.500031 × 0.5000 = 15.500016
1210.06250.562531 × 0.5625 = 17.437517
1310.06250.625031 × 0.6250 = 19.375019
1410.06250.687531 × 0.6875 = 21.312521
1610.06250.750031 × 0.7500 = 23.250023
1910.06250.812531 × 0.8125 = 25.187525
2510.06250.875031 × 0.8750 = 27.125027
2910.06250.937531 × 0.9375 = 29.062529
3010.06251.000031 × 1.0000 = 31.000031

Mapping the new gray levels into number of pixels:

gray level rknew gray level skno. of pixels nk
021
141
461
581
6101
8121
9141
10161
12171
13191
14211
16231
19251
25271
29291
30311

Equalized histogram: add the nk that land on the same sk. Levels not listed get 0 pixels.

skpixels addedn(sk)
211
411
611
811
1011
1211
1411
1611
1711
1911
2111
2311
2511
2711
2911
3111
0.51012141618110112114116117119121123125127129131sknk
Equalized histogram (n against new level sk)
048121620242831048121620242831input rkoutput sk
Transformation function sk = T(rk) drawn as a staircase (slide 40 style). Empty levels keep the previous step.

Equalized output image: replace every pixel r by its s.

1630810
251120
61395
1914294
input
23311216
274172
1019148
2521296
equalized output

Try it, step by step: F21 (5-bit), every step.

TrapFor 5-bit images most of the 32 levels are empty. Put only the non-zero intensities in the table (the slide says so), but draw the axes from 0 to 31. Use L − 1 = 31, not 7.

Homework and class work

slides 59–61On study list
Slide 59 · Home workproof

An image is histogram-equalized. Show that a second pass of histogram equalization produces exactly the same result as the first pass.

After the first pass, every input level rk has been moved to sk = round((L−1)·CDF(rk)). The order of levels is kept, since the CDF never decreases.

In the equalized image, the number of pixels at or below level sk is exactly the number of original pixels at or below rk (for the last rk that maps to sk). So the CDF of the new image at sk equals the old CDF at rk.

The second pass therefore computes round((L−1)·CDF) from the same CDF values and gets the same sk again. Each level maps to itself: the second mapping is the identity, and the image does not change.

Numeric check with the slide 36 result: equalized counts at s = 0, 2, 4, 5, 6, 7 are 1, 6, 5, 3, 3, 2. A second equalization gives new levels 0→0, 2→2, 4→4, 5→5, 6→6, 7→7, every level unchanged.

Slides 60–61 · Class worksolved

Equalize the given histogram: r = 0..7, n = [100, 90, 50, 20, 0, 0, 0, 0].

Given: 3-bit image, so L = 23 = 8 and L − 1 = 7. Total pixels N = 260.

Step 1, histogram h(rk) = nk: count the pixels at each gray level.

rk01234567
nk1009050200000

Check: Σ nk = 100 + 90 + 50 + 20 = 260.

5010010009015022034567rknk
Original histogram (nk against rk)
0.190.380.3800.3510.1920.0834567rkp(rk)
Normalized histogram p(rk) = nk/N

Steps 2–5. PDF p(rk) = nk/N; CDF = running sum of the PDF; multiply by (L − 1) = 7; round off (.5 goes up). Only the intensities with non-zero occurrence are listed, as the slide says.

rkStep 1: nkStep 2: PDFStep 3: CDFStep 4: (L−1)·CDFStep 5: sk
01000.38460.38467 × 0.3846 = 2.69233
1900.34620.73087 × 0.7308 = 5.11545
2500.19230.92317 × 0.9231 = 6.46156
3200.07691.00007 × 1.0000 = 7.00007

Mapping the new gray levels into number of pixels:

gray level rknew gray level skno. of pixels nk
03100
1590
2650
3720

Equalized histogram: add the nk that land on the same sk. Levels not listed get 0 pixels.

skpixels addedn(sk)
3100100
59090
65050
72020
5010001210034905506207sknk
Equalized histogram (n against new level sk)
0123456701234567input rkoutput sk
Transformation function sk = T(rk) drawn as a staircase (slide 40 style). Empty levels keep the previous step.

Try it, step by step: the slide 60 class work.

Histogram specification (matching)

slides 46–58On study list

Slide 46. Why another method? Histogram equalization has a disadvantage: it can produce only one type of output image. With histogram specification we can specify the shape of the histogram we want the output to have. It doesn't have to be uniform. Specification is a trial-and-error process: there are no rules for choosing the target histogram, and each enhancement task needs case-by-case analysis.

Slide 47. Example: you have a dull image and want it to look like a bright, high-contrast photo. Use the bright photo's histogram as the reference, and the result inherits its tonal style.

In plain wordsEqualization always aims at a flat histogram. Matching aims at a histogram you pick. The trick: equalize both your image and the target. Both now sit on the same flat scale, so you can line them up and see which input level should become which target level.

Slide 48. Definition: generate a processed image that has a specified histogram, i.e. convert the input image so that its histogram matches one that was arbitrarily specified. The mapping is found in three steps:

  1. Equalize the histogram of the input image, giving Hk.
  2. Equalize the specified histogram, giving Sk.
  3. Relate the two equalized histograms: map each rk to the z whose S is closest to its Hk.
Slides 49–53 · 3-bit, 64×64 (MN = 4096)worked

Get the histogram transformation function and make the output image follow the specified histogram pz = [0, 0, 0, 0.15, 0.20, 0.30, 0.20, 0.15].

Step 1, equalize the original histogram to get Hk.

rknkPDFCDF(L−1)·CDFHk
07900.190.191.331
110230.250.443.083
28500.210.654.555
36560.160.815.676
43290.080.896.236
52450.060.956.657
61220.030.986.867
7810.021.007.007

Step 2, equalize the specified histogram to get Sk.

zqpz(zq)CDF(L−1)·CDFSk
00.000.000.000
10.000.000.000
20.000.000.000
30.150.151.051
40.200.352.452
50.300.654.555
60.200.855.956
70.151.007.007

Step 3, relate the two. For each rk, take its Hk, find the S value closest to it, and map rk to that z. If two S values are equally close, take the higher one (slide 55). If several z share that S value, take the first such z.

rkHkclosest Smap to z
0113
1324
2555
3666
4666
5777
6777
7777

Output histogram: add the pixels that land on each z.

zpixels addedn(z)
0–0
1–0
2–0
3790790
410231023
5850850
6656 + 329 = 985985
7245 + 122 + 81 = 448448
511.5102379001023185026563329424551226817rknk
Original histogram
511.51023012790310234850598564487zqnk
Output (specified) histogram
0123456701234567input rkoutput zq
Transformation function rk → zq

Compare with the slide: the actual output histogram has p(z) = 0.00, 0.00, 0.00, 0.19, 0.25, 0.21, 0.24, 0.11, close to the specified one, though not exact, because levels are discrete.

Try it, step by step: all three matching steps for slide 49: equalize the input, equalize the target, then relate them level by level.

Slides 55–58 · second example (N = 64)worked

Original histogram n = [8, 10, 10, 2, 12, 16, 4, 2]. Desired histogram n = [0, 0, 0, 0, 20, 20, 16, 8]. Find the mapping and the output histogram.

Step 1, equalize the original histogram to get Hk.

rknkPDFCDF(L−1)·CDFHk
080.120.120.841
1100.160.281.962
2100.160.443.083
320.030.473.293
4120.190.664.625
5160.250.916.376
640.060.976.797
720.031.007.007

Step 2, equalize the specified histogram to get Sk.

zqspecified npz(zq)CDF(L−1)·CDFSk
000.000.000.000
100.000.000.000
200.000.000.000
300.000.000.000
4200.310.312.172
5200.310.624.344
6160.250.876.096
780.121.007.007

Step 3, relate the two. For each rk, take its Hk, find the S value closest to it, and map rk to that z. If two S values are equally close, take the higher one (slide 55). If several z share that S value, take the first such z.

rkHkclosest Smap to z
0124
1224
2345
3345
4566
5666
6777
7777

Output histogram: add the pixels that land on each z.

zpixels addedn(z)
0–0
1–0
2–0
3–0
48 + 10 = 1818
510 + 2 = 1212
612 + 16 = 2828
74 + 2 = 66
81680101102231241654627rknk
Original histogram
1428012318412528667zqnk
Output (specified) histogram
0123456701234567input rkoutput zq
Transformation function rk → zq

Try it, step by step: slide 55, including the ties that go to the higher S.

TrapIn Step 3 you compare H with S values, then write down the z position of that S, not the S value itself. Example: on slide 49, H = 1 is closest to S = 1, which sits at z = 3, so r = 0 → 3.

Theory questions, answer-script form

Must studyslides 4–17, 31–34, 62Came in past papers
F23 Q1(a)4 marks

What is the histogram of an image? Can you reconstruct the image from the histogram? How does the histogram differ for a dark, bright, low-contrast or high-contrast image? [1+1+2]

Histogram: a graphical representation of the distribution of pixel intensity values in an image. For gray levels in [0, L−1] it is the discrete function h(rk) = nk, where nk is the number of pixels with gray level rk.

Reconstruction: No. A histogram holds only statistical information (how many pixels have each value), not the location of the pixels. Different images can have the same histogram, e.g. a half-black/half-white image and a checkerboard with the same numbers of black and white pixels.

Four types [draw the four small histograms]:

dark → brightn
dark
dark → brightn
bright
dark → brightn
low contrast
dark → brightn
high contrast
  • Dark: components concentrated on the low (left) side of the gray scale.
  • Bright: concentrated on the high (right) side.
  • Low contrast: a narrow band, usually in the middle of the scale.
  • High contrast: spread over a broad range, fairly uniformly.
SP23 Q2(b) · Fall 25 Quiz (D) Q2(a)4–5 marks

What is the histogram of an image? Explain the four basic intensity characteristics of an image and their corresponding histograms.

Write the definition and h(rk) = nk as above, then the four types with their four sketches. Add the meaning of each:

  • Dark image: most pixels have low values, so the histogram sits on the left; the image looks dark and details in shadows are hidden.
  • Bright image: most pixels have high values, so the histogram sits on the right; the image looks washed out.
  • Low-contrast image: pixels occupy a narrow range of gray levels, so the histogram is narrow; the image looks dull and gray.
  • High-contrast image: pixels cover almost the whole range and the histogram is spread fairly evenly; the image shows a lot of gray-level detail.
F22 A2(b)5 marks

What is the histogram of an image? How does the histogram differ for dark, bright, low-contrast and high-contrast images of the same scene? Write the steps of the histogram equalization algorithm. [1+2+2]

Definition and four types as above. Algorithm (input: image f with L levels):

  1. Compute the histogram h(rk) = nk.
  2. Compute the PDF p(rk) = nk/N.
  3. Compute the CDF, the cumulative sum of the PDF.
  4. Compute sk = (L − 1) × CDF(rk).
  5. Round sk to get the new intensity values, and replace every rk by sk.
Fall 25 Quiz (set D) Q16 marks

How do you generate an image histogram? What does an image histogram say? [3+3]

Generate: (1) find the number of gray levels L = 2k and make one bin per level 0 … L−1; (2) scan the image and, for each pixel with value rk, add 1 to bin h(rk); (3) plot nk against rk as bars. Σ h = total number of pixels.

Example (slide 7, 3-bit 4×4): h = [1, 1, 3, 5, 1, 2, 1, 2].

2.551011325314251627rknk
h(rk) for the slide 7 image

What it says: how many pixels have each intensity, i.e. the tonal distribution. From its shape we can tell whether the image is dark, bright, low- or high-contrast; judge the overall quality and the possibility of enhancement; check whether the image was scanned properly; and choose a threshold for object detection and segmentation. It gives only statistical information and no indication of pixel locations.

Fall 25 Quiz (set C) Q16 marks

What is the histogram of an image? Two 20×20 images have equal numbers of black (0) and white (1) pixels. Are their histograms identical? Explain. [2+4]

Definition: as above, h(rk) = nk.

Each image has 20 × 20 = 400 pixels: 200 black and 200 white. So for both images h(0) = 200 and h(1) = 200.

1002002000 (black)2001 (white)gray levelnk
Histogram of either image: identical

Yes, the histograms are identical, even if one image is half black / half white and the other is a checkerboard. A histogram records only how many pixels have each value, not where they are. So the same histogram can come from different images, and an image cannot be reconstructed from its histogram.

Short note · continuous CDF (slide 62)theory

For continuous intensities the equalizing transformation is s = T(r) = (L − 1) ∫0r pr(w) dw. In the discrete case it becomes s = T(r) = (L − 1) Σ p(rj) for j = 0 … k, i.e. (L − 1) times the CDF. This is why the algorithm multiplies the CDF by L − 1.

Traps and one-minute sheet

slides all
thingformula / rule
histogramh(rk) = nk
normalizedp(rk) = nk/N, Σ = 1
slidingadd a constant: brighter or darker, same shape
stretchingg = 255(f − MIN)/(MAX − MIN), same shape
equalizationsk = round((L−1)·CDF(rk))
matchingequalize input → H, equalize target → S, map H to closest S