CSE 4227 · Chapter 8 · Deck 13

Image Compression, from zero

Every slide you need, in plain words, with every Huffman, arithmetic, LZW, RLE, Golomb and RMSE past question solved the way you write it in the answer script, and a step-by-step simulator next to each method.

★ Must study on this page

Start here: why compress, and data vs information

slides 3–7Came in past papers
In plain wordsA picture holds some information, but a file stores data (bits). The same information can be stored with many or few bits. Compression finds the bits that are repeated or not needed and drops them, so the picture takes less space or downloads faster.

Slide 3. Data compression is the process of reducing the amount of data required to represent a given quantity of information. Data and information are not the same thing: various amounts of data can represent the same information. Data that is irrelevant or repeated is called data redundancy.

Slide 4. The goal of image compression is to reduce the amount of data required to represent a digital image. It is very important for image storage and image transmission. Compression is achieved by encoding data with an appropriate encoding scheme.

Slide 5. Applications: internet, businesses, multimedia, satellite imaging, medical imaging.

Slides 6–7 · why images must be compressedworked

SD TV, 2 hours, 720×480 colour frames at 30 frames/s, 3 bytes per pixel:

One second = 30 × (720 × 480) × 3 = 31,104,000 bytes ≈ 31 MB.

Two hours = 31,104,000 × 60² × 2 ≈ 2.24 × 1011 bytes = 224 GB. That needs twenty-seven 8.5 GB dual-layer DVDs, so the video must be compressed by a factor of about 26.3 to fit on one DVD.

HD TV (1920×1080) needs even higher compression. Web images of 128×128 colour take 7.0 to 0.03 s to download over 56 kbps to 12 Mbps links. A 1 GB flash memory holds only about 41 uncompressed 8-megapixel photos.

F23 Q1(d) first part · F22 A2(a) · SP22 Q5(a)i1–2 marks

What is image compression? Why do we need it?

Image compression is the process of reducing the amount of data required to represent a digital image, by removing redundant (repeated or irrelevant) data while keeping the information.

We need it because raw images and videos are huge: for example a 2-hour SD video needs about 224 GB uncompressed, which is too big to store or send. Compression saves storage space and transmission time/bandwidth (internet, multimedia, satellite and medical imaging).

Compression ratio and relative data redundancy

Must studyslides 8–9Came in past papers

Slide 8. Data redundancy is unnecessary or repetitive data in an image file; removing it reduces the file size without losing essential information. It is a mathematically quantifiable entity. If n1 and n2 are the numbers of information-carrying units (e.g. bits) in the original and compressed data sets:

CR = n1 / n2      RD = 1 − 1/CR
caseCRRDmeaning
n1 = n210the original contains no redundant data
n1 ≫ n2→ ∞→ 1highly compressed, highly redundant data
n1 ≪ n2→ 0→ −∞undesirable: the 'compressed' file is bigger

Slide 9. A practical ratio such as CR = 10 (10:1) means the original has 10 bits for every 1 bit of the compressed data. Then RD = 1 − 1/10 = 0.9: 90% of the data in the original was redundant.

SP25 Q2(c)i1 mark

An uncompressed grayscale image is 512×512 with 8 bits/pixel. After compression the file is 64 KB. Compute the compression ratio.

n1 = 512 × 512 × 8 = 2,097,152 bits.

n2 = 64 KB = 64 × 1024 bytes × 8 = 524,288 bits.

CR = n1/n2 = 2,097,152 / 524,288 = 4, i.e. 4:1. (RD = 1 − 1/4 = 0.75: 75% of the original data was redundant.)

SP22 Q5(a)iii · SP23 Q3(a)iii2 marks

A 512×512, 8-bit image with entropy 5.3 bits/pixel is Huffman coded. What is the maximum compression that can be expected?

Entropy is the lower limit of the average code length: no lossless code can use fewer than H = 5.3 bits/pixel on average (slide 40). So the best possible Lavg is 5.3 bits/pixel.

Cmax = 8 / 5.3 = 1.509 (about 1.51:1). In bits: 512 × 512 × 8 = 2,097,152 bits against at least 512 × 512 × 5.3 ≈ 1,389,363 bits.

The three data redundancies

Must studyslides 10–23Came in past papers
In plain wordsThere are three ways an image wastes bits. Coding: every gray level gets the same 8 bits, even the very common ones. Spatial: neighbouring pixels are nearly the same, so writing each one again repeats information. Irrelevant: some detail the eye can't see anyway. Each type has its own cure.

Slide 10. Three basic redundancies can be identified and exploited. Compression is achieved when one or more of them are reduced or eliminated:

  1. Coding redundancy (inefficient bit usage): intensity arrays contain more bits than are needed.
  2. Spatial / inter-pixel redundancy (correlated neighbouring pixels): intensity arrays are correlated spatially.
  3. Psychovisual redundancy (irrelevant information): the array holds information that is ignored by the human visual system.

Coding redundancy (slides 11–16)

Slide 11. Elements of an encoding scheme: code, a list of symbols (letters, numbers, bits); code word, a sequence of symbols that represents one piece of information or gray level; word length, the number of symbols in each code word. Example: 3-bit binary code 0 = 000, 1 = 001, …, 7 = 111.

If an image uses fixed-length codes for every pixel value although some values occur far more often than others, storage is wasted.

Slide 12. Idea: variable-length coding. Assign shorter code words to the most probable pixel values and longer code words to less probable ones. The process is reversible (lossless). The histogram gives the frequency of each gray level.

Slide 14. With p(rk) = nk/n and l(rk) bits for rk:

Lavg = Σk=0L−1 l(rk) p(rk)     total bits = M N Lavg
Slides 13–15 · fixed vs variable lengthworked

A computer-generated 256×256, 8-bit image uses only four gray levels:

rkpr(rk)Code 1l1Code 2l2
870.25010101118012
1280.4710000000811
1860.251100010080003
2550.031111111180013
others0—8—0

Code 1 (fixed): 8 bits/pixel, total 256 × 256 × 8 bits.

Code 2 (variable): Lavg = 0.25×2 + 0.47×1 + 0.25×3 + 0.03×3 = 1.81 bits; total 256 × 256 × 1.81 bits.

C = 8/1.81 ≈ 4.42; RD = 1 − 1/4.42 = 0.774: 77.4% of the data in the first image was redundant.

Slide 16 · second exampleworked
rkpr(rk)Code 1l1Code 2l2
r0 = 00.190003112
r1 = 1/70.250013012
r2 = 2/70.210103102
r3 = 3/70.1601130013
r4 = 4/70.08100300014
r5 = 5/70.061013000015
r6 = 6/70.0311030000016
r7 = 7/70.0211130000006

Lavg = 2(0.19) + 2(0.25) + 2(0.21) + 3(0.16) + 4(0.08) + 5(0.06) + 6(0.03) + 6(0.02) = 2.7 bits.

