Skip to content

Commit 1815deb

Browse files
committed
Added codex folder with exercise 3 (incomplete)
1 parent dc52b93 commit 1815deb

File tree

10 files changed

+123
-18
lines changed

10 files changed

+123
-18
lines changed

Codec/codec.m

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
% Part 3. Implementation of a codec
2+
% 1. Load image/lena_gray_512.tif
3+
% Use the double haar transform.
4+
lena_haar = haar_transform_multilevel(lena_gray_512,2);
5+
% Let's see the histogram for each sub-band
6+
% First we divide the matrix into submatrices
7+
% We have 3x3 sub-bands
8+
sub11 = lena_haar(1:128,1:128);
9+
sub12 = lena_haar(1:128,129:256);
10+
sub13 = lena_haar(129:256,1:128);
11+
sub14 = lena_haar(129:256,129:256);
12+
sub21 = lena_haar(1:256,257:512);
13+
sub22 = lena_haar(257:512,1:256);
14+
sub23 = lena_haar(257:512,257:512);
15+
figure
16+
subplot(4,4,1)
17+
imshow(sub11,[0,255])
18+
subplot(4,4,2)
19+
imshow(sub12,[0,255])
20+
subplot(4,4,5)
21+
imshow(sub13,[0,255])
22+
subplot(4,4,6)
23+
imshow(sub14,[0,255])
24+
% Now for the bigger subbands we are going to use bigger subplots
25+
subplot(4,4,[3:4 7:8])
26+
imshow(sub21,[0,255])
27+
subplot(4,4,[9:10 13:14])
28+
imshow(sub22,[0,255])
29+
subplot(4,4,[11:12 15:16])
30+
imshow(sub23,[0,255])
31+
32+
% We are going to use the same subplot distribution to show the histogram
33+
% of each sub image
34+
% Since imhist only uses values from 0 to 1,
35+
% we need to move everything 256 values up (to make the negative numbers)
36+
% possitive and then divide by 512 to obtain values from 0 to 1.
37+
% In this scale, the old 0 is at 0.5, -255 at 0 and 255 at 1.
38+
figure
39+
subplot(4,4,1)
40+
imhist((sub11+255)./512)
41+
subplot(4,4,2)
42+
imhist((sub12+255)./512)
43+
subplot(4,4,5)
44+
imhist((sub13+255)./512)
45+
subplot(4,4,6)
46+
imhist((sub14+255)./512)
47+
% Now for the bigger subbands we are going to use bigger subplots
48+
subplot(4,4,[3:4 7:8])
49+
imhist((sub21+255)./512)
50+
subplot(4,4,[9:10 13:14])
51+
imhist((sub22+255)./512)
52+
subplot(4,4,[11:12 15:16])
53+
imhist((sub23+255)./512)
54+
55+
% After looking at the distribution of data, we should use a non-uniform
56+
% scalar quantizer and with a smaller quantization step around 0, but since
57+
% we only have a uniform scalar quantizer, we will use that.
58+
% No Quantization for the ll part of the image (sub 11)
59+
60+
61+
62+
% Need to count all the appearances of each number
63+
entropies = zeros(7,8);
64+
for i= 7:-1:1
65+
q_sub11 = quantize_matrix(sub11,8);
66+
q_sub12 = quantize_matrix(sub12,i);
67+
q_sub13 = quantize_matrix(sub13,i);
68+
q_sub14 = quantize_matrix(sub14,i);
69+
q_sub21 = quantize_matrix(sub21,i);
70+
q_sub22 = quantize_matrix(sub22,i);
71+
q_sub23 = quantize_matrix(sub23,i);
72+
e_sub11 = shannonEntropy(q_sub11);
73+
e_sub12 = shannonEntropy(q_sub12);
74+
e_sub13 = shannonEntropy(q_sub13);
75+
e_sub14 = shannonEntropy(q_sub14);
76+
e_sub21 = shannonEntropy(q_sub21);
77+
e_sub22 = shannonEntropy(q_sub22);
78+
e_sub23 = shannonEntropy(q_sub23);
79+
q_lena = quantize_matrix(lena_haar,i);
80+
q_lena(1:128,1:128) = q_sub11;
81+
e_lena = shannonEntropy(q_lena);
82+
dict = huffmanDict(q_lena);
83+
sizeMap = containers.Map('KeyType','double', 'ValueType','any');
84+
for row = 1:size(dict,1)
85+
keyCell = dict(row,1);
86+
valueCell = dict(row,2);
87+
code = valueCell{1};
88+
key = keyCell{1};
89+
sizeMap(key) = size(code,2);
90+
end
91+
%SizeMap holds the lengths of all the symbols
92+
% TODO subtitute the symbols by their length and add all the elements
93+
% of the matrix, then divide by the nuelem of the matrix
94+
entropies(i,:) = [e_sub11, e_sub12, e_sub13, e_sub14, e_sub21, e_sub22, e_sub23, e_lena];
95+
end
96+
entropies
97+
98+

