0% found this document useful (0 votes)
6 views2 pages

21BCE9535 LABresult8 PDF

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

21BCE9535 LABresult8 PDF

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

4/19/24, 12:39 PM untitled567

%....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");

The Lookup Table is:


0 0.0625
0.0625 0.1250
0.1250 0.1875
0.1875 0.2500
0.2500 0.3125
0.3125 0.3750
0.3750 0.5000
0.5000 0.5625
0.5625 0.6250
0.6250 0.6875
0.6875 0.7500
0.7500 0.8750
0.8750 0.9375
0.9375 1.0000

0.9693

Published with MATLAB® R2024a

https://matlab.mathworks.com 2/2

You might also like