CR = 3/2.7 = 1.11; RD = 1 − 1/1.11 = 0.099: 9.9% of the data was redundant.

Spatial / inter-pixel redundancy (slides 17–19)

When the value of a pixel can be obtained from its neighbouring pixels, the redundancy is called inter-pixel or spatial redundancy. Intensities of each pixel may correlate to their neighbours, so much information is unnecessarily replicated.

Slide 18. A computer-generated 256×256×8 image of horizontal lines: each line has one random gray level, so the histogram is uniform (all 256 levels equally likely). Pixels are maximally correlated horizontally but independent vertically. This image cannot be compressed with variable-length coding.

Slide 19. It can be represented as run-length pairs (intensity value, number of pixels with that intensity). Each of the 256 lines becomes one pair (value, 256):

C = (256 × 256 × 8) / ((256 + 256) × 8) = 128 : 1

Psychovisual redundancy / irrelevant information (slides 20–21)

Many images hold information that is ignored by the human visual system (HVS); try to remove this extraneous data. Slide 20: a 256×256×8 image that looks like one flat gray. Its histogram is a narrow spike, and it can be represented by a single average gray level:

C = (256 × 256 × 8) / 8 = 65536 : 1

Slide 21. However, this coarse quantization may remove useful "invisible" information: after histogram equalization the "flat" image shows a hidden pattern.

Slide 22. Summary: coding redundancy, the image does not need all 8 bits; spatial redundancy, information is unnecessarily replicated; irrelevant information, information that is not useful.

Compression types (slide 23)

typeremovesmethods
Error-free (lossless)coding redundancyvariable-length coding: Huffman, Golomb, arithmetic
Error-free (lossless)spatial / inter-pixel redundancyLZW coding, run-length coding
Lossyirrelevant (psychovisual) informationinformation is lost; quality judged by fidelity criteria
SP25 Q1(d)4 marks

What is data redundancy in images? Name its three main types. Discuss the redundancy type that can be overcome using variable-length code words. How do you calculate relative data redundancy?

Data redundancy is unnecessary or repeated data in an image file which, when removed, reduces the file size without losing essential information. It is a mathematically quantifiable entity.

Three types: (1) coding redundancy, (2) spatial / inter-pixel redundancy, (3) psychovisual redundancy (irrelevant information).

Coding redundancy is overcome with variable-length codes. A fixed-length code gives every gray level the same number of bits (e.g. 8), although some levels occur much more often. Variable-length coding (e.g. Huffman) gives shorter code words to more probable gray levels and longer ones to rare levels, so the average length Lavg = Σ l(rk) p(rk) drops below 8. Example (slide 15): Lavg = 1.81 bits instead of 8. This is reversible, so lossless.

Relative data redundancy: with n1 and n2 the bits in the original and compressed data, CR = n1/n2 and RD = 1 − 1/CR. Example: CR = 8/1.81 = 4.42 gives RD = 0.774, so 77.4% of the data was redundant.

F23 Q1(d) · SP23 Q2(a) · SP22 Q5(a)ii · F22 A2(a)3–5 marks

How many types of data redundancy are there in an image? Describe each briefly. How can compression be implemented? How do you calculate the percentage of redundant data after compression?

There are three types of data redundancy:

  1. Coding redundancy: the code used for the gray levels has more bits than needed, e.g. every pixel stored in 8 bits although a few values dominate. Removed by variable-length coding (Huffman, Golomb, arithmetic): short codes for frequent values.
  2. Spatial (inter-pixel) redundancy: neighbouring pixels are correlated, so a pixel's value can be predicted from its neighbours and the same information is replicated. Removed by run-length coding or LZW. Example: an image of constant horizontal lines compresses 128:1 with run-length pairs.
  3. Psychovisual (irrelevant) redundancy: information the human visual system ignores. Removed by quantization (lossy). Example: a near-flat gray image stored as one average gray level, 65536:1.

Implementation: compression is achieved when one or more of these redundancies are reduced or eliminated, by encoding the data with a suitable scheme (compress → store/transmit → decompress).

Percentage redundant: CR = n1/n2, RD = 1 − 1/CR, and the percentage of redundant data = RD × 100%. Example: CR = 10 → RD = 0.9 → 90% redundant.

Huffman coding

Must studyslides 24–38Came in past papers
In plain wordsHuffman coding builds the best variable-length code by repeatedly merging the two rarest symbols. Rare symbols get merged many times, so they end up deep in the tree with long codes. Common symbols are merged late and get short codes.

Slide 24. Huffman coding is a lossless compression algorithm, the most popular technique for removing coding redundancy. It is variable-length: most frequent symbols get shorter codes, rare symbols longer codes. Widely used in JPEG. The resulting code is optimal.

Slide 25. Steps:

  1. Find the gray-level probabilities from the image histogram.
  2. Order the input probabilities from largest to smallest.
  3. Combine the smallest two probabilities by addition.
  4. Go to step 2 until only two probabilities are left.
  5. Working backward along the tree, generate the code by alternating assignment of 0 and 1.
Slides 26–34 · 3-bit image, six gray levelsworked

Probabilities: a1 = 0.1, a2 = 0.4, a3 = 0.06, a4 = 0.1, a5 = 0.04, a6 = 0.3. Find the Huffman code.

Step 1, sort the probabilities largest first. Steps 2–4, add the two smallest and re-sort, until two values are left (a sum that ties with an existing value goes below it). Step 5, walk back from the last column: upper branch 0, lower branch 1.

SymbolProbabilityCodeReduction 1Reduction 2Reduction 3Reduction 4
a20.410.4  10.4  10.4  10.6  0
a60.3000.3  000.3  000.3  000.4  1
a10.10110.1  0110.2  0100.3  01
a40.101000.1  01000.1  011
a30.06010100.1  0101
a50.0401011
SymbolProbability pCodeLength lp × l
a20.4110.4
a60.30020.6
a10.101130.3
a40.1010040.4
a30.060101050.3
a50.040101150.2

Average length Lavg = Σ l·p = (0.4)(1) + (0.3)(2) + (0.1)(3) + (0.1)(4) + (0.06)(5) + (0.04)(5) = 2.2 bits/symbol.

Entropy H = −Σ p log2 p = −(0.4)log2(0.4) + −(0.3)log2(0.3) + −(0.1)log2(0.1) + −(0.1)log2(0.1) + −(0.06)log2(0.06) + −(0.04)log2(0.04) = 2.1435 bits/symbol.

Coding efficiency η = H / Lavg = 2.1435 / 2.2 = 97.43%.

Compression ratio CR = n1/n2 = 3 / 2.2 = 1.3636. Relative redundancy RD = 1 − 1/CR = 1 − 1/1.3636 = 0.2667 → 26.67% of the data was redundant.

As on slide 34: n1 = 3, n2 = 2.2, C = 3/2.2 = 1.364, RD = 0.267. 26.7% fewer bits: coding redundancy eliminated. Yes, it reduces the total bit count.

