Image Compression
Introduction
In this study, we want to compress images by using two methods: JPEG and Singular Value Decompositon.
We assume that the images are grayscale images.
Each pixel is represented by a brightness value ranging from
from math import* from numpy.random import* import random import numpy as np import scipy.linalg as sl import matplotlib.image as mpimg import matplotlib.pyplot as plt A = mpimg.imread('lena.png') A = A * 255
JPEG compression and Discrete Cosines Transform
Introduction
Every JPEG image is actually stored as a series of compressed image tiles that are usually only
-
Segmentation into Blocks
The raw image data is chopped into 8x8 pixel blocks (these blocks are the Minimum Coded Unit). This means that the JPEG compression algorithm depends heavily on the position and alignment of these boundaries.
-
Discrete Cosine Transformation (DCT)
The image is transformed from a spatial domain representation to a frequency domain representation. This is perhaps the most confusing of all steps in the process and hardest to explain. Basically, the contents of the image are converted into a mathematical representation that is essentially a sum of wave patterns.
-
Quantization
Given the resulting wave equations from the DCT step, they are sorted in order of low-frequency components (changes that occur over a longer distance across the image block) to high-frequency components (changes that might occur every pixel). Is it widely known that humans are more critical of errors in the low-frequency information than the high-frequency information. The JPEG algorithm discards many of these high-frequency (noise-like) details and preserves the slowly-changing image information. This is done by dividing all equation coefficients by a corresponding value in a quantization table and then rounding the result to the nearest integer. Components that either had a small coefficient or a large divisor in the quantiziation table will likely round to zero. The lower the quality setting, the greater the divisor, causing a greater chance of a zero result. On the converse, the highest quality setting would have quantization table values of all 1's, meaning the all of the original DCT data is preserved.
-
Zigzag Scan
The resulting matrix after quantization will contain many zeros. The lower the quality setting, the more zeros will exist in the matrix. By re-ordering the matrix from the top-left corner into a 64-element vector in a zig-zag pattern, the matrix is essentially sorted from low-frequency components to high-frequency components. As the high-frequency components are the most likley to round to zero, one will typically end up with a run of zeros at the end of the 64-entry vector. This is important for the next step.
1D Fourier Serie and Discrete Cosines Transform
A Fourier series is an expansion of a
Note that the coefficient of the constant term
For image compression, in general
Consequently, the Fourier serie expansion only contains cosines terms:
we then have
Let
If we replace
2D Discrete Cosines Transform
We denote by
and

The use of this basis for image compression was introduced by Ahmed, Natarajan
et Rao (IEEE 1974).
To perform the first step of JPEG compression, the image in question is first split
into
Notice the upper-left elements have lower spacial frequency both in the horizontal and vertical directions, while the lower-right elements have higher frequencies. With a DCT, most of the original information can be reconstructed from the lower frequency coefficients (the ones closer to the top-left), because of the high-energy compaction in those coefficients. Additionally, the human visual system is less perceptive to errors in high-frequency spacial content. These two facts together mean that errors in the low-frequency coefficients will be significantly more noticeable to human beings than errors in the high-frequency elements.
Quantization
Once the DCT is applied to the
Though the JPEG compression standard does not specify a quantization matrix
this is one of the suggested matrices. To quantize the result of the 2-D DCT,
each coefficient is divided by the appropriate value from the matrix above
and rounded to the nearest integer.
Coefficients
Let’s go through an example. For each
By applying the inverse DCT on the quantized DCT matrix,

Zig-Zag Sequencing
After quantization, the 2-D matrix is rearranged into a 1-D array.
The elements are read in a fashion that gives the coefficients with high energy
density first. The sequencing is done in a zig-zag method such that coefficients
are arranged in an increasing spacial frequency order. Using this method,
the more important coefficients appear earlier on in the series,
while the less important ones appear later on.
As the high-frequency components are the most likley to round to zero,
one will typically end up with a run of zeros at the end of the
Let’s imagine we have this quantized matrix:
The output of zig-zag encoding will be this:
Exercice [PY-1]
Apply the 4 steps of the JPEG method so as to compress the lena image. Draw the result and save the "zigzag" sequences.
Singular Value Decomposition and Image Compression
To compress the lena image, we apply in this Section the "famous" Theorem called Singular Value
Decomposition of a matrix.
We will see that the compression of the image is obtained by creating
an approximation matrix
The eigenvalue decomposition of a square matrix

A singular matrix
Let
-
is an matrix composed of the orthogonal eigenvectors of the symmetric matrix , also called the left singular vectors of : where is the eigenvalue matrix of with . -
is an matrix composed of the orthogonal eigenvectors of the symmetric matrix , also called the right singular vectors of Note that and have the same eigenvalue matrix . -
is an diagonal matrix of non-zero values , the square roots of the eigenvalues of or defined as the singular values of : with .
Pre-multiplying
If
If
Note that the matrix can be rewritten as a sum
of outer products of columns of
Exercice [M-1]
-
Show that
have the same solution where (resp. ) are the column vectors of (resp. ). -
Show that for any
, and
Let
The SVD equation
Therefore, we need to show that for any matrix
For any
Consequently, there exists
Exercice [PY-2]
-
Create a random matrix
of size 30 × 50 such that each coefficient follows a random law: (seerandom.rand
oruniform
). -
Draw the relation between the singular values of
and the spectrum of and (sort the values by usingsorted
). -
Create matrix
being the best least squares approximation of rank of . Calculate the error of this approximation with the -norm and the Schur (Frobenius) norm for with and the draw the error curves. Compare these errors (you can use the results of the exercice above) -
Use the command
data = mpimg.imread(’lena.png’)
so as to associate the pixel values of the image to the data matrix of size (256 × 256) -
Viewing the photo can be done with the
plt.imshow (data, cmap = "gray")
command. You can add a threshold for the gray values, by adding the parametersvmin = 0
andvmax = 255
. -
Calculate the singular value decomposition of
then display the minimum and maximum of coefficients of , , as well as the singular values and . -
The
plt.subplots
command will allow you to create subplots:
fig, ax = plt.subplots (2,2, figsize = (10,10))
Display on 4 graphics side by side the image of Lena as well as the approximations of rank 5, 15 and 50 of this same image. Add titles to each subplot. How much memory space saved with the last rebuild?
We use scipy.linalg
to obtain the SVD of [U,S,Vh] = sl.svd(A)
(this algorithm uses the QR-decomposition)
(we recall that Vh=s = sl.svdvals(A)
References
- C. Eckart, G. Young, The approximation of one matrix by another of lower rank. Psychometrika, Volume 1, 1936, Pages 211–8. doi:10.1007/BF02288367
- W. B. Pennebaker and J. L. Mitchell, Van Nostrand Reinhold, JPEG still image data compression standard, New York, 1992
- Golub, G.H., and Van Loan, C.F. 1989, Matrix Computations, 2nd ed. (Baltimore: Johns Hopkins University Press), x8.3 and Chapter 12.
- H.J. Nussbaumer (1981) Fast Fourier Transform and Convolution Algorithms. Springer-Verlag.
- Forsythe, G.E., Malcolm, M.A., and Moler, C.B. 1977, Computer Methods for Mathematical Computations (Englewood Cliffs, NJ: Prentice-Hall), Chapter 9