Download as pdf or txt
Download as pdf or txt
You are on page 1of 40

Lab 8 of COMP 319

Instruction 2 to the second


course project

Lab tutor : Dennis Yang LIU


Email : csygliu@comp.polyu.edu.hk
cs gli @comp pol ed hk
Lab 8 : Nov. 6, 2014

1
Review of Lab 7

• Color image representation in Matlab


• R, G, B components
• Color space transform
• DCT and Inverse DCT in Matlab
• 1-D and 2-D functions
• Sub-function definition
• Direct definition
• Nested definition
• Sub-block processing in Matlab
• B = blkproc(A, [m n], fun)

2
Outline of Lab 8

• Q ti ti
Quantization
• Zig-zag scan
• Run-length coding
• Inter-image compression

3
Outline of Lab 8

• Q
Quantization
ti ti
• Zig-zag scan
• Run-length coding
• Inter-image compression

4
Quantization in Matlab

Two different ways of quantization:

1. Quantization with a unique value:


All elements in a matrix are divided by the same value.

2. Quantization with different values:


All elements in a matrix are divided by different numbers.
There is usually a quantization table for the compression task.

5
1. Quantization with a unique value

>> a = imread('onion.png');
>> a1=a(1:8,1:8,1);
( )
>> da1=dct2(double(a1))

da1 =
da

515.1250 -6.9009 -4.4097 -0.1354 5.6250 0.7751 -1.8265 0.4425


2.2310 -0.9758 -1.7370 -1.8775 -2.6824 -1.3261 1.0516 -1.0729
1 2950
1.2950 1 6965
1.6965 -0.8687
0 8687 -3.0164
3 0164 1 8362 -1.5076
1.8362 1 5076 -0.1527
0 1527 -1.4253
1 4253
2.7060 1.6665 -0.1457 1.5836 -2.0586 -1.2816 -1.3506 -1.4155
-2.6250 3.0794 1.0312 -0.4235 2.8750 0.4794 -1.1036 -0.5436
1.9798 3.9761 0.6547 -2.9155 -1.5988 -0.7426 -1.2419 -0.3764
1.4931 0.1617 2.0973 0.9814 0.1865 -0.4356 0.3687 0.4056
-3.0155 -1.2068 -1.9821 -2.1311 1.4400 -1.4382 0.0831 -1.3652

6
1. Quantization with a unique value

>> qq=4;
4;
>> qa1=round(da1/q)

qa1 =

129 -2 -1 0 1 0 0 0
1 0 0 0 -1 0 0 0
0 0 0 -1 0 0 0 0
1 0 0 0 -1 0 0 0
-1 1 0 0 1 0 0 0
0 1 0 -1 0 0 0 0
0 0 1 0 0 0 0 0
-1 0 0 -1 0 0 0 0

7
1. Inverse Quantization

>> iqa1
iqa1=qa1*q
qa1 q

iqa1 =

516 -88 -44 0 4 0 0 0


4 0 0 0 -4 0 0 0
0 0 0 -4 0 0 0 0
4 0 0 0 -4 0 0 0
-4 4 0 0 4 0 0 0
0 4 0 -4 0 0 0 0
0 0 4 0 0 0 0 0
-44 0 0 -44 0 0 0 0

8
1. Difference computation

>> ra1
ra1=idct2(iqa1);
idct2(iqa1);
>> diff=double(a1)-ra1

diff =

-0.0214 1.4985 -2.1073 1.9375 1.7390 0.0091 -2.1001 -0.1033


-0.8028 -1.8643 -0.0620 0.3465 -1.1458 -2.7719 -2.4730 0.0307
-1.1878 -1.4177 0.7109 0.8063 1.7701 0.8336 -0.3697 -0.0369
-0.7010 0.3393 1.3827 -1.1284 -2.2723 -1.5843 3.1274 0.3276
-0.4347 -0.9688 -1.6189 0.4172 -0.0438 -0.8478 0.1704 -0.1646
1.3119 -0.0713 -0.4372 -0.1528 0.3775 -1.7754 -0.4732 -1.8884
1 6139
1.6139 1 4909
1.4909 -1.5798
1 5798 0 0424
0.0424 0 7297
0.7297 -0.0828
0 0828 00.3430
3430 -0.8146
0 8146
1.5442 0.5652 0.8324 0.7124 0.4122 -0.2308 1.5330 -0.2207

9
2. Quantization with different values:

>> a = imread('onion.png');
>> a1=a(1:8,1:8,1);
( )
>> da1=dct2(double(a1))

da1 =
da

515.1250 -6.9009 -4.4097 -0.1354 5.6250 0.7751 -1.8265 0.4425


2.2310 -0.9758 -1.7370 -1.8775 -2.6824 -1.3261 1.0516 -1.0729
1 2950
1.2950 1 6965
1.6965 -0.8687
0 8687 -3.0164
3 0164 1 8362 -1.5076
1.8362 1 5076 -0.1527
0 1527 -1.4253
1 4253
2.7060 1.6665 -0.1457 1.5836 -2.0586 -1.2816 -1.3506 -1.4155
-2.6250 3.0794 1.0312 -0.4235 2.8750 0.4794 -1.1036 -0.5436
1.9798 3.9761 0.6547 -2.9155 -1.5988 -0.7426 -1.2419 -0.3764
1.4931 0.1617 2.0973 0.9814 0.1865 -0.4356 0.3687 0.4056
-3.0155 -1.2068 -1.9821 -2.1311 1.4400 -1.4382 0.0831 -1.3652