Try it, step by step: every sort, merge and re-sort, then the codes passed back column by column (slides 26–33).

Slide 35. Short-cut: draw the tree directly: join the two smallest, label the branches 0 (upper) and 1 (lower), keep joining, then read each code from the root down.

Slide 36 · Class worksolved

A 3-bit grayscale image has A (dark gray) 0.45, B (medium gray) 0.13, C (light gray) 0.12, D (white) 0.16, E (black) 0.09, F (very light gray) 0.05. Find the Huffman code.

Step 1, sort the probabilities largest first. Steps 2–4, add the two smallest and re-sort, until two values are left (a sum that ties with an existing value goes below it). Step 5, walk back from the last column: upper branch 0, lower branch 1.

SymbolProbabilityCodeReduction 1Reduction 2Reduction 3Reduction 4
A0.4510.45  10.45  10.45  10.55  0
D0.160000.16  0000.25  010.3  000.45  1
B0.130100.14  0010.16  0000.25  01
C0.120110.13  0100.14  001
E0.0900100.12  011
F0.050011
SymbolProbability pCodeLength lp × l
A0.45110.45
B0.1301030.39
C0.1201130.36
D0.1600030.48
E0.09001040.36
F0.05001140.2

Average length Lavg = Σ l·p = (0.45)(1) + (0.13)(3) + (0.12)(3) + (0.16)(3) + (0.09)(4) + (0.05)(4) = 2.24 bits/symbol.

Entropy H = −Σ p log2 p = −(0.45)log2(0.45) + −(0.13)log2(0.13) + −(0.12)log2(0.12) + −(0.16)log2(0.16) + −(0.09)log2(0.09) + −(0.05)log2(0.05) = 2.2199 bits/symbol.

Coding efficiency η = H / Lavg = 2.2199 / 2.24 = 99.10%.

Compression ratio CR = n1/n2 = 3 / 2.24 = 1.3393. Relative redundancy RD = 1 − 1/CR = 1 − 1/1.3393 = 0.2533 → 25.33% of the data was redundant.

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

Slide 37. Applications: file compression (ZIP, GZIP), image compression (PNG), multimedia (part of MP3 and JPEG), data transmission (efficient coding for bandwidth-limited channels).

Slide 38. Is Huffman lossless? Yes. Every code word is unique and no code word is the prefix of another (prefix code), so the decoder reads the bits and recovers every symbol exactly. Nothing is thrown away; only the coding redundancy is removed.

TrapTie rule: when the new sum equals an existing value, put the sum below the equal value (it is what the slide 26–33 table does). A different tie order can give different codes with the same Lavg; the examiner accepts either if your table is consistent, but follow the slide to be safe.

Entropy: the minimum number of bits

Must studyslides 39–45Came in past papers
In plain wordsEntropy measures how unpredictable the gray levels are. If every pixel is the same, there is nothing to learn: 0 bits. If all 8 levels are equally likely, you need the full 3 bits. Entropy is the lowest average number of bits per pixel any lossless code can reach.

Slide 39. Information theory. A random event E with probability P(E) carries

I(E) = log 1/P(E) = −log P(E)

units of information. Higher uncertainty means higher information content. For a source of events a1 … aJ with probabilities P(aj), the average information per event is the entropy:

H = −Σj=1J P(aj) log P(aj)

Slide 40. Treating pixel intensities as random events, the normalized histogram approximates the probabilities, and the image entropy is H̃ = −Σ Pr(rk) log2 Pr(rk) bits/pixel. Entropy is the measure of the average information in an image, and it is not possible to encode any image with fewer than H bits/pixel (losslessly).

Slide 41 · entropy of the 4-level imageworked

H = −[0.25 log2 0.25 + 0.47 log2 0.47 + 0.25 log2 0.25 + 0.03 log2 0.03]

≈ −[0.25(−2) + 0.47(−1.09) + 0.25(−2) + 0.03(−5.06)] ≈ 1.6614 bits/pixel.

Compare: the variable-length code used 1.81 bits, close to this limit.

Slides 42–43. The star image has H = 1.6614, the random-lines image H = 8, the flat-looking image H = 1.566. Entropy (information) and visual impression are far from intuitive.

Slides 44–45 · two extreme casesworked

Uniform distribution, 3-bit image (L = 8), every level has p = 1/8: H = −Σ (1/8) log2(1/8) = 3 bits/pixel. The natural 3-bit code is already optimal; a uniform distribution has the highest entropy.

Certain event, every pixel is 2: p(2) = 1, so H = −(1) log2(1) = 0 bits/pixel. The gray level is known in advance, so one value codes the whole image.

Huffman past questions, solved in full

Must studyslides 24–45Came in past papers

Recipe for every image question: count each gray level → probability = count/N → Huffman table → code table → Lavg → entropy → CR = (original bits)/Lavg → RD. Lossless? Always yes for Huffman.

SP25 Q2(c) ii–iv1 + 2 + 4 marks

(ii) Encode the 1-D run AAAAABBBCCDAA with RLE (symbol, count). (iii) A row of 100 pixels alternates ABAB… (50 A, 50 B). Explain how RLE performs and compute the encoded length, the count stored as one byte. (iv) Symbols A: 0.4, B: 0.3, C: 0.2, D: 0.1. Build a Huffman code, compute the average code length and compare it with the entropy.

(ii) Group equal neighbours: AAAAA | BBB | CC | D | AA → (A,5) (B,3) (C,2) (D,1) (A,2). 13 symbols become 5 pairs.

(iii) Every run has length 1, so the row becomes 100 pairs (A,1)(B,1)(A,1)… With 1 byte for the symbol and 1 byte for the count, encoded length = 100 × 2 = 200 bytes against 100 bytes originally (one byte per pixel). CR = 100/200 = 0.5: the data doubles. RLE performs badly here: it only helps when there are long runs; small runs result in expansion instead of compression (slide 109).

(iv)

Step 1, sort the probabilities largest first. Steps 2–4, add the two smallest and re-sort, until two values are left (a sum that ties with an existing value goes below it). Step 5, walk back from the last column: upper branch 0, lower branch 1.

SymbolProbabilityCodeReduction 1Reduction 2
A0.410.4  10.6  0
B0.3000.3  000.4  1
C0.20100.3  01
D0.1011
SymbolProbability pCodeLength lp × l
A0.4110.4
B0.30020.6
C0.201030.6
D0.101130.3

Average length Lavg = Σ l·p = (0.4)(1) + (0.3)(2) + (0.2)(3) + (0.1)(3) = 1.9 bits/symbol.

Entropy H = −Σ p log2 p = −(0.4)log2(0.4) + −(0.3)log2(0.3) + −(0.2)log2(0.2) + −(0.1)log2(0.1) = 1.8464 bits/symbol.

Coding efficiency η = H / Lavg = 1.8464 / 1.9 = 97.18%.

