Entropy

Information conveyed by an event is negatively related to its probability.

H(E)=log⁡21P(E)=−log⁡2P(E) bitsH(E) = \log_2 \frac{1}{P(E)} = -\log_2 P(E) \text{ bits}

The probability of a grey level comes from the histogram.

P(li)=riN∑i=0L−1ri=N∑i=0L−1P(li)=1P(l_i) = \frac{r_i}{N} \qquad \sum_{i=0}^{L-1} r_i = N \qquad \sum_{i=0}^{L-1} P(l_i) = 1

For an image, entropy is the probability-weighted sum of the information carried by each grey level.

Bavg=−∑i=0L−1P(li)log⁡2P(li)B_\text{avg} = -\sum_{i=0}^{L-1} P(l_i) \log_2 P(l_i)
Btotal=Bavg×NB_\text{total} = B_\text{avg} \times N
Here:
LL
number of grey levels
NN
number of pixels
lil_i
grey level ii
rir_i
number of pixels with grey level lil_i
P(li)P(l_i)
probability of grey level lil_i

BavgB_\text{avg} is the minimum average number of bits per pixel required to represent the image.

Bounds

0≤Bavg≤log⁡2L0 \le B_\text{avg} \le \log_2 L
  • Bavg=0B_\text{avg} = 0 iff a single grey level has P=1P = 1
  • Bavg=log⁡2LB_\text{avg} = \log_2 L iff every grey level has P=1LP = \frac{1}{L}
  • A grey level with P=0P = 0 adds nothing, since lim⁡P→0Plog⁡2P=0\lim_{P \to 0} P \log_2 P = 0

Data Redundancy

Information is the meaning conveyed by a message. Data is the container used to store or transmit it.

  • Information is not compressible, since its meaning doesn’t change with the medium
  • Data is compressible, since data capacity can exceed the information it carries

Image compression exploits 3 types of redundancy.

  • Coding redundancy
    The coding scheme uses more space than the information requires, e.g. an 8bpp image stored in 16bpp format.
  • Interpixel redundancy
    Pixel values are correlated locally, e.g. a monotonous background or a repeating texture.
  • Psychovisual redundancy
    Image detail that isn’t visually perceivable.

Average Code Length

A code assigns grey level kk a codeword of l(k)l(k) bits. With P(k)=rk/NP(k) = r_k / N the probability of grey level kk, the average codeword length and the size of the coded image are:

Lavg=∑k=0L−1l(k) P(k)b=N LavgL_\text{avg} = \sum_{k=0}^{L-1} l(k)\, P(k) \qquad b = N\, L_\text{avg}

No code beats the entropy, and the gap is the coding redundancy per pixel.

Lavg≥BavgRcoding=Lavg−BavgL_\text{avg} \ge B_\text{avg} \qquad R_\text{coding} = L_\text{avg} - B_\text{avg}
Here:
l(k)l(k)
codeword length of grey level kk in bits
LavgL_\text{avg}
average codeword length in bits per pixel
bb
size of the coded image in bits
RcodingR_\text{coding}
coding redundancy in bits per pixel

An 8bpp natural binary code has l(k)=8l(k) = 8 for every kk, so Lavg=8L_\text{avg} = 8. Replacing it with a code of average length Lavg′L'_\text{avg} gives the compression ratio CRCR and the relative data redundancy RDRD:

CR=8Lavg′RD=1−Lavg′8CR = \frac{8}{L'_\text{avg}} \qquad RD = 1 - \frac{L'_\text{avg}}{8}

The best possible compression ratio from removing coding redundancy alone is 8/Bavg8 / B_\text{avg}.

Worked Examples

3 Grey Levels

An image has 100 pixels across 3 grey levels: white (rk=50r_k = 50, P=0.5P = 0.5), grey (rk=25r_k = 25, P=0.25P = 0.25), black (rk=25r_k = 25, P=0.25P = 0.25). Find its entropy.

Solution Reveal Hide
Bavg=−0.5log⁡20.5−0.25log⁡20.25−0.25log⁡20.25=1.5 bits per pixelB_\text{avg} = -0.5\log_2 0.5 - 0.25\log_2 0.25 - 0.25\log_2 0.25 = 1.5 \text{ bits per pixel}Btotal=Bavg×N=1.5×100=150 bitsB_\text{total} = B_\text{avg} \times N = 1.5 \times 100 = 150 \text{ bits}

4x4 Image

Entropy of the following 4x4 image with grey levels 0 to 3.

Col 1Col 2Col 3Col 4
Row 10010
Row 20201
Row 31003
Row 40123
Solution Reveal Hide

Count the pixels of each grey level, with N=16N = 16 and P(li)=ri/NP(l_i) = r_i / N:

Grey level lil_irir_iP(li)P(l_i)−log⁡2P(li)-\log_2 P(l_i)−P(li)log⁡2P(li)-P(l_i) \log_2 P(l_i)
080.510.5
140.2520.5
220.12530.375
320.12530.375

Sum the last column.

Bavg=0.5+0.5+0.375+0.375=1.75 bits per pixelB_\text{avg} = 0.5 + 0.5 + 0.375 + 0.375 = 1.75 \text{ bits per pixel}Btotal=1.75×16=28 bitsB_\text{total} = 1.75 \times 16 = 28 \text{ bits}

A fixed length code needs 2 bits per pixel, so 32 bits for the image. The gap of 2−1.75=0.252 - 1.75 = 0.25 bits per pixel is the maximum saving from any encoding.

Written by November 3, 2026 3 min read
Was this helpful?