You are on page 1of 14

Lecture 4

Image data compression, Discrete Cosine


Transform, and the JPEG format

Compression
• There are two primary motivations for
compressing image data:
– Reduction of storage requirements. Many jurisdictions
require that patient data be retained for at least 10 years.
– Reduced transmission times for image data over
networks (“teleradiology”, “telemedicine”, etc.).
JPEG Compression
• The JPEG (Joint Photographic Experts Group)
compression technique is one of the most popular
image data compression methods in use.
• It is a transform (DCT) based method that involves
some loss of information (hence “lossy” data
compression) in proportion to the degree of
compression achieved.

JPEG Compression
• It is important to remember that taking the transform of
an image does not result in loss of information. The
complete information still exists, but is simply presented
in another form (i.e. spatial frequency). The data file
size is the same, or even larger than the original (e.g. real
integer Æ complex float).
• Loss of information takes place when some of the
transform coefficients are discarded.
JPEG Compression
• The loss of information with JPEG is manifested
primarily in degradation of edges, sharp lines, i.e.
features defined by high spatial frequency
components.
• Adequate grey scale precision is important, as an
input image with only a few grey scale levels may
give a result in which boundaries will undergo
significant geometric distortion.
Original 3.5:1 5.8:1 7.9:1
JPEG Compression
• The use of JPEG should be restricted to images
that are intended primarily for visual examination,
as opposed to quantitative analysis.

JPEG Compression
• The JPEG algorithm implements the discrete
cosine transform with the following additional
steps:
1. The image is subdivided into 8x8 pixel
blocks. If the image size in one or both
dimensions is not an integer multiple of 8,
the array is padded out to the next integer
multiple.
JPEG Compression
2. Each block is transformed using the DCT. For an
8 bit precision input (255 grayscales) the
transform is usually represented at 12 bits
precision including a sign bit. At this point, there
has not been any loss of information aside from
rounding errors due to the finite precision of the
DCT.

JPEG Compression
3. The 64 coefficients for each block are reduced to
lower precision, causing some coefficients to
disappear, and reducing precision of remaining
higher order coefficients. This is the step where
information is lost. In general, more luminance
(contrast) information is retained, since this is
the dominant factor in viewing of images.
4. The first coefficient is the DC term (average
brightness). It is represented as the difference
from the same term in the preceding block.
Blocks are listed in raster scan order.
JPEG Compression
5. The remaining coefficients are listed in order with
lowest order coefficients first. The entire stream is
then further compressed using Huffman coding.
This step is loss-free.
6. Reconstitution of the image is simply the reversal
of these steps and requires the same computation
time.
JPEG Compression
• Huffman coding is a technique which uses the
frequency with which each grey level values
occurs in the image (histogram) to assign a code
for each grey level. Those grey level values that
occur most frequently have the shortest codes (1-4
bits). Longer codes are assigned to infrequently
occurring grey level values.

Uncompressed Compressed

Blood 33.9

37.4

Liver Needle
22.2
Biopsy
Breast Tissue – Which one is compressed?

JPEG Compression
• The JPEG standard has been enlarged to include the
wavelet transform (JPEG2000), which shows
promise in reducing some of the distracting artifacts
that can appear in the JPEG result at higher
compression levels.
• However, the method can produce small alterations
to boundaries, which can be unacceptable in
applications requiring morphologic quantification.
MATLAB Syntaxes
• Although MATLAB provides a function for image
conversion to the JPEG format, it will be useful to
examine the process in detail.
• In this case, rather than transforming the image as
a whole, it will be broken down into 8x8 blocks
and subjected to an 8x8 DCT transform matrix T:
1
Tpq , p 0, 0 d q d M 1
M
2 S 2q 1 p
Tpq cos , 1 d p d M 1, 0 d q d M 1
M 2M

The Operations behind JPEG/DCT

• For an MxM matrix A, TxA is an MxM matrix


whose columns contain the 1D DCT of the
columns of A. The 2D DCT of A is given as:
B = T x A x T’

• T is a real orthonormal matrix so its inverse is


equal to its transpose. Thus the inverse 2D DCT
is:
A = T’ x B x T
MATLAB Syntax for JPEG/DCT
• First generate the transform matrix T:
– T = dctmtx(8);

• Apply T across the image in 8x8 blocks:


– B = blkproc(A,[8 8],’P1*x*P2’,T,T’);

• At this point you now have the DCT of all of the


8x8 blocks in the image.

MATLAB Syntaxes
• Compression at this point is achieved by setting
some of the DCT coefficients in each block to
zero.
• In this case this can be accomplished by
generating an 8x8 binary mask array in which the
positions of the coefficients are represented by 1 if
the coefficients are to be retained, and by 0 if they
are to be discarded.
MATLAB Syntaxes
• Upon examination of the DCT of one of the
blocks it is observed that the DC coefficient is
located in the upper left, with spatial frequency
increasing down and to the right. Thus, the mask
array elements will have values of 1 in the upper
left region.

MATLAB Syntaxes
ª1 1 1 1 0 0 0 0º
«1 1 1 0 0 0 0 0»»
«
«1 1 0 0 0 0 0 0»
« »
« 1 0 0 0 0 0 0 0»
mask
«0 0 0 0 0 0 0 0»
« »
«0 0 0 0 0 0 0 0»
«0 0 0 0 0 0 0 0»
« »
«¬0 0 0 0 0 0 0 0»¼
MATLAB Syntaxes
• To enter the mask matrix by rows:
– mask=[1 1 1 1 0 0 0 0; 1 1 1 0 0 0 0 0; 1 1 0 0 0 0 0 0;…];
• This only needs to be done once, since in Version 6 and
upwards of MATLAB one can then pull up the array
editor to make changes.
• It is at the mask stage that you effectively eliminate
coefficients to simulate compression.
• The key is not to compromise diagnostically relevant
information in the compression process.

JPEG Usage
• JPEG is at its best on photographs and paintings of
realistic scenes with smooth variations of tone and
color. In this case it usually performs much better than
purely lossless methods while still giving a good
looking image (in fact it will produce a much higher
quality image than other common methods such as GIF
which are lossless for drawings and iconic graphics but
require severe quantization for full-color images).
• Some medical image processing software packages use
12-bit JPEG compression.

You might also like