Help with Image compression decoding using Huffman coding
7 views (last 30 days)
Show older comments
I'm trying to compress an image using Huffman coding 'without the help of its built-in functions'.
This is what i've done so far.
clc;
clear all;
global CODE
A1 = imread('fig1.jpg');
A1=rgb2gray(A1);
A1=imresize(A1,[128 128]);
figure(1)
imshow(A1)
[M, N]=size(A1);
A = A1(:);
count = imhist(A);
A=num2cell(A);
p = count / numel(A);
%sum(p);
CODE = cell(length(p), 1);
s = cell(length(p), 1);
for i = 1:length(p) % Generate a starting tree with symbol nodes 1, 2, 3, ... to
% reference the symbol probabilities.
s{i} = i ;
end
while numel(s) > 2
[p, i] = sort(p); % Sort the symbol probabilities
p(2) = p(1) + p(2); % Merge the 2 lowest probabilities
p(1) = []; % and prune the lowest one
s = s(i) ; % Reorder tree for new probabilities
s{2} = {s{1}, s{2}}; % and merge & prune its nodes
s(1) = []; % to match the probabilities
end
makecode(s, [])
for i = 1:numel(CODE)
c = CODE{i};
t = c; c(c=='1') = '0'; c(t=='0') = '1';
CODE{i} = c;
end
CODE;
for n=1:256
index=find(cell2mat(A)==n-1);
ANN(index)=CODE(n);
end
codedim=bin2dec(ANN');
codedim=reshape(codedim,M,N);
codedim=uint8(codedim);
imwrite(codedim, 'fig1-Copy.jpg')
figure(2)
imshow(codedim)
can you please review my code for mistakes? it works and i get a coded image, i tried to use bin2dec to just be able to display the coded image.. now i need to decode this image and calculate the compression ratio? any help please? thanks in advance.
1 Comment
Answers (1)
Rohan Balar
on 2 May 2019
@Ahmed Saeed I tried your code for compression but in this line "makecode(s, [])" there is a error stating undefined function or undefined variable "makecode(s, [])" so please can you help me to resolve this issue
0 Comments
See Also
Categories
Find more on Denoising and Compression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!