Image encoding assigns a codeword to each grey level. Codeword lengths decide how many bits per pixel the coded image needs.
The average codeword length , its lower bound and the coding redundancy are defined in entropy. is the average number of bits per pixel of the coded image.
The smallest is achieved by assigning the shortest codeword to the most probable grey level.
Decodability
Code
A code assigns a codeword to each grey level. A codeword is a finite sequence of bits.
- Encoded stream
Concatenation of the codewords of all pixels in order, with no separators. - Codebook
The mapping between grey levels and codewords.
Unique Decodability
A code is uniquely decodable iff every encoded stream corresponds to exactly 1 sequence of grey levels.
- Two different sequences of grey levels never produce the same encoded stream
- The decoder can split the stream into codewords in only 1 way
A code that is not uniquely decodable has an ambiguous stream. With , and , the stream 010 decodes as or as .
A uniquely decodable code can still need lookahead. With , and , the codeword 0 is a prefix of 01, so the decoder must read further bits before it knows where a codeword ends.
Sardinas-Patterson Test
If a codeword is a proper prefix of a codeword , the non-empty remainder is a dangling suffix.
- Let be the set of codewords
- Let be the dangling suffixes of all codeword pairs in
- Let be the non-empty remainders when a codeword in is a prefix of some , or some is a prefix of a codeword in
- If any contains a codeword, the code is not uniquely decodable
- If is empty or repeats an earlier with , the code is uniquely decodable
- Otherwise repeat from step 3
Prefix Code
A code is a prefix code iff no codeword is a prefix of another codeword.
- Codewords sit only at the leaves of the code tree
- A codeword is recognised as soon as its last bit is read, with no lookahead
- Every prefix code is uniquely decodable
- Huffman codes are prefix codes
A prefix code with codeword lengths exists iff the lengths satisfy the Kraft inequality:
- The sum equals 1 iff the code tree is full, with no unused leaf
- Every uniquely decodable code satisfies the same inequality
Decoding a prefix code:
- Read bits until they match a codeword
- Output the grey level and clear the bits read
- Repeat from the next bit
Fixed Length Coding
All grey levels are assigned codewords of the same length.
- number of grey levels
- codeword length of grey level
Easy to encode and decode. Allows random access to any pixel. Always uniquely decodable.
is the same for every image, regardless of the grey level probabilities. For an -pixel image, the coded size is bits.
Coding redundancy shows up when gray levels are not equiprobable. Otherwise, when is a power of 2, .
Variable Length Coding
Grey levels are assigned codewords of different lengths. More probable grey levels get shorter codewords. Must remain uniquely decodable.
Achieves close to the entropy , with only when every is a power of .
Uniquely decodable iff every bit stream can be split into codewords in exactly 1 way.
The ideal length of grey level is its information content. Real codewords have integer lengths, so the ideal length is reached only when is a power of .
Satisfies the Kraft inequality.
Compared with a fixed length code:
Huffman Coding
Produces the optimal variable length codeword allocation while preserving decodability. Reduces the source by merging the least probable grey levels, then assigns codes back through the reductions.
The code is optimal among prefix codes. Its average length satisfies:
Steps:
- List grey levels in descending order of probability
- Add the 2 lowest probabilities into 1 and re-sort the list
- Repeat until 2 probabilities remain
- Assign 0 and 1 to the last 2 probabilities
- Working backwards, append 0 and 1 to the code of every merged probability, one bit for each of the 2 probabilities it came from
Interpixel Redundancy
Correlated or repeating pixel patterns, such as texture or constant regions, can be represented more compactly than as independent pixel values.
Delta Coding
Transforms pixels into differences from a neighbour:
- grey level at column and row
- difference between a pixel and its right neighbour
The first pixel of each row has no left neighbour, so it is stored unchanged. The decoder rebuilds the row by accumulating the differences:
- Differences are typically small integers, since local pixels are correlated
- The difference histogram peaks sharply at 0, so its entropy is lower than the entropy of the raw grey levels
- Reduces correlation among pixel values, increasing coding efficiency
- Lossless, since the accumulation recovers every pixel exactly
Image Pyramid Coding
Each layer stores the difference from the layer above, upscaled to match resolution. Removes correlation relative to the previous layer.
The image is repeatedly filtered with a Gaussian low-pass filter and downsampled to build layers of decreasing resolution:
Each layer is predicted by upscaling the next coarser layer. Only the prediction error is stored:
The coarsest layer is stored as is. The decoder starts there and works down to full resolution:
- original image
- image at level , with the full-resolution image
- index of the coarsest level
-
Gaussian low-pass filtering followed by downsampling by 2 in each dimension
-
upsampling by 2 in each dimension with interpolation
- difference layer at level
Difference layers are mostly near 0 in smooth regions, so they have low entropy. The total number of samples is about of the image size, since each level has the samples of the one below. Compression comes from the lower entropy per sample.
Used for progressive image transmission, updating the receiver incrementally. The receiver shows first, then sharpens it as each arrives.
Run-Length Coding
Encodes a run of identical pixel values as pairs rather than storing each pixel separately. Pixels are scanned row by row, and a run normally ends at the end of a row.
A run of pixels, with bits per value and bits per count, saves space iff:
For a row with runs and pixels:
- Short runs can take more space than the raw pixels, so isolated pixels are a worst case
- More effective on binary images, which more often contain large regions of constant value
- In a binary image, runs alternate between 0 and 1. Only the first value and the run lengths need to be stored.
Bit-Plane Coding
Separates an -bit grey level image into binary bit-planes:
-
bit of the pixel at , with the least significant bit
Plane is the binary image formed by over all pixels.
- Lower order planes capture minor grey level changes
- Higher order planes capture larger changes, and hold larger regions of constant value
- Higher order planes compress better with schemes such as run-length coding
A small grey level change can flip many bits in the natural binary code, e.g. and differ in all 8 bits. This breaks runs in every plane. Gray code representation avoids it, since consecutive grey levels differ in exactly 1 bit:
The decoder recovers the original bits from the top plane downwards:
- bit of the Gray coded pixel
- exclusive OR
Psychovisual Redundancy
Not all image detail is equally perceivable. Removing imperceptible detail doesn’t affect apparent quality, but always loses information.
- Re-quantizing with lower grey level depth
- Gaussian filtering to remove noise and minor variations before coding
Re-quantizing an -bit image to bits keeps the top bits of each pixel:
Gaussian filtering replaces each pixel with a weighted average of its neighbourhood:
- bits per pixel before and after re-quantizing
- pixel value after the operation
- Gaussian weight at offset
Worked Examples
Fixed Length Code Sizes
Find the codeword length of a fixed length code for an 8bpp image () and for an image with 3 grey levels.
Solution Reveal Hide
For the 3 grey level image, , so the code wastes about bits per pixel.
Fixed vs Variable Length Code
For the 3 grey level image in the entropy worked example, compare a fixed length code with a variable length code, using the codes below.
| Grey level | Fixed code | Variable code | |
|---|---|---|---|
| White | 0.5 | 00 | 0 |
| Grey | 0.25 | 01 | 10 |
| Black | 0.25 | 10 | 11 |
Solution Reveal Hide
Fixed length code:
Variable length code:
The variable length equals , so no coding redundancy remains.
Huffman Code
An image has 4 grey levels with probabilities 0.4, 0.3, 0.2, 0.1. Build a Huffman code and find its .
Solution Reveal Hide
Codewords:
| Grey level | Codeword | ||
|---|---|---|---|
| 0.4 | 1 | 1 | |
| 0.3 | 00 | 2 | |
| 0.2 | 010 | 3 | |
| 0.1 | 011 | 3 |
A fixed length code needs 2 bits per pixel for 4 grey levels.
Checking Unique Decodability
Use the Sardinas-Patterson test on the code .
Solution Reveal Hide
comes from the codeword pairs where one is a prefix of the other.
- is a prefix of , with remainder
- is a prefix of , with remainder
- is a prefix of , with remainder
No element of is a codeword. comes from and .
- is a prefix of , with remainder
- is a prefix of , with remainder
- is a prefix of , with remainder
- No other pair has a prefix relation
is a codeword, so the code is not uniquely decodable. The stream 1110011 decodes as followed by , or as , , .
Delta Coding of a Row
Delta code the row , then decode it.
Solution Reveal Hide
The first pixel is stored unchanged. Every other value is the difference from its predecessor.
The decoder accumulates the differences.
Run-Length Coding of a Row
Run-length code the binary row .
Solution Reveal Hide
The row has 4 runs.
With and , the coded row takes bits against bits raw.
The runs are too short, so the coded row is larger than the raw row.