Image Compression

80
Image Compression Chapter 8

description

Image Compression. Chapter 8. 1 Introduction and background. The problem: Reducing the amount of data required to represent a digital image. Compression is achieved by removing the data redundancies: Coding redundancy Interpixel redundancy Psychovisual redundancy. - PowerPoint PPT Presentation

Transcript of Image Compression

Page 1: Image  Compression

Image Compression

Chapter 8

Page 2: Image  Compression

1 Introduction and background

The problem: Reducing the amount of data required to represent a digital image.

Compression is achieved by removing the data redundancies: Coding redundancy Interpixel redundancy Psychovisual redundancy

Page 3: Image  Compression

1 Introduction and background (cont.)

Structral blocks of image compression system. Encoder Decoder

The compression ratio

where n1 and n2 denote the number of information carrying units (usually bits) in the original and encoded images respectively.

2

1

n

nCR

Page 4: Image  Compression

1 Introduction and background (cont.)

Page 5: Image  Compression

1 Introduction and background (cont.)

function cr = imratio(f1,f2)

%IMRATIO Computes the ratio of the bytes in two images / variables.%CR = IMRATIO( F1 , F2 ) returns the ratio of the number of bytes in%variables / files F1 and F2. If F1 and F2 are an original and compressed%image, respectively, CR is the compression ratio.

error(nargchk(2,2,nargin)); %check input argumentscr = bytes(f1) / bytes(f2); %compute the ratio

The Function that finds the compression ratio between two images:

Page 6: Image  Compression

1 Introduction and background (cont.)

(cont.)

% return the number of bytes in input f. If f is a string, assume that it% is an image filename; if not, it is an image variable.

function b = bytes(f)if ischar(f) info = dir(f); b = info.bytes; elseif isstruct(f) b = 0; fields = fieldnames(f); for k = 1:length(fields) b = b + bytes(f.(fields{k})); end else info = whos('f'); b = info.bytes; end

Page 7: Image  Compression

1 Introduction and background (cont.)

