Professional Documents
Culture Documents
Lab 8 of COMP 319 Lab 8 of COMP 319: Instruction 2 To The Second Course Project
Lab 8 of COMP 319 Lab 8 of COMP 319: Instruction 2 To The Second Course Project
1
Review of Lab 7
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
5
1. Quantization with a unique value
>> a = imread('onion.png');
>> a1=a(1:8,1:8,1);
( )
>> da1=dct2(double(a1))
da1 =
da
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 =
8
1. Difference computation
>> ra1
ra1=idct2(iqa1);
idct2(iqa1);
>> diff=double(a1)-ra1
diff =
9
2. Quantization with different values:
>> a = imread('onion.png');
>> a1=a(1:8,1:8,1);
( )
>> da1=dct2(double(a1))
da1 =
da
10
2. Quantization with different values:
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 =
14
Quantization + block processing
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
17
Outline of Lab 8
• Q ti ti
Quantization
• Zig-zag scan
• Run-length coding
• Inter-image compression
18
Zig-zag scan
19
A much easier way
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 = 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
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 =
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
28
Why RLE ?
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
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
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
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
35
Modified RLE
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
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
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