10
2. Quantization with different values:

An 8*8 quantization table for the compression task.

>> q=[16 11 10 16 24 40 51 61;...


12 12 14 19 26 58 60 55;...
14 13 16 24 40 57 69 56;...
56;
14 17 22 29 51 87 80 62;...
18 22 37 56 68 109 103 77;...
24 35 55 64 81 104 113 92;...
49 64 78 87 103 121 120 101;...
72 92 95 98 112 100 103 99];

11
2. Quantization with different values:

>> qa1=round(da1./q)

qa1 =

32 -11 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 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

12
2. Inverse quantization

>> iqa1=qa1.*q
q q q

iqa1 =

512 -11
11 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 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

13
2. Difference computation

>> ra1
ra1=idct2(iqa1);
idct2(iqa1);
>> diff=double(a1)-ra1

diff =

0.9072 4.6168 2.0803 4.3794 1.6206 -1.0803 -2.6168 0.0928


-1.0928 -1.3832 1.0803 2.3794 2.6206 1.9197 0.3832 0.0928
-1.0928 -1.3832 1.0803 1.3794 1.6206 -0.0803 -0.6168 1.0928
2.9072 0.6168 1.0803 1.3794 0.6206 -2.0803 0.3832 -1.9072
-0.0928 -2.3832 -2.9197 1.3794 1.6206 -1.0803 -1.6168 -1.9072
3.9072 1.6168 0.0803 0.3794 2.6206 1.9197 2.3832 -0.9072
-0.0928
0 0928 1 6168
1.6168 1 0803
1.0803 3 3794
3.3794 1 6206
1.6206 -2.0803
2 0803 -1.6168
1 6168 -0.9072
0 9072
2.9072 -2.3832 -1.9197 2.3794 1.6206 -4.0803 -2.6168 0.0928

14
Quantization + block processing

First, you should define a quantization function, e. g. blkq(orig_image).

% Image quantization function


function b = blkq(orig_image)

q=[16 11 10 16 24 40 51 61;...
12 12 14 19 26 58 60 55;...
14 13 16 24 40 57 69 56;...
14 17 22 29 51 87 80 62;...
62
18 22 37 56 68 109 103 77;...
24 35 55 64 81 104 113 92;...
49 64 78 87 103 121 120 101;...
;
72 92 95 98 112 100 103 99];

b = orig_image./q;

15
An example

>> a = imread('onion.png');
>> a1 = a(:,:,1);
>> fun = @dct2;
>> da1 = blkproc(double(a1)
blkproc(double(a1), [8 8],
8] fun);
>> fun = @blkq;
>> qa1 = round(blkproc(da1, [8 8], fun));

16
An example

An inverse quantization function must be predefined.

>> fun = @iblkq;


>> iqa1 = blkproc(qa1
blkproc(qa1, [8 8],
8] fun);
>> fun = @idct2;
>> ra1 = blkproc(iqa1, [8 8], fun);

17
Outline of Lab 8

• Q ti ti
Quantization
• Zig-zag scan
• Run-length coding
• Inter-image compression

18
Zig-zag scan

Why Zig-zag Scan?


 to group low frequency coefficients in top of vector
 maps 8×8 to 1×64
 Zig-zag scan does not have the functionality of
compression itself, but it can help the further processing,
such as the run-length
g coding, g, to achieve higher
g compression
p
ratio

19
A much easier way

% concatenate all rows


% scan the matrix line by line