Lavg = 1.9 is just above H = 1.8464: Huffman is within 0.0536 bits of the theoretical minimum (efficiency 97.18%). A fixed-length code would need 2 bits.

Try it, step by step: SP25 (iv).

Try it, step by step: SP25 (ii): the runs found one by one.

SP24 Q2(c)1 + 3 + 2 + 3 marks

5×5, 8-bit image. (i) Table of frequencies and histogram (ii) minimum number of bits per symbol (iii) Huffman code for each symbol (iv) average number of bits per symbol.

180160160140120
110110120140120
110140120120140
120160160170170
170120110140110
image

(i) N = 25.

Symbol (gray level)CountProbability = count/N
12077/25 = 0.28
11055/25 = 0.2
14055/25 = 0.2
16044/25 = 0.16
17033/25 = 0.12
18011/25 = 0.04
3.57511071205140416031701180gray levelnk
Histogram (count per gray level)

(ii) 6 different gray levels appear, so a fixed-length code needs ⌈log2 6⌉ = 3 bits per symbol. The theoretical minimum average (entropy) is H = −Σ p log2 p = 2.4188 bits/symbol; no lossless code can do better.

(iii)–(iv)

Step 1, sort the probabilities largest first. Steps 2–4, add the two smallest and re-sort, until two values are left (a sum that ties with an existing value goes below it). Step 5, walk back from the last column: upper branch 0, lower branch 1.

SymbolProbabilityCodeReduction 1Reduction 2Reduction 3Reduction 4
1200.28010.28  010.32  000.4  10.6  0
1100.2100.2  100.28  010.32  000.4  1
1400.2110.2  110.2  100.28  01
1600.160000.16  0000.2  11
1700.1200100.16  001
1800.040011
SymbolProbability pCodeLength lp × l
1200.280120.56
1100.21020.4
1400.21120.4
1600.1600030.48
1700.12001040.48
1800.04001140.16

Average length Lavg = Σ l·p = (0.28)(2) + (0.2)(2) + (0.2)(2) + (0.16)(3) + (0.12)(4) + (0.04)(4) = 2.48 bits/symbol.

Entropy H = −Σ p log2 p = −(0.28)log2(0.28) + −(0.2)log2(0.2) + −(0.2)log2(0.2) + −(0.16)log2(0.16) + −(0.12)log2(0.12) + −(0.04)log2(0.04) = 2.4188 bits/symbol.

Coding efficiency η = H / Lavg = 2.4188 / 2.48 = 97.53%.

Compression ratio CR = n1/n2 = 8 / 2.48 = 3.2258. Relative redundancy RD = 1 − 1/CR = 1 − 1/3.2258 = 0.6900 → 69.00% of the data was redundant.

Try it, step by step: SP24, built from the counts (probabilities shown as count/25).

SP23 Q3(a)3 + 3 + 2 marks

4×5, 6-bit image, every row [3, 10, 3, 21, 34]. (i) Compress with Huffman coding and calculate the compression ratio. (ii) How much data was redundant? Was any data lost? (iii) A 512×512 8-bit image with entropy 5.3 bits/pixel is Huffman coded: maximum compression expected?

(i) N = 20.

Symbol (gray level)CountProbability = count/N
388/20 = 0.4
1044/20 = 0.2
2144/20 = 0.2
3444/20 = 0.2

Step 1, sort the probabilities largest first. Steps 2–4, add the two smallest and re-sort, until two values are left (a sum that ties with an existing value goes below it). Step 5, walk back from the last column: upper branch 0, lower branch 1.

SymbolProbabilityCodeReduction 1Reduction 2
30.410.4  10.6  0
100.2010.4  000.4  1
210.20000.2  01
340.2001
SymbolProbability pCodeLength lp × l
30.4110.4
100.20120.4
210.200030.6
340.200130.6

Average length Lavg = Σ l·p = (0.4)(1) + (0.2)(2) + (0.2)(3) + (0.2)(3) = 2 bits/symbol.

Entropy H = −Σ p log2 p = −(0.4)log2(0.4) + −(0.2)log2(0.2) + −(0.2)log2(0.2) + −(0.2)log2(0.2) = 1.9219 bits/symbol.

Coding efficiency η = H / Lavg = 1.9219 / 2 = 96.10%.

Compression ratio CR = n1/n2 = 6 / 2 = 3.0000. Relative redundancy RD = 1 − 1/CR = 1 − 1/3.0000 = 0.6667 → 66.67% of the data was redundant.

Original = 20 × 6 = 120 bits; Huffman = 20 × 2 = 40 bits; CR = 3.0000.

(ii) RD = 1 − 1/3 = 0.6667: 66.7% of the data was redundant. No data was lost: Huffman coding is lossless (a prefix code, decoded exactly).

(iii) Cmax = 8/5.3 = 1.509 (see above).

Try it, step by step: SP23 (i).

F22 A3(a)3 + 3 + 2 marks

4×5, 6-bit image. (i) Compress using Huffman coding (ii) compress using run-length coding (iii) compression ratio of (i) and (ii).

31032435
31010243
310102435
310102435
image

(i)

Symbol (gray level)CountProbability = count/N
1077/20 = 0.35
366/20 = 0.3
2444/20 = 0.2
3533/20 = 0.15

Step 1, sort the probabilities largest first. Steps 2–4, add the two smallest and re-sort, until two values are left (a sum that ties with an existing value goes below it). Step 5, walk back from the last column: upper branch 0, lower branch 1.

SymbolProbabilityCodeReduction 1Reduction 2
100.3510.35  10.65  0
30.3010.35  000.35  1
240.20000.3  01
350.15001
SymbolProbability pCodeLength lp × l
100.35110.35
30.30120.6
240.200030.6
350.1500130.45

Average length Lavg = Σ l·p = (0.35)(1) + (0.3)(2) + (0.2)(3) + (0.15)(3) = 2 bits/symbol.

Entropy H = −Σ p log2 p = −(0.35)log2(0.35) + −(0.3)log2(0.3) + −(0.2)log2(0.2) + −(0.15)log2(0.15) = 1.9261 bits/symbol.

Coding efficiency η = H / Lavg = 1.9261 / 2 = 96.31%.

Compression ratio CR = n1/n2 = 6 / 2 = 3.0000. Relative redundancy RD = 1 − 1/CR = 1 − 1/3.0000 = 0.6667 → 66.67% of the data was redundant.

(ii) Row by row (gray level, run length), each number stored in 6 bits:

Row(gray level, run length) pairs
Row 1(3,1) (10,1) (3,1) (24,1) (35,1)
Row 2(3,1) (10,2) (24,1) (3,1)
Row 3(3,1) (10,2) (24,1) (35,1)
Row 4(3,1) (10,2) (24,1) (35,1)

