Skip to content

Commit 6630245

Browse files
authored
Merge pull request #1 from yolanda93/iuya
Merge between master and iuya branch
2 parents 326c44a + 1815deb commit 6630245

19 files changed

+267
-91
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

Documents/TD2016.pdf

238 KB
Binary file not shown.

Quantization/dequantization.m

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

Quantization/dequantize_matrix.m

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
% dequantize_matrix:
2+
% Uniformly proyects a matrix with a number of levels (2^old_bytesize)
3+
% into a new matrix with a number of levels (2^new_bytesize)
4+
%
5+
% Correct behaviour only when:
6+
% new_bytesize >= old_bytesize
7+
function dequantized_matrix = dequantize_matrix(matrix, old_bytesize, new_bytesize)
8+
delta = 2^(new_bytesize-old_bytesize);
9+
dequantized_matrix = zeros(size(matrix,1),size(matrix,2),'uint8');
10+
for row = 1:size(matrix,1)
11+
for col = 1:size(matrix,2)
12+
dequantized_matrix(row,col) = dequantize(matrix(row,col), delta);
13+
end
14+
end
15+
end
16+
17+
function dequantization = dequantize(pixel, delta)
18+
% Operation avg*Pixel+avg changed to:
19+
dequantization = delta*(double(pixel) + 0.5);
20+
end

Quantization/quantization.m

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

Quantization/quantization2.m

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

Quantization/quantize_matrix.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function quantized_matrix = quantize_matrix(matrix, new_bytesize)
2+
quantized_matrix = zeros(size(matrix,1),size(matrix,2),'double');
3+
for row = 1:size(matrix,1)
4+
for col = 1:size(matrix,2)
5+
quantized_matrix(row,col) = quantize(matrix(row,col), new_bytesize);
6+
end
7+
end
8+
end
9+
10+
function quantization = quantize(pixel, new_bytesize)
11+
dif_levels = 2^(8-new_bytesize);
12+
quantization=floor(double(pixel)/dif_levels);
13+
end

Test-images/cameraman.tif

257 KB
Binary file not shown.

Test-images/lena_gray_512.tif

256 KB
Binary file not shown.

Test-images/mandril_gray.tif

257 KB
Binary file not shown.

Transformation/ HaarWavelet.m

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

Transformation/ReverseHaar.m

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

Transformation/haar_reverse.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
% haar_reverse:
2+
% Given an input haar_transform image of 256 levels, undo the transform.
3+
function haar_matrix = haar_reverse(original_matrix)
4+
temp_matrix = transpose(original_matrix);
5+
temp_matrix = revert_haar(temp_matrix);
6+
temp_matrix = transpose(temp_matrix);
7+
haar_matrix = revert_haar(temp_matrix);
8+
end
9+
10+
function haar_matrix = revert_haar(matrix)
11+
haar_matrix = zeros(size(matrix,1),size(matrix,2),'double');
12+
half_col = size(matrix,2)/2;
13+
for row = 1:size(matrix,1)
14+
i = 1;
15+
for col = 1: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);
18+
i = i + 2;
19+
end
20+
end
21+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function haar_matrix = haar_reverse_multilevel(original_matrix, n_transforms)
2+
haar_matrix = double(original_matrix);
3+
for i = n_transforms-1:-1:0
4+
rows = 1:(size(haar_matrix,1)/2^i);
5+
columns = 1:(size(haar_matrix,1)/2^i);
6+
haar_matrix(rows,columns) = haar_reverse(haar_matrix(rows,columns));
7+
end
8+
end

Transformation/haar_transform.m

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
% haar_transform:
2+
% Given an input greyscale image of 256 levels, apply the harr transform
3+
% to it.
4+
% Will apply the transform first to the rows and then to the columns
5+
% Parameteres:
6+
% matrix: the image to be transformed
7+
function haar_matrix = haar_transform(original_matrix)
8+
temp_matrix = haar_pass(original_matrix);
9+
temp_matrix = transpose(temp_matrix);
10+
temp_matrix = haar_pass(temp_matrix);
11+
haar_matrix = transpose(temp_matrix);
12+
end
13+
14+
function haar_matrix = haar_pass(matrix)
15+
haar_matrix = zeros(size(matrix,1),size(matrix,2),'double');
16+
half_col = size(matrix,2)/2;
17+
for row = 1:size(matrix,1)
18+
i = 1;
19+
for col = 1:2:size(matrix,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;
22+
haar_matrix(row,i+half_col) = c ;
23+
i = i + 1;
24+
end
25+
end
26+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function haar_matrix = haar_transform_multilevel(original_matrix, n_transforms)
2+
haar_matrix = double(original_matrix);
3+
for i = 1:n_transforms
4+
rows = 1:size(haar_matrix,1)/i;
5+
columns = 1:size(haar_matrix,1)/i;
6+
haar_matrix(rows,columns) = haar_transform(haar_matrix(rows,columns));
7+
end
8+
end

main.m

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
% figure 1
2+
%for i = 1:8
3+
% temp = quantize_matrix(lena_gray_512,i);
4+
% subplot(2,4,i,'replace')
5+
% imshow(temp,[0,2^(i)-1]);
6+
%end
7+
8+
%figure 2
9+
%for i = 1:8
10+
% temp = quantize_matrix(lena_gray_512,i);
11+
% temp2 = dequantize_matrix(temp,i,8);
12+
% subplot(2,4,i,'replace')
13+
% imshow(temp2);
14+
%end
15+
16+
%zeros(8,N,M)
17+
18+
%for j = 1:8
19+
% output=zeros(256,1);
20+
% delta = 2^(8-j);
21+
% for i = 0:255
22+
% output(i+1,1) = dequantize(quantize(i,j),delta);
23+
% end
24+
% subplot(2,4,j,'replace')
25+
% plot(0:255,output(:,1));
26+
% axis([0,255,0,255])
27+
%end
28+
29+
%for j = 1:8
30+
% output=zeros(256,1);
31+
% for i = 0:255
32+
% output(i+1,1) = quantize(i,j);
33+
% end
34+
% subplot(2,4,j,'replace')
35+
% plot(0:255,output(:,1));
36+
% axis([0,255,0,2^(j)])
37+
%end
38+
39+
%results = zeros(8,1);
40+
%for i = 1:8
41+
% temp = dequantize_matrix(quantize_matrix(lena_gray_512,i),i,8);
42+
% D = abs(lena_gray_512-temp).^2;
43+
% MSE = sum(D(:))/numel(lena_gray_512);
44+
% results(i,1) = MSE;
45+
%end
46+
%plot(1:8,results);
47+
48+
49+
50+
%function quantization = quantize(pixel, new_bytesize)
51+
% dif_levels = 2^(8-new_bytesize);
52+
% quantization=floor(double(pixel)/double(dif_levels));
53+
%end
54+
55+
%function dequantization = dequantize(pixel, delta)
56+
% Operation avg*Pixel+avg changed to:
57+
% dequantization= delta*(double(pixel) + 0.5);
58+
%end

0 commit comments

Comments
 (0)