21BCE9535 LABresult8 PDF
21BCE9535 LABresult8 PDF
%....21BCE9535....%
%...MAHIMALURU ACHYUTH...%
clc
clear all;
close all;
%...............Image Compression - Arithmetic Coding...............%
I = imread("cameraman.tif");
I = imresize(I,[4 NaN]);
str = reshape(I,1,[]);
u = unique(str);
len = length(str);
len_unique = length(u);
z = zeros(1,len_unique);
p = zeros(1,len_unique);
for i=1:len_unique
z(i) = length(strfind(str,u(i)));
p(i) = z(i)/len;
end
cpr = cumsum(p);
newcpr = [0 cpr];
table = zeros(len_unique,2);
for i = 1:len_unique
table(i,1) = newcpr(i);
table(i,2) = cpr(i);
end
disp("The Lookup Table is: ");
disp(table);
UL = 1;
LL = 0;
table_mod = table;
for i=1:len
for j=1:len_unique
if str(i)==u(j)
pos = j;
UL = table_mod(pos,2);
LL = table_mod(pos,1);
diff_range = UL - LL;
table_mod(:,2) = LL + (diff_range .* table(:,2));
table_mod(:,1) = LL + (diff_range .* table(:,1));
break;
end
end
end
tag = LL;
disp(tag);
str = [];
UL = 1;
LL = 0;
table_mod = table;
for i=1:len
for j=1:len_unique
if tag>=table_mod(j,1) && tag<table_mod(j,2)
pos = j;
pos = j;
UL = table_mod(pos,2);
LL = table_mod(pos,1);
diff_range = UL - LL;
table_mod(:,2) = LL + (diff_range .* table(:,2));
table_mod(:,1) = LL + (diff_range .* table(:,1));
decoded_str = u(pos);
https://matlab.mathworks.com 1/2
4/19/24, 12:39 PM untitled567
str = horzcat(str,decoded_str);
break;
end
end
end
DD = uint8(str);
Restore = reshape(DD,size(I,1),size(I,2));
figure
subplot(1,2,1),imshow(I),title("Original Image");
subplot(1,2,2),imshow(Restore),title("Restored Image");
0.9693
https://matlab.mathworks.com 2/2