17 pairs = 34 numbers, each stored in 6 bits. Original = 4×5×6 = 120 bits. RLE = 34×6 = 204 bits. CR = 120/204 = 0.5882.

(iii) Huffman: CR = 6/2 = 3.0000. RLE: CR = 0.5882, less than 1, so RLE expands this image (the runs are too short). Huffman is far better here.

Try it, step by step: F22 (i).

Try it, step by step: F22 (ii): every run, row by row.

SP22 Q5(b)3 + 2 + 1 + 2 marks

3×3, 8-bit image [18 16 16; 11 11 14; 11 14 16]. (i) Illustrate Huffman encoding (ii) entropy (iii) which redundancy is reduced; is it lossless? (iv) Is compression achieved? Prove it.

(i)–(ii) N = 9.

Symbol (gray level)CountProbability = count/N
1133/9 = 0.3333
1633/9 = 0.3333
1422/9 = 0.2222
1811/9 = 0.1111

Step 1, sort the probabilities largest first. Steps 2–4, add the two smallest and re-sort, until two values are left (a sum that ties with an existing value goes below it). Step 5, walk back from the last column: upper branch 0, lower branch 1.

SymbolProbabilityCodeReduction 1Reduction 2
110.333310.3333  10.6667  0
160.3333000.3333  000.3333  1
140.22220100.3333  01
180.1111011
SymbolProbability pCodeLength lp × l
110.3333110.3333
160.33330020.6667
140.222201030.6667
180.111101130.3333

Average length Lavg = Σ l·p = (0.3333)(1) + (0.3333)(2) + (0.2222)(3) + (0.1111)(3) = 2 bits/symbol.

Entropy H = −Σ p log2 p = −(0.3333)log2(0.3333) + −(0.3333)log2(0.3333) + −(0.2222)log2(0.2222) + −(0.1111)log2(0.1111) = 1.8911 bits/symbol.

Coding efficiency η = H / Lavg = 1.8911 / 2 = 94.55%.

Compression ratio CR = n1/n2 = 8 / 2 = 4.0000. Relative redundancy RD = 1 − 1/CR = 1 − 1/4.0000 = 0.7500 → 75.00% of the data was redundant.

(iii) Huffman reduces coding redundancy. It is lossless: each code word is unique and prefix-free, so the image is recovered exactly.

(iv) Original = 9 × 8 = 72 bits. Huffman = Σ count × length = 18 bits. CR = 72/18 = 4.00 > 1, so yes, compression is achieved.

Try it, step by step: SP22 Q5(b).

F21 B2(a)2 + 1 + 2 + 2 + 1 marks

5×5, 8-bit image. (i) Derive the entropy equation from information theory (ii) entropy (iii) Huffman codes (iv) compression ratio against 8-bit fixed length (v) relative data redundancy.

(i) An event E with probability P(E) carries I(E) = log2(1/P(E)) = −log2P(E) bits of information: the rarer the event, the more information. For a source with symbols a1 … aJ, symbol aj occurs with probability P(aj) and carries −log2P(aj) bits, so the average information per symbol is the weighted sum

H = Σ P(aj) I(aj) = −Σj=1J P(aj) log2 P(aj)   bits/symbol

For an image, P(aj) is the normalized histogram p(rk) = nk/N.

(ii)–(v) N = 25.

Symbol (gray level)CountProbability = count/N
1277/25 = 0.28
1155/25 = 0.2
1455/25 = 0.2
1644/25 = 0.16
1733/25 = 0.12
1811/25 = 0.04

Step 1, sort the probabilities largest first. Steps 2–4, add the two smallest and re-sort, until two values are left (a sum that ties with an existing value goes below it). Step 5, walk back from the last column: upper branch 0, lower branch 1.

SymbolProbabilityCodeReduction 1Reduction 2Reduction 3Reduction 4
120.28010.28  010.32  000.4  10.6  0
110.2100.2  100.28  010.32  000.4  1
140.2110.2  110.2  100.28  01
160.160000.16  0000.2  11
170.1200100.16  001
180.040011
SymbolProbability pCodeLength lp × l
120.280120.56
110.21020.4
140.21120.4
160.1600030.48
170.12001040.48
180.04001140.16

Average length Lavg = Σ l·p = (0.28)(2) + (0.2)(2) + (0.2)(2) + (0.16)(3) + (0.12)(4) + (0.04)(4) = 2.48 bits/symbol.

Entropy H = −Σ p log2 p = −(0.28)log2(0.28) + −(0.2)log2(0.2) + −(0.2)log2(0.2) + −(0.16)log2(0.16) + −(0.12)log2(0.12) + −(0.04)log2(0.04) = 2.4188 bits/symbol.

Coding efficiency η = H / Lavg = 2.4188 / 2.48 = 97.53%.

Compression ratio CR = n1/n2 = 8 / 2.48 = 3.2258. Relative redundancy RD = 1 − 1/CR = 1 − 1/3.2258 = 0.6900 → 69.00% of the data was redundant.

Total bits: Huffman = 62 bits; fixed = 25 × 8 = 200 bits.

Try it, step by step: F21 (iii)–(v).

Golomb coding

Must studyslides 46–51Came in past papers
In plain wordsGolomb coding is a quick code for non-negative integers where small numbers are the most common. Split n by a chosen divisor m into a quotient and a remainder. Write the quotient in unary (that many 1s, then a 0) and the remainder in a short binary form.

Slide 46. A lossless method, particularly effective when small values occur much more frequently than large ones. It can only represent non-negative integers, is computationally simpler than Huffman, and depends on a tunable parameter m that decides how numbers are split into quotient and remainder.

Slides 47–48. The Golomb code of n with respect to m, Gm(n), combines the unary code of the quotient ⌊n/m⌋ and the binary representation of the remainder n mod m:

  1. Step 1. Form the unary code of the quotient q = ⌊n/m⌋ (q ones followed by a 0; e.g. q = 3 → 1110).
  2. Step 2. Let k = ⌈log2 m⌉, c = 2k − m, r = n mod m, and compute the truncated remainder r′: r truncated to k − 1 bits if 0 ≤ r < c; otherwise r + c truncated to k bits.
  3. Step 3. Concatenate the results of steps 1 and 2.
SP24 Q3(b)ii · F23 Q2(c)ii · F21 B2(b)ii · slide 493–4 marks

Compute the Golomb code G4(9).

Step 1: q = ⌊9/4⌋ = 2 → unary code 110.

Step 2: k = ⌈log2 4⌉ = 2; c = 22 − 4 = 0; r = 9 mod 4 = 1. r = 1 ≥ c = 0 → r + c = 1 in k = 2 bits = 01.

Step 3: concatenate: 110 + 01 = G4(9) = 11001.

Try it, step by step: any n and m. Pick n and m from the menus; it opens on G4(9).

Slide 50 · G4(7) and G5(12)worked

G4(7): q = 1 → 10; k = 2, c = 0, r = 3 ≥ 0 → r + c = 3 in 2 bits = 11 → 1011.

