MATLAB Answers

Could someone help me with the errors I'm facing in this code ? The code is for Image Compression using Huffman Coding

12 views (last 30 days)
Vincent Abraham
Vincent Abraham on 3 Dec 2020
clc;
clear all;
close all;
base = 2; % For base (b) Huffman Coding
%% READING THE TEXT FILE
img = imread('chair.png');
img1 = double(img(:));
[M, N] = size(img1);
%% HUFFMAN CODING
[sym_prob,sym] = hist(img1,unique(img1));
sym_prob = sym_prob/sum(sym_prob);
entropy = - sum(sym_prob .*(log10(sym_prob)/log10(base)));
disp(['The entropy of the file is: ' num2str(entropy)]);
% Creating the dictionary corresponding to each used symbol
[dict,avglen]=huffmandict(sym,sym_prob, base);
disp(['The average length of the Huffman code is: ' num2str(avglen)]);
disp(['The efficiency of the Huffman code is: ' num2str(entropy/avglen*100) '%']);
disp(['The redundancy of the Huffman code is: ' num2str((1 - (entropy/avglen))*100) '%']);
fprintf('\n')
enco = huffmanenco(img1, dict); % Encoding the data
%% STORING & DISPLAYING THE COMPRESSED DATA
huff_im=bin2dec(enco');
huff_im=reshape(huff_im,M,N);
huff_im=uint8(huff_im);
imwrite(huff_im, 'compressed_img.png')
imshow(huff_im)

  0 Comments

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!