Z = [];
for i = 1:n
Z = [Z,
[Z A(i , :)];
end

20
A much easier way

>> A = [1 2 3; 4 5 6; 7 8 9]

A=
1 2 3
4 5 6
7 8 9

>> Z = [];
for i = 1:3
Z = [Z
[Z, A(i
A(i, :)];
)]
end

>> Z

Z=

1 2 3 4 5 6 7 8 9

21
Zig-zag scan

>> index = zigzag(3) index

index = 1 2 6
(1,1) (1,2) (1,3)
1 1
1 2
2 1 3 5 7
3 1 (2,1) (2,2) (2,3)
2 2
1 3
4 8 9
2 3
(3,1) (3,2) (3,3)
3 2
3 3

22
Zig-zag scan
Input A: 2D array of size N-by-N
Output Z: 1D vector of size 11-by-N^2,
by N 2, that holds zig-zag
zig zag values of A

function Z=zigzag2dto1d(A)

[r, c] = size(A);

if r ~= c
error('input
error( input array should have equal number of rows and columns
columns'))
end

ind = zigzag(r);
Z []
Z=[];
for k=1:size(ind,1)
Z=horzcat(Z, A(ind(k,1),ind(k,2)));
end

23
Zig-zag scan

>> A = [1 2 3; 4 5 6; 7 8 9];
>> B = zigzag2dto1d(A)
i 2dt 1d(A)

B=

1 2 4 7 5 3 6 8 9

1 2 3

4 5 6

7 8 9

24
De-zigzag scan

Input Z: 1D zig-zag vector


Output A: 2D array, restored from 1D zig-zag vector to original pattern

ffunction A=dezigzag1dto2d(Z)
g g ( )

ind = zigzag(sqrt(length(Z)));

A=[];
for k=1:length(Z)
A( ind(k,1),ind(k,2) )=Z(k);
end

25
De-zigzag scan

>> B =

1 2 4 7 5 3 6 8 9

>> reco_A = dezigzag1dto2d(B)

reco_A =

1 2 3
4 5 6
7 8 9

26
Outline of Lab 8

• Q ti ti
Quantization
• Zig-zag scan
• Run-length coding
• Inter-image compression

27
What is Run Length Encoding (RLE) ?

• Compression technique
– Represents data using value and run length
– Run length is defined as number of consecutive equal
values
RLE
1110011111 130215

Values Run Lengths

28
Why RLE ?

• Useful for compressing data that contains repeated


values
– e.g. output from a filter, many consecutive values are 0.

• Very simple compared with other compression


techniques
• Reversible (Lossless) compression
– decompression is easy

29
Important notes of RLE

• Compression
p effectiveness depends
p on input
p
• Must have consecutive runs of values in order to
maximize compression
– Best case: all values same
• Can represent any length using two values
– Worst case: no repeating values
• Compressed data twice the length of original!!
• Should only be used in situations where we know for
sure have
h repeating
i values
l

30
RLE Algorithm

• Start on the first element of input


• Examine next value
– If same as previous value
• Keep a counter of consecutive values
• Keep examining the next value until a different value or end
of input
p then output
p the value followed byy the counter.
Repeat
– If not same as previous value
• Output the previous value followed by ‘1’
1 (run length.
length
Repeat

31
Matlab Code
function encoded = RLE_encode(input)

my_size = size(input);
length = my_size(2);

run_length = 1;
encoded = [];

for i=2:length
if input(i) == input(i-1)
run_length = run_length + 1;
else
l
encoded = [encoded, input(i-1), run_length];
run_length = 1;
end
end

if length > 1
encoded = [encoded input(i) run_length]; % Add last value and run length to output
else
encoded = [input(1) 1]; % Special case if input is of length 1
end
32
Examples

>> RLE_encode([1
_ ( 0 0 0 0 2 2 2 1 1 3]))
ans =
1 1 0 4 2 3 1 2 3 1

>> RLE_encode([0 0 0 0 0 0 0 0 0 0 0])


ans =
0 11

>> RLE_encode([0 1 2 3 4 5 6 7 8 9])

ans =
0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8
1 9 1

33
Run Length Decoder

function decoded = RLE_decode(encoded)

my_size = size(encoded);
length = my_size(2);

index = 1;
d d d = []
decoded [];
% iterate through the input
while (index <= length)
% get value which is followed by the run count
value = encoded(index);
run_length = encoded(index + 1);
for i=1:run_length
% loop adding 'value' to output 'run_length' times
decoded = [decoded value];
end
% put index at next value element (odd element)
index = index + 2;
end

34
Examples

>> RLE_decode([0 12])


ans =
0 0 0 0 0 0 0 0 0 0 0 0

>> RLE_decode([0 1 1 1 2 1 3 1 4 1 5 1])


ans =
0 1 2 3 4 5

>> RLE_decode(RLE_encode([0 0 3 1 4 4 5 6 10]))


ans =
0 0 3 1 4 4 5 6 10

35
Modified RLE

Zigzag scan result:


(32, 6, -1, -1, 0, –1, 0, 0, 0, -1, 0,
0,, 1,, 0,, 0 …… 0))

Original RLE:
(1,32)(1,6)(2,-1)(1,0)(1,-1)(3,0)(1,-1)(2,0)(1,1)(51,0)

Modified RLE:
(0 32)(0 6)(0 1)(0 1)(1 1)(3 1)(2 1)(0 0)
(0,32)(0,6)(0,-1)(0,-1)(1,-1)(3,-1)(2,1)(0,0)
36
Outline of Lab 8

• Q ti ti
Quantization
• Zig-zag scan
• Run-length coding
• Inter-image compression

37
Subtraction using a reference image

Suppose that image f is the reference image, then we can


perform a for loop:

for i = 1:n
if i ~= f % because image f is the reference image
image i = image i – image f
end
end

38
Subtraction between consecutive images

We also can compute the differences between consecutive


images:

for i = 2:n
image i = image i – image (i-1)
end

39
Summary of Lab 8
• Quantization
 Quantization and de
de-quantization
quantization using a unique value
 Quantization and de-quantization using quantization table

• Zig-zag
g g scan
 A straightforward scan
 Zigzag scan (2-D to 1-D) and De-zigzag scan (1-D to 2-D)

• Run-length coding
 Basic RLC and modified RLC

• I t i
Inter-image compression
i
 Subtraction using one reference image
 Subtraction between consecutive images

40

You might also like