Codec/huffmanDict.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function dict = huffmanDict(matrix)
2+
a = unique(matrix);
3+
count = numel(matrix);
4+
out = [a,histc(matrix(:),a)/count];
5+
p = out(:,2);
6+
dict = huffmandict(a,p);
7+
end

Codec/shannonEntropy.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function bitsPerSymbol = shannonEntropy(matrix)
2+
a = unique(matrix);
3+
count = numel(matrix);
4+
out = [a,histc(matrix(:),a)/count];
5+
p = out(:,2);
6+
H = sum(-(p(p>0).*(log2(p(p>0))))); % Shannon entropy formula
7+
bitsPerSymbol = H;
8+
end

Quantization/quantize_matrix.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function quantized_matrix = quantize_matrix(matrix, new_bytesize)
2-
quantized_matrix = zeros(size(matrix,1),size(matrix,2),'uint8');
2+
quantized_matrix = zeros(size(matrix,1),size(matrix,2),'double');
33
for row = 1:size(matrix,1)
44
for col = 1:size(matrix,2)
55
quantized_matrix(row,col) = quantize(matrix(row,col), new_bytesize);

Transformation/haar_reverse.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
end
99

1010
function haar_matrix = revert_haar(matrix)
11-
haar_matrix = zeros(size(matrix,1),size(matrix,2));
11+
haar_matrix = zeros(size(matrix,1),size(matrix,2),'double');
1212
half_col = size(matrix,2)/2;
1313
for row = 1:size(matrix,1)
1414
i = 1;
1515
for col = 1:half_col
16-
haar_matrix(row,i) = matrix(row,col) + matrix(row,col + half_col);
17-
haar_matrix(row,i+1) = matrix(row,col) - matrix(row,col + half_col);
16+
haar_matrix(row,i) = double(matrix(row,col)) + matrix(row,col + half_col);
17+
haar_matrix(row,i+1) = double(matrix(row,col)) - matrix(row,col + half_col);
1818
i = i + 2;
1919
end
2020
end

Transformation/haar_reverse_multilevel.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function haar_matrix = haar_reverse_multilevel(original_matrix, n_transforms)
2-
haar_matrix = original_matrix;
2+
haar_matrix = double(original_matrix);
33
for i = n_transforms-1:-1:0
44
rows = 1:(size(haar_matrix,1)/2^i);
55
columns = 1:(size(haar_matrix,1)/2^i);

Transformation/haar_reverse_multilevel2.m

Lines changed: 0 additions & 8 deletions
This file was deleted.

Transformation/haar_transform.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
end
1313

1414
function haar_matrix = haar_pass(matrix)
15-
haar_matrix = zeros(size(matrix,1),size(matrix,2));
15+
haar_matrix = zeros(size(matrix,1),size(matrix,2),'double');
1616
half_col = size(matrix,2)/2;
1717
for row = 1:size(matrix,1)
1818
i = 1;
1919
for col = 1:2:size(matrix,2)
20-
haar_matrix(row,i) = (double(matrix(row,col))+ double(matrix(row,col+1)))/2;
21-
c = (double(matrix(row,col)) - double(matrix(row,col+1)))/2;
20+
haar_matrix(row,i) = (double(matrix(row,col)) + double(matrix(row,col+1)))/2.0;
21+
c = (double(matrix(row,col)) - double(matrix(row,col+1)))/2.0;
2222
haar_matrix(row,i+half_col) = c ;
2323
i = i + 1;
2424
end

Transformation/haar_transform_multilevel.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function haar_matrix = haar_transform_multilevel(original_matrix, n_transforms)
2-
haar_matrix = original_matrix;
2+
haar_matrix = double(original_matrix);
33
for i = 1:n_transforms
44
rows = 1:size(haar_matrix,1)/i;
55
columns = 1:size(haar_matrix,1)/i;

main.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,4 @@
5555
%function dequantization = dequantize(pixel, delta)
5656
% Operation avg*Pixel+avg changed to:
5757
% dequantization= delta*(double(pixel) + 0.5);
58-
%end
58+
%end

0 commit comments

Comments
 (0)