G5(12): q = ⌊12/5⌋ = 2 → 110; k = ⌈log2 5⌉ = 3, c = 8 − 5 = 3, r = 12 mod 5 = 2. r = 2 < c = 3 → r in k − 1 = 2 bits = 10. So G5(12) = 110 + 10 = 11010.

TrapSlide 50 prints G5(12) = 110010, which writes r = 2 in 3 bits (010) without truncating. Its own step 2 says r < c, so r goes in k − 1 = 2 bits: 10, giving 11010. The textbook rule gives 11010. If this comes up, show the step-2 working and write 11010; mention 110010 only as the slide's value.
Slide 51 · "Fill the chart at home"solved
nG1(n)G2(n)G4(n)
0000000
11001001
2110100010
31110101011
41111011001000
511111011011001
61111110111001010
711111110111011011

m = 1: k = 0, so the code is just the unary code of n. m = 2: k = 1, c = 0, the remainder is 1 bit. m = 4: k = 2, c = 0, the remainder is 2 bits. Row 7 matches the slide (11111110, 11101, 1011).

Arithmetic coding

slides 52–74On study list
In plain wordsArithmetic coding turns a whole message into one number between 0 and 1. Start with the interval [0, 1). Each symbol zooms into its own slice of the current interval, sized by its probability. After the last symbol, any number inside the final tiny interval stands for the whole message.

Slide 52. Variable-length, error-free (lossless), non-block code: there is no one-to-one correspondence between symbols and code words. An entire sequence of source symbols is mapped to a single arithmetic number (code word), which defines an interval of real numbers between 0 and 1. It can achieve theoretically higher compression than Huffman.

Slide 53. It maintains an interval within [0, 1) based on the symbol probabilities.

Slides 53–64 · encode a1 a2 a3 a3 a4worked

Four-symbol source a1 = 0.2, a2 = 0.2, a3 = 0.4, a4 = 0.2. Encode the message a1 a2 a3 a3 a4.

SymbolProbabilityInitial sub-interval
a10.2[0, 0.2)
a20.2[0.2, 0.4)
a30.4[0.4, 0.8)
a40.2[0.8, 1)

For each symbol: width = high − low; new low = low + width × (symbol's low end); new high = low + width × (symbol's high end).

SymbolCurrent intervalWidthNew lowNew high
a1[0, 1)10 + 1×0 = 00 + 1×0.2 = 0.2
a2[0, 0.2)0.20 + 0.2×0.2 = 0.040 + 0.2×0.4 = 0.08
a3[0.04, 0.08)0.040.04 + 0.04×0.4 = 0.0560.04 + 0.04×0.8 = 0.072
a3[0.056, 0.072)0.0160.056 + 0.016×0.4 = 0.06240.056 + 0.016×0.8 = 0.0688
a4[0.0624, 0.0688)0.00640.0624 + 0.0064×0.8 = 0.067520.0624 + 0.0064×1 = 0.0688

Final interval: [0.06752, 0.0688). Any number in it encodes the message. Shortest decimal: 0.068.

The slide picks 0.068: the arithmetic code for a1a2a3a3a4.

Try it, step by step: slides 55–63: watch each symbol zoom into its slice.

Decoding (slides 65–73)

The decoder knows the same sub-intervals. Take the code 0.068: it lies in [0, 0.2), so the first symbol is a1. Inside [0, 0.2) the slices are a1 [0, 0.04), a2 [0.04, 0.08), a3 [0.08, 0.16), a4 [0.16, 0.2): 0.068 lies in a2. Keep zooming: [0.04, 0.08) → a3 slice [0.056, 0.072); then a3 again [0.0624, 0.0688); then a4. Decoded: a1 a2 a3 a3 a4. (The decoder also needs the message length or an end symbol to know when to stop.)

Slide 74 · Class worksolved

Six-symbol source A 0.45, B 0.13, C 0.12, D 0.16, E 0.09, F 0.05 with sub-intervals A [0, 0.45), B [0.45, 0.58), C [0.58, 0.70), D [0.70, 0.86), E [0.86, 0.95), F [0.95, 1.00). Encode the message "ACEF". (The Huffman code of the same source is the slide 36 class work above.)

SymbolProbabilityInitial sub-interval
A0.45[0, 0.45)
B0.13[0.45, 0.58)
C0.12[0.58, 0.7)
D0.16[0.7, 0.86)
E0.09[0.86, 0.95)
F0.05[0.95, 1)

For each symbol: width = high − low; new low = low + width × (symbol's low end); new high = low + width × (symbol's high end).

SymbolCurrent intervalWidthNew lowNew high
A[0, 1)10 + 1×0 = 00 + 1×0.45 = 0.45
C[0, 0.45)0.450 + 0.45×0.58 = 0.2610 + 0.45×0.7 = 0.315
E[0.261, 0.315)0.0540.261 + 0.054×0.86 = 0.307440.261 + 0.054×0.95 = 0.3123
F[0.30744, 0.3123)0.004860.30744 + 0.00486×0.95 = 0.3120570.30744 + 0.00486×1 = 0.3123

Final interval: [0.312057, 0.3123). Any number in it encodes the message. Shortest decimal: 0.3121.

Try it, step by step: the slide 74 class work 'ACEF'.

TrapUse the sub-intervals in the order the question lists the symbols, not sorted by probability. Keep every decimal (no rounding) until the final interval, then pick the shortest decimal that lies inside it.

LZW coding

Must studyslides 75–106Came in past papers
In plain wordsLZW builds a dictionary while it reads. The first time it meets a pattern such as 39-39, it adds it to the dictionary. The next time 39-39 appears, one dictionary code replaces both pixels. Repeated patterns therefore shrink, without needing any probabilities.

Slide 75. Key features: lossless (no data lost); dictionary-based (builds a dictionary of sequences met in the input); a universal algorithm (text, images, other data); efficient: it removes spatial / inter-pixel redundancy, is simple to implement with high throughput, and needs no knowledge of the probability of occurrence. Used in GIF, TIFF and PDF.

Slide 76. Encoding: images are scanned left to right and top to bottom. The codebook (dictionary) is extended with new sequences met in the input, each assigned a fixed-length code word. For 8-bit monochrome images the first 256 words are the intensities 0, 1, …, 255; new gray-level sequences get entries from #256. With a 9-bit, 512-word dictionary, two pixels (16 bits) can be represented by 9 bits only.

Slide 77. Read input symbols and form longer sequences. If a sequence is already in the dictionary, continue extending it. If not, add the new sequence to the dictionary and output the code for the longest existing sequence.

Slides 77–95 · 4×4, 8-bit image of 39 39 126 126 rowsworked
3939126126
3939126126
3939126126
3939126126
image