>> r = imratio( (imread( 'bubbles25.jpg' ) ) , bubbles25.jpg')r = 0.9988

>> f = imread('bubbles.tif');>> imwrite(f,'bubbles.jpg','jpg')>> r = imratio( (imread( 'bubbles.tif' ) ) , 'bubbles.jpg')

r = 14.8578

Page 8: Image  Compression

1 Introduction and background (cont.)

Let denote the reconstructed image.

Two types of compression Loseless compression: if Loseless compression: if

),(ˆ and image thedenote ),( yxfyxf

),( of tionrepresentaexact an is ),(ˆ yxfyxf

),( of tionrepresentaexact an is ),(ˆ yxfyxf

Page 9: Image  Compression

1 Introduction and background (cont.)

In lossy compression the error between is defined by root mean square which is given by

),(ˆ and ),( yxfyxf

2/11

0

1

0

2)],(ˆ -),(ˆ[1

M

x

N

yrms yxfyxf

MNe

Page 10: Image  Compression

1 Introduction and background (cont.)

The M- Function that computes e(rms) and displays both e(x,y) and its histogram

% COMPARE Computes and displays the error between two matrices. RMSE = COMPARE (F1 , F2, SCALE) returns the root-mean-square error between inputs F1 and F2, displays a histogram of the difference, and displays a scaled difference image. When SCALE ,s omitted, a scale factor of 1 is used

function rmse = compare(f1 , f2 , scale)error(nargchk(2,3,nargin));if nargin < 3 scale = 1;end

%compute the root mean square error

e = double(f1) - double(f2);[m,n] = size(e);rmse = sqrt (sum(e(:) .^ 2 ) / (m*n));

Page 11: Image  Compression

1 Introduction and background (cont.)

(cont.)%output error image & histogram if an error if rmse %form error histogram. emax= max(abs(e(:))); [h,x]=hist(e(:),emax); if length(h) >= 1 figure; bar(x,h,'k'); %scale teh error image symmetrically and display emax = emax / scale; e = mat2gray(e,[-emax,emax]); figure; imshow(e); endend

Page 12: Image  Compression

1 Introduction and background (cont.)

>> r1 = imread('bubbles.tif');>> r2 = imread('bubbles.jpg');>> compare(r1, r2,1)>> In E:\matlab\toolbox\images\images\truesize.m (Resize1) at line 302 In E:\matlab\toolbox\images\images\truesize.m at line 40 In E:\matlab\toolbox\images\images\imshow.m at line 168 In E:\matlab\work\matlab_code\compare.m at line 32

ans = 1.5660

Page 13: Image  Compression

1 Introduction and background (cont.)

Error histogram

Page 14: Image  Compression

1 Introduction and background (cont.)

Error image

Page 15: Image  Compression

2 Coding redundancy

Let nk denote the number of times that the kth gray level appears in the image and n denote the total number of pixels in the image. The associated probability for the kth gray level can be expressed as

Lkn

nkp k

r ...,,2,1,)(

Page 16: Image  Compression

2 Coding redundancy (cont.)

If the number of bits used to represent each value of rk is

l(rk), then the average number of bits required to represent each pixel is

Thus the total number of bits required to code an M×N image is MNLavg

L

krkavg kprlL

1)()(

Page 17: Image  Compression

2 Coding redundancy (cont.)

When fixed variable length that is l(rk) =m then

m

kpm

kmpL

L

kr

L

kravg

1

1

)(

)(

Page 18: Image  Compression

2 Coding redundancy (cont.)

The average number of bits requred by Code 2 is

8125.18125.1/2

8125.1)1875.0(2)125.0(3)5.0(1)1875.0(3

)()(4

12

r

kkravg

C

rpklL

Page 19: Image  Compression

2 Coding redundancy (cont.)

How few bits actually are needed to represent the gray levels of an image?

Is there a minimum amount of data that is sufficient to describe completely an image without loss information?

Information theory provides the mathematical framework to answer these questions.

Page 20: Image  Compression

2 Coding redundancy (cont.)

Formulation of generated information

)(log)(

1log)( EP

EPEI

Note that if P(E)=1 that is if the event always occurs then I(E)=0 and no information is attributed to it. No uncertainity associated with the event

Page 21: Image  Compression

2 Coding redundancy (cont.)

Given a source of random events from the discrete set of possible events with associated probabilities naaa ...,,, 21

The average information per source output, called the entropy, is

J

jjj aPaPH

1)(log)(

)(...,),(),( 21 naPaPaP

Page 22: Image  Compression

2 Coding redundancy (cont.)

If we assume that the histogram based on gray levels is an estimate of the true probability distribution, the estimate of H can be expressed by

L

kkrk rprpH

1)(log)(

~

Page 23: Image  Compression

1 Introduction and background (cont.)

function h = entropy(x,n)

%ENTROPY computes a first-order estimate of the entropy of a matrix.%H = ENTROPY(X,N) returns the first-order estimate of matrix X with N%symbols in bits / symbol. The estimate assumes a statistically independent%source characterized by the relative frequency of occurence of the%elements in X

error(nargchk(1,2,nargin));

if nargin < 2 n=256;end

Page 24: Image  Compression

1 Introduction and background (cont.)

(cont)

x = double(x);xh = hist(x(:),n);xh = xh/sum(xh(:));

% make mask to eliminate 0's since log2(0) = -inf

i = find(xh);h = -sum(xh(i) .* log2(xh(i))); %compute entropy

Page 25: Image  Compression

1 Introduction and background (cont.)

f = [119 123 168 119;123 119 168 168] ;f = [f; 119 119 107 119 ; 107 107 119 119] ;f = 119 123 168 119 123 119 168 168 119 119 107 119 107 107 119 119

p=hist(f(:),8)p =

3 8 2 0 0 0 0 3

Page 26: Image  Compression

1 Introduction and background (cont.)

p = p / sum(p)

p =

Columns 1 through 7

0.1875 0.5000 0.1250 0 0 0 0 0.1875

H = entropy(f)

h =

1.7806

Page 27: Image  Compression

Huffman codes

Huffman codes are widely used and very effective technique for compressing data.

We consider the data to be a sequence of charecters.

Page 28: Image  Compression

Huffman codes (cont.)

Consider a binary charecter code wherein each charecter is represented by a unique binary string.

Fixed-length code:

a = 000, b = 001, c = 010, d = 011, e = 100, f = 101

variable-length code:

a = 0, b = 101, c = 100, d = 111, e = 1101, f = 1100

Page 29: Image  Compression

Huffman codes (cont.)

100

86 14

58 28 14

b:13

a:45

d:16c:12 f:5e:9

100

55

25 30

c:12

a:45

a:45 b:13 14d:16 d:16

f:5 e:9

Fixed-length code

1

0

0

0

0

0

0 0

0

0 0

0

1

1 1 1 1

1

1

1

1

Variable-length code

Page 30: Image  Compression

Huffman codes (cont.)

Prefix code:Codes in which no codeword is also a prefix of some other codeword.

Encoding for binary code:Example: Variable-length prefix code.

a b c

Decoding for binary code:Example: Variable-length prefix code.

01011001001010

ebaa 110110100001011101

Page 31: Image  Compression

Constructing Huffman codes

tree theofroot Return theMIN(Q)-EXTRACT9

),(NSERT8

7

MIN(Q)-EXTRACT6

MIN(Q)-EXTRACT5

node new a allocate 4

113

2

1

)HUFFMAN(

return

do

tofor

zQI

yfxfzf

yzright

xzleft

z

ni

CQ

cn

C

Page 32: Image  Compression

Constructing Huffman codes

Huffman’s algorithm assumes that Q is implemented as a binary min-heap.

Running time: Line 2 : O(n) (uses BUILD-MIN-HEAP) Line 3-8: O(n lg n) (the for loop is executed exactly n-

1 times and each heap operation requires time O(lg n) )

Page 33: Image  Compression

Constructing Huffman codes: Example

c:12 b:13e:9f:5 d:16 a:45

14b:13c:12 d:16 a:45

f:5 e:9

14

f:5 e:9

d:16 a:45 25

c:12 b:13

00

000

1

11

Page 34: Image  Compression

Constructing Huffman codes: Example

14

f:5 e:9

d:16

a:45 25

c:12 b:13

30

0 0

0

1

1

1

Page 35: Image  Compression

Constructing Huffman codes: Example

14

f:5 e:9

d:16

a:45

25

c:12 b:13

30

55

0

0 0

0

1

1

1

1

Page 36: Image  Compression

Constructing Huffman codes: Example

14

f:5 e:9

d:16

a:45

25

c:12 b:13

30

55

100

1

1

1

1 1

0

0

0

0

0

Page 37: Image  Compression

Huffman Codes

function CODE = huffman(p)%check the input arguments for reasonablenesserror(nargchk(1,1,nargin));if (ndims(p) ~= 2) | (min(size(p))>1)| ~isreal(p)|~isnumeric(p) error('P must be a real numeric vector');endglobal CODECODE = cell(length(p),1);if length (p) > 1 p=p /sum(p); s=reduce(p); makecode(s,[]);else CODE ={'1'};end;

Page 38: Image  Compression

Huffman Codes(cont.)

%Create a Huffman source reduction tree in a MATLAB cell structure by performing %source symbol reductions until there are only two reduced symbols remaining

function s = reduce(p);s= cell(length(p),1)for i=1:length(p) s{i}=i;endwhile numel(s) > 2 [p,i] = sort(p); p(2) = p(1) + p(2); p(1) = []; s = s(i); s{2} = {s{1},s{2}}; s(1) = [];end

Page 39: Image  Compression

Huffman Codes(cont.)

%Scan the nodes of a Huffman source reduction tree recursively to generate%the indicated variable length code words.

function makecode(sc,codeword)

global CODEif isa(sc,'cell') makecode(sc{1},[codeword 0]); makecode(sc{2},[codeword 1]);else CODE{sc} = char('0'+codeword);end

Page 40: Image  Compression

Huffman Codes(cont.)

>> p = [0.1875 0.5 0.125 0.1875];>> c = huffman(p)

c =

'011' '1' '010' '00'

Page 41: Image  Compression

Huffman Encoding

>> f2 = uint8 ([2 3 4 2; 3 2 4 4; 2 2 1 2; 1 1 2 2])

f2 =

2 3 4 2 3 2 4 4 2 2 1 2 1 1 2 2

>> whos('f2') Name Size Bytes Class

f2 4x4 16 uint8 array

Grand total is 16 elements using 16 bytes

Page 42: Image  Compression

Huffman Encoding(cont.)

>> c = huffman(hist(double(f2(:)),4))c = '011' '1' '010' '00'

>> h1f2 = c(f2(:))'h1f2 =

Columns 1 through 9

'1' '010' '1' '011' '010' '1' '1' '011' '00'

Columns 10 through 16

'00' '011' '1' '1' '00' '1' '1'

Page 43: Image  Compression

Huffman Encoding(cont.)

>> whos('h1f2') Name Size Bytes Class

h1f2 1x16 1018 cell arrayGrand total is 45 elements using 1018 bytes

>> h2f2 = char (h1f2)'

h2f2 =1010011000011011 1 11 1001 0 0 10 1 1

>> whos('h2f2') Name Size Bytes Class

h2f2 3x16 96 char arrayGrand total is 48 elements using 96 bytes

Page 44: Image  Compression

Huffman Encoding(cont.)

>> h2f2 = h2f2(:);>> h2f2(h2f2 == ' ') = [];>> whos('h2f2') Name Size Bytes Class

h2f2 29x1 58 char array

Grand total is 29 elements using 58 bytes

Page 45: Image  Compression

Huffman Encoding(cont.)

>> h3f2 = mat2huff(f2)h3f2 =

size: [4 4] min: 32769 hist: [3 8 2 3] code: [43867 1944]

>> whos('h3f2') Name Size Bytes Class

h3f2 1x1 518 struct array

Grand total is 13 elements using 518 bytes

Page 46: Image  Compression

Huffman Encoding(cont.)

>> hcode = h3f2.code;>> whos('hcode') Name Size Bytes Class

hcode 1x2 4 uint16 array

Grand total is 2 elements using 4 bytes

>> dec2bin(double(hcode))

ans =

10101011010110110000011110011000

Page 47: Image  Compression

Huffman Encoding(cont.)

>> f = imread('Tracy.tif');>> c=mat2huff(f);

>> cr1 = imratio(f,c)

cr1 =

1.2191

>> save SqueezeTracy c;>> cr2 = imratio ('Tracy.tif','SqueezeTracy.mat')

cr2 =

1.2627

Page 48: Image  Compression

Huffman Decoding

function x = huff2mat(y)

% HUFF2MAT decodes a Huffman encoded matrix

if ~isstruct(y) | ~isfield(y,'min') | ~isfield(y,'size') | ~isfield(y,'hist') | ~isfield(y,'code') error('The input must be a structure as returned by MAT2HUFF');end

sz = double(y.size); m=sz(1); n= sz(2);

xmin = double(y.min) - 32768;map = huffman(double(y.hist));

code = cellstr(char('','0','1'));link = [2; 0; 0]; left = [2 3];found = 0; tofind = length(map);

Page 49: Image  Compression

Huffman Encoding(cont.)

(cont.) while length(left) & (found < tofind) look = find(strcmp(map, code {left(1)})); if look link(left(1)) = -look; left = left (2:end); found = found + 1; else len = length (code); link(left(1)) = len + 1; link = [link; 0; 0]; code{end + 1} = strcat(code{left(1)},'0'); code{end + 1} = strcat(code{left(1)},'1'); left = left(2:end); left = [left len + 1 len + 2]; endendx = unravel (y.code',link, m*n); %Decode using C 'unravel'x = x + xmin - 1;x = reshape(x,m,n);

Page 50: Image  Compression

Huffman Encoding(cont.)

(cont.)

#include mex.hvoid unravel (unsigned short *hx, double *link, double *x, double xsz, int hxsz){ int i = 15, j=0, k=0, n=0;

while (xsz - k) {if (*(link + n) > 0){

if((*(hx + j) >> i) & 0x0001)n = *(link + n);else n = *(link + n) - 1;

if (i) i--; else {j++; i=15;}

if( j > hxsz )mexErrMsgTxt("OutOf code bits??");

}

Page 51: Image  Compression

Huffman Encoding(cont.)

(cont.)else {*(x + k++) = -*(link + n);n = 0;}}

if ( k == xsz - 1 )

*(x + k++) = -*(link + n);}

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){

double *link, *x, xsz;unsigned short *hx;int hxsz;

if(nrhs != 3)mexErrMsgTxt("Three inputs required.");

Page 52: Image  Compression

Huffman Decoding(cont.)

(cont.)

else if (nlhs > 1)mexErrMsgTxt("Too many output arguments");

if( !mxIsDouble(prhs[2]) || mxIsComplex(prhs[2]) || mxGetN(prhs[2]) * mxGetM(prhs[2]) != 1 )mexErrMsgTxt("Input XSIZE must be a scalar");

hx = mxGetPr(prhs[0]);link = mxGetPr(prhs[1]);xsz = mxGetScalar(prhs[2]);

hxsz = mxGetM(prhs[0]);plhs[0] = mxCreateDoubleMatrix( xsz, 1, mxREAL);

x = mxGetPr(plhs[0]);unravel(hx, link, x, xsz, hxsz);

}

Page 53: Image  Compression

Consider these two images:

•They have virtually identical histograms

•The histograms are three modal indicating the presence of three dominant grey levels

Interpixel redundancy

Page 54: Image  Compression

Interpixel redundancy (cont.)

Because the gray levels of the image are not equally probable, variable-length coding can be used to reduce the coding redundancy.

Random matches Entropy=7.4253 Compression ratio=1.0704

Aligned matches Entropy=7.3505 Compression ratio=1.0821

•Note that the entropy estimates and the compression ratios of the two images are about the same.

•The variable length coding does not take advantage of the obvious structral relationship between the aligned matches

Page 55: Image  Compression

Interpixel redundancy (cont.)

In order to reduce pixel redundancies, the 2-D pixel array normally used for human viewing and interpretation must be transformed into a more efficient format.

For example the difference between the adjacent pixels can be used to represent an image.

Transformations of this type are referred to as mapping. If the original image can be reconstructed from such transformed set is

referred to as reversible mappings.

Page 56: Image  Compression

Interpixel redundancy (cont.)

The following figure shows a lossless predictive coding.

Page 57: Image  Compression

Interpixel redundancy (cont.)

The following figure shows a lossless predictive coding.

error. prediction:ˆ

integer.nearest the toroundedpredictor theofoutput The:ˆ

imageinput The:

nnn

n

n

ffe

f

f

The decoder reconstructs the prediction error from the received

variable-length code words and performs the inverse operation

nnn fef ˆ

Page 58: Image  Compression

Interpixel redundancy (cont.)

Various methods can be used to generate . In most cases the prediction is formed by a linearcombination

of m previous pixels.

m

iinin ff

1roundˆ

nf̂

where m is the order of the predictor and

are prediction coefficients. For 1-D linear predictive coding this equation can be rewritten

)...,,2,1(, mii

m

iin iyxff

1),(roundˆ

Page 59: Image  Compression

Interpixel redundancy (cont.)

Example: Consider encoding the Figure 8.7(c) using the simple linear predictor

)1,(roundˆ yxffn

nf̂

Page 60: Image  Compression

Psychovisual Redundancy

Psychovisual Redundancy is associated with real or quantifiable visual information.

Its elimination is desirable because the information itself is not essential for normal visual processing.

Page 61: Image  Compression

JPEG compression (cont.)

In transform coding, a reversible, linear transform like DFT or DCT

1...,,2,1/2

01

1...,,2,1/2

01

where

2

)12(cos

2

)12(cos)()(),(),(

1

0

1

0

NvN

v/Nα(v)

MuM

u/Mα(u)

N

vy

M

uxvuyxfvuT

M

x

N

y

is used to map an image into a set of transform coefficients which are then quantized and coded.

Page 62: Image  Compression

JPEG compression (cont.)

One of the most popular and comprehensive compression standard is the JPEG standard.

In the JPEG baseline coding system, which is based on the discrete cosine transform and is adequate for most compression applications.

It is performed in four sequential steps:

Page 63: Image  Compression

JPEG compression (cont.)

Page 64: Image  Compression

JPEG compression (cont.)

Page 65: Image  Compression

JPEG compression (cont.)

After computing the DCT coefficients, they are normalized in accordance with

),(

),(),(ˆ

vuZ

vuTroundvuT

where , u,v=0,1, ...,7 are the resulting normalized and quantized coefficients, T(u,v) DCT of an 8x8 block of image f(x,y) and Z(u,v) is a transform normalization array like that of

Figure 8.12(a)

),(ˆ vuT

Page 66: Image  Compression

JPEG compression (cont.)

After each block’s DCT coefficientsare quantized, the elements of

are recorded in accordance with zigzag pattern of Figure 8.12(b).

Since the resulting on-dimensionally arrayqualitatively arranged according to increasing spatial frequency,the encoder in Figure 8.11(a) is designed to take advantage of long runs of zero that normally result from the reordering.

),(ˆ vuT

Page 67: Image  Compression

JPEG 2000

JPEG 2000 is based on the idea that the coefficients of a transform that decorrelates the pixels of an image can be coded more efficiently than the original pixels themselves. If the transform basis functions-wawelets in the JPEG 2000 case- pack most of the important visual information into a small number of coefficients, the remaining coefficients can be quantized coarsely or truncated to zero with little image distortion.

Page 68: Image  Compression

JPEG 2000

Page 69: Image  Compression

Fractal Image Compression

Page 70: Image  Compression

Resim İçindeki Benzer Parçalar

Range : Küçük bloklar Domain: Büyük bloklar

Page 71: Image  Compression

Görüntünün bloklara bölünmesi

Orjinal

görüntü

4x4 Range

Parçaları

Orjinal 8x8

Domain parçaları

4x4’e indirgenmiş

halleri

Page 72: Image  Compression

Parçaların Benzerliği

domainth i':

rangeth i' :

i

i

iii

d

r

edar

Page 73: Image  Compression

Parçaların Benzerliği-3

Benzerini arayacağımız Range üsttedir.

Domainler’in orijinal halleri ilk kolonda gösterilmiştir.

Regresyon modeline göre düzeltilmiş domainler ise ikinci sütunda gösterilmiştir.

Yandaki rakamlar residual mean square(rms) değerlerini göstermektedir.

Rms değeri en küçük olan domain eldeki range ile eşleştirilir.

Page 74: Image  Compression

Rangeleri Domainler Cinsinden İfade Etmek

Benzer Range-Domainler bulunduktan sonra bunlar bir dosyada saklanarak resim bilgisini oluştururlar.

Örnek resmimiz 320x200x8 = 512,000 bit (64,000 byte) içeriyor. Domain numaraları 0-999 arasında olduğu için 10 bitle ifade

edilebilir. Parlaklık ve konrast tamsayıya çevrilip, 0-15 ve 0-31 aralığında

kuantize edilirse, 4 ve 5 bitle ifade edilebilir. Dolayısıyla, 4000 Range’e karşılık gelen Domain bilgilerini

dosyaya yazarsak :4000*(Domain Numarası+Parlaklık+Kontrast)= 4000*(10+4+5) = 76,000 bit (9,500 byte) olmaktadır.

512/76 = 6.73:1, (512-76)/512 = %85 sıkıştırmak demektir, üstelik %85 sıkıştıran diğer algoritmalardan çok daha iyi bir kalitede !

Page 75: Image  Compression

Görüntü Kalitesi

Decompression, siyah bir ekrandan başlanarak 8-10 iterasyonla gerçekleştirilir.

Resmin kalitesi sonucun orjinale ne kadar yakın olduğuna yani herbir Range-Domain çiftinin ne kadar benzer olduk-larına

bağlıdır.

Page 76: Image  Compression

Decompression: Iteration-1

Page 77: Image  Compression

Decompression: Iteration-2

Page 78: Image  Compression

Decompression: Iteration-3

Page 79: Image  Compression

Decompression: Iteration-10

Page 80: Image  Compression

Aynı Resimden Daha Fazla Domain Elde Etmek : Domain Transformasyonları

Range’e uygun Domaini ararken Domainleri olduğu gibi bırakmayıp transformasyon işlemlerine sokabiliriz.

Orjinal, 90o, 180o ve 270o derece döndürülür.

X eksenine göre yansımaları alınır.

Elde edilen 8 ayrı Domainin negatifi alınarak, bir Domain-den hareketle 16 farklı Domain elde edilir.