Image encoding assigns a codeword to each grey level. Codeword lengths decide how many bits per pixel the coded image needs.
For grey level with probability and codeword length :
is the average number of bits per pixel of the coded image. It is bounded below by the entropy .
The smallest is achieved by assigning the shortest codeword to the most probable grey level.
Fixed Length Coding
All grey levels are assigned codewords of the same length.
Here:
- : number of grey levels
- : codeword length of grey level
- for every image, regardless of the grey level probabilities
- only when all grey levels are equiprobable and is a power of 2
- Simple to encode and decode, and allows random access to any pixel
- Contains coding redundancy whenever the grey levels are not equiprobable
Examples:
- 8bpp image, , bits
- 3 grey level image, , bits
Variable Length Coding
Grey levels are assigned codewords of different lengths. More probable grey levels get shorter codewords.
- Achieves close to the entropy , with only when every is a power of
- Must remain uniquely decodable
- A prefix code satisfies this, since no codeword is a prefix of another codeword
Example:
For the 3 grey level image in the entropy example:
| Grey level | Fixed code | Variable code | |
|---|---|---|---|
| White | 0.5 | 00 | 0 |
| Grey | 0.25 | 01 | 10 |
| Black | 0.25 | 10 | 11 |
Fixed length code:
Variable length code:
The variable length equals , so no coding redundancy remains.
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.
Example:
An image has 4 grey levels with probabilities 0.4, 0.3, 0.2, 0.1.
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.
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.
Example:
For the variable code above, the pixels white, grey, black give the stream 01011.
Unique Decodability
A fixed length code is always uniquely decodable.
A variable length code is uniquely decodable iff every bit stream can be split into codewords in exactly 1 way.
The stream has no fixed boundaries, so the decoder must find where each codeword ends. A codeword that is a prefix of another makes this ambiguous.
Example:
For the code , , , the stream 01 decodes as , or as followed by .
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
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
Example:
For the variable code in the table above, the stream 010110 splits as 0, 10, 11, 0. It decodes to white, grey, black, white.