Dictionary locations 0–255 already hold the 256 single gray levels; new sequences go from location 256. Read the pixels left to right, top to bottom. For each pixel: if currently recognized sequence + pixel is in the dictionary, keep growing it; otherwise output the code of the recognized sequence, add the longer sequence to the dictionary, and restart from the pixel.

Currently recognized sequencePixel being processedEncoded outputDictionary location (code word)Dictionary entry
39
39393925639-39
391263925739-126
126126126258126-126
12639126259126-39
3939
39-3912625626039-39-126
126126
126-12639258261126-126-39
3939
39-39126
39-39-12612626026239-39-126-126
12639
126-3939259263126-39-39
39126
39-12612625726439-126-126
126126

Encoded output: 39 39 126 126 256 258 260 259 257 126 (10 code words).

Original = 16 pixels × 8 bits = 128 bits. Compressed = 10 codes × 9 bits = 90 bits. CR = 128/90 = 1.4222 (compression achieved).

Try it, step by step: the slide example, one pixel at a time (slides 78–95).

Slide 106. Compression ratio: the sequence 39-39-126 is replaced by code 260, i.e. 3 × 8 = 24 bits by 9 bits. Original 16 × 8 = 128 bits, compressed 10 × 9 = 90 bits, CR = 128 : 90 = 1.42 : 1.

Decoding (slides 96–105)

The decoder rebuilds the same dictionary while it reads the codes. Each code is translated back into its sequence; after each code (from the second on) the entry previous-sequence + first pixel of the current sequence is added.

code readoutput pixelsdictionary entry added
3939—
3939256: 39-39
126126257: 39-126
126126258: 126-126
25639 39259: 126-39
258126 126260: 39-39-126
26039 39 126261: 126-126-39
259126 39262: 39-39-126-126
25739 126263: 126-39-39
126126264: 39-126-126

Output: 39 39 126 126 / 39 39 126 126 / 39 39 126 126 / 39 39 126 126, the original image.

SP25 Q3(b)i6 marks

A collection of bitmap images with many repeating colours and patterns must be compressed with a lossless fixed-length coding algorithm. Name it and explain step-by-step encoding of the 4×4 8-bit image with every row [7 8 40 100].

Use LZW coding: lossless, fixed-length (9-bit) code words, and it removes the repeated patterns (spatial redundancy) without needing probabilities.

Dictionary locations 0–255 already hold the 256 single gray levels; new sequences go from location 256. Read the pixels left to right, top to bottom. For each pixel: if currently recognized sequence + pixel is in the dictionary, keep growing it; otherwise output the code of the recognized sequence, add the longer sequence to the dictionary, and restart from the pixel.

Currently recognized sequencePixel being processedEncoded outputDictionary location (code word)Dictionary entry
7
7872567-8
84082578-40
401004025840-100
1007100259100-7
78
7-8402562607-8-40
40100
40-100725826140-100-7
78
7-840
7-8-401002602627-8-40-100
1007
100-78259263100-7-8
840
8-401002572648-40-100
100100

Encoded output: 7 8 40 100 256 258 260 259 257 100 (10 code words).

Original = 16 pixels × 8 bits = 128 bits. Compressed = 10 codes × 9 bits = 90 bits. CR = 128/90 = 1.4222 (compression achieved).

Try it, step by step: SP25.

F23 Q3(c)10 marks

Encode the 4×4 8-bit image with every row [5 8 40 120] using LZW, step by step; the codebook uses 9 bits.

Dictionary locations 0–255 already hold the 256 single gray levels; new sequences go from location 256. Read the pixels left to right, top to bottom. For each pixel: if currently recognized sequence + pixel is in the dictionary, keep growing it; otherwise output the code of the recognized sequence, add the longer sequence to the dictionary, and restart from the pixel.

Currently recognized sequencePixel being processedEncoded outputDictionary location (code word)Dictionary entry
5
5852565-8
84082578-40
401204025840-120
1205120259120-5
58
5-8402562605-8-40
40120
40-120525826140-120-5
58
5-840
5-8-401202602625-8-40-120
1205
120-58259263120-5-8
840
8-401202572648-40-120
120120

Encoded output: 5 8 40 120 256 258 260 259 257 120 (10 code words).

Original = 16 pixels × 8 bits = 128 bits. Compressed = 10 codes × 9 bits = 90 bits. CR = 128/90 = 1.4222 (compression achieved).

Try it, step by step: F23.

SP22 Q1(b)5 + 2 + 2 marks

3×3 8-bit image with every row [3 4 7]. (i) Generate a 9-bit codebook and LZW-encode it step by step. (ii) Which redundancy does it reduce? (iii) Is any compression achieved? Prove it.

(i)

Dictionary locations 0–255 already hold the 256 single gray levels; new sequences go from location 256. Read the pixels left to right, top to bottom. For each pixel: if currently recognized sequence + pixel is in the dictionary, keep growing it; otherwise output the code of the recognized sequence, add the longer sequence to the dictionary, and restart from the pixel.

Currently recognized sequencePixel being processedEncoded outputDictionary location (code word)Dictionary entry
3
3432563-4
4742574-7
7372587-3
34
3-472562593-4-7
73
7-342582607-3-4
47
4-7257

Encoded output: 3 4 7 256 258 257 (6 code words).

Original = 9 pixels × 8 bits = 72 bits. Compressed = 6 codes × 9 bits = 54 bits. CR = 72/54 = 1.3333 (compression achieved).

(ii) LZW reduces spatial / inter-pixel redundancy: repeated pixel patterns (3-4, 4-7, 7-3 …) are replaced by single dictionary codes.

(iii) Yes: 72 bits are coded with 6 × 9 = 54 bits, CR = 72/54 = 1.33 > 1.

Try it, step by step: SP22.

Run-length encoding

slides 107–118Came in past papers
In plain wordsRun-length encoding writes "what value, how many times" instead of every pixel. Ten 10s in a row become the pair (10, 10). It is great for images with long flat runs and bad for busy ones, where every run is short.

Slides 107–108. Image features: in the line image all 256 gray levels are equally probable (uniform histogram), so variable-length coding cannot be applied. But the gray level of each line is chosen at random, so pixels are independent vertically and identical (completely dependent) horizontally: spatial redundancy.

The spatial redundancy is eliminated with run-length pairs (a mapping scheme). A run-length pair has two parts: the start of a new intensity and the number of consecutive pixels having that intensity. Each 256-pixel line is replaced by a single 8-bit intensity and the run length 256: CR = (256 × 256 × 8)/((256 + 256) × 8) = 128.

Slide 109. RLE uses run-length pairs, e.g. (0, 1), (1, 2), (0, 1), (1, 3), …. It eliminates small spatial redundancies. However, small runs result in expansion instead of compression.

Slides 110–112 · Example 10.2.9, 8×8 4-bit imageworked
1010101010101010
1010101010121212
1010101010121212
00010101000
55500000
55510109910
55544400
00000000
image
Row(gray level, run length) pairs
Row 1(10,8)
Row 2(10,5) (12,3)
Row 3(10,5) (12,3)
Row 4(0,3) (10,3) (0,2)
Row 5(5,3) (0,5)
Row 6(5,3) (10,2) (9,2) (10,1)
Row 7(5,3) (4,3) (0,2)
Row 8(0,8)

18 pairs = 36 numbers, each stored in 4 bits. Original = 8×8×4 = 256 bits. RLE = 36×4 = 144 bits. CR = 256/144 = 1.7778.

As on slide 112: original 64 × 4 = 256 bits, compressed 36 × 4 = 144 bits, compression ratio 64:36 = 16:9.

Try it, step by step: Example 10.2.9, every run.

RLE in binary images (slides 113–117)

In binary images adjacent pixels are even more likely to be identical, so the image can be written as a sequence of run lengths only (e.g. 1b 2w 1b 3w 4b 1w → 121341). Scan each row left to right and code each contiguous run of 0s or 1s by its length. A convention fixes the value of the runs:

Example (slide 115): the 30-bit row 000001100000100000000101100000 has runs of 5, 2, 5, 1, 8, 1, 1, 2, 5. With the first convention it is written 0 5 2 5 1 8 1 1 2 5, where the leading 0 says "the first run is black (0)". With the second convention it is also 0 5 2 5 1 8 1 1 2 5, but now the leading 0 means "the first white run has length 0".

Slides 116–117 · Example 10.2.7, 8×8 binary imageworked
00000000
11110000
01100000
01111100
01110010
00100110
11110100
00000000
binary image (1 = white, 0 = black)

Convention: the first number of each row is the number of zeros (black) in the first run, so a row that starts with 1 begins with 0.

RowRLC numbers
Row 18
Row 20, 4, 4
Row 31, 2, 5
Row 41, 5, 2
Row 51, 3, 2, 1, 1
Row 62, 1, 2, 2, 1
Row 70, 4, 1, 1, 2
Row 88

Rows 2 and 7 begin with 0 because they start with a white run.

Slide 118. RLE in BMP combines an encoded mode (2-byte RLE: the first byte is the run length, the second the gray/colour index) and an absolute mode (first byte 0, the second byte is 0 end of line, 1 end of image, 2 move to a new position, 3–255 specify pixels individually). Either mode can appear anywhere in the image.

Fidelity criteria (how much quality was lost)

Must studyslides 119–132Came in past papers

Slide 119. Removing irrelevant information also removes useful "invisible" information. Information is lost in many other lossy compression techniques. How do we assess the loss? With image fidelity criteria.

Slide 120. The goal of image compression is to reduce file size while keeping acceptable quality. Fidelity criteria define how closely the reconstructed image matches the original, i.e. the degree of exactness (measuring the quality). Two classes: (i) objective fidelity criteria (quantifiable, mathematically defined); (ii) subjective fidelity criteria (person dependent, based on human observers).

Slides 121–122. f(x,y) is compressed to g(x,y) and decompressed to f̂(x,y) = f(x,y) + e(x,y). The error is e(x,y) = f̂(x,y) − f(x,y), and the total error between the two images is ΣΣ [f̂(x,y) − f(x,y)].

Slide 123. Objective criteria: RMSE, signal-to-noise ratio (SNR), structural similarity index (SSIM), normalized cross-correlation (NCC).

erms = [ (1/MN) Σx Σy ( f̂(x,y) − f(x,y) )² ]1/2

Slide 124. RMSE measures the average squared difference between pixel values of the original and decompressed image: compute the squared differences pixel by pixel, average them and take the square root. The smaller the RMSE, the better the compressed image represents the original.

Slide 125 · 2×2 exampleworked

Original [100 150; 200 250], reconstructed [102 148; 198 255].

Step 1, differences: [−2 2; 2 −5]. Step 2, squares: [4 4; 4 25]. Step 3, mean: (4 + 4 + 4 + 25)/4 = 37/4 = 9.25. Step 4, root: RMSE = √9.25 ≈ 3.04.

Slide 126. Three approximations of one image have RMSE 5.17, 15.67 and 14.17: the first is closest to the original.

SNRms = Σx Σy f̂(x,y)² / Σx Σy ( f̂(x,y) − f(x,y) )²

Slide 127. SNR expresses the ratio between the signal and the noise introduced by compression. The higher the SNR, the better the quality.

Slides 128–129 (short). SSIM evaluates similarity based on luminance, contrast and structure; it is often closer to human perception than MSE/PSNR; values from −1 to 1, where 1 is perfect similarity. NCC compares the correlation between original and compressed images and is invariant to linear changes in brightness and contrast; −1 (perfect negative) to 1 (perfect positive correlation).

Slides 130–131. Subjective criteria rely on human judgement: visual losslessness (indistinguishable to the eye), quality ratings (excellent, good, fair, poor), artifact evaluation (blockiness, blurring, colour banding). The Television Allocations Study Organization scale: 1 Excellent, 2 Fine, 3 Passable, 4 Marginal, 5 Inferior, 6 Unusable.

SP24 Q3(b)i · F23 Q2(c)i · F21 B2(b)i · SP25 Q3(b)ii2–5 marks

A 3×3, 3-bit image f(x,y) = [1 4 3; 2 3 4; 2 4 5] is compressed with a lossy technique; after decompression every pixel value increased by 2. Give the decompressed image f̂ and evaluate it with a suitable fidelity criterion (RMSE).

143
234
245
original f
365
456
467
decompressed f̂ = f + 2
222
222
222
error e = f̂ − f
444
444
444
e²

erms = [ (1/9) × (9 × 2²) ]1/2 = [ 36/9 ]1/2 = √4 = 2.

SNRms = Σ f̂² / Σ e² = 248 / 36 = 6.8889.

Every pixel is off by exactly 2 gray levels (out of 0–7), so the decompressed image is a brighter copy; the RMSE of 2 is large for a 3-bit image.

Try it, step by step: the RMSE, pixel by pixel.

Slide 132 · Class worksolved

A 3×3, 3-bit image [1,4,3,5,2,3,4,2,1] is lossy compressed; after decompression it becomes [3,6,5,7,4,5,6,4,3]. Calculate the RMSE.

Every pixel increased by 2, so e = 2 everywhere: erms = √((9 × 4)/9) = √4 = 2.

Try it, step by step: slide 132 class work.

Traps and one-minute sheet

slides all
methodremoveslossless?key formula / rule
Huffmancoding redundancyyesmerge two smallest; Lavg = Σ l p
Golombcoding redundancyyesunary(⌊n/m⌋) + truncated(n mod m)
Arithmeticcoding redundancyyesone number in the final interval
LZWspatial / inter-pixelyesdictionary from 256, 9-bit codes
RLEspatial / inter-pixelyes(value, run length) pairs
quantizationirrelevant informationno (lossy)judge with RMSE, SNR
entropy——H = −Σ p log2 p, lower bound on Lavg