Asked by Nidhi Kumari
on 9 Oct 2018

i am using the following code for huffman encoding-

img = imread('xyz.jpg');

Image = rgb2gray(img);

Image = Image(:);

[N M] = size(Image)

Count = zeros(256,1)

for i = 1:N

for j = 1:M

Count(Image(i,j)+1)=Count(Image(i,j)+1)+1;

end

end

prob = Count/(M*N)

symbols = 1:256;

[dict,avglen] = huffmandict(symbols,prob);

comp = huffmanenco(Image,dict);

but i am getting the following error-

Error using huffmanenco (line 86)

The Huffman dictionary provided does not have the codes for all the input signals.

Error in Untitled2 (line 14)

comp = huffmanenco(Image,dict);

please suggest the required changes

Answer by reb rebz
on 22 Mar 2019

img = imread('xyz.jpg');

Image = rgb2gray(img);

Image = Image(:);

[N M] = size(Image)

Count = zeros(256,1)

for i = 1:N

for j = 1:M

Count(Image(i,j)+1)=Count(Image(i,j)+1)+1;

end

end

prob = Count/(M*N)

symbols = 0:255;

[dict,avglen] = huffmandict(symbols,prob);

comp = huffmanenco(Image,dict)

Shapna Rani Sutradhar
on 23 Apr 2019 at 15:09

How can i show and compare the original image and compressed image in matlab with this code?

Islam Abu Tabikh
on 12 May 2019 at 21:15

Did you know how ???

Sign in to comment.

Answer by satyendra kumar
on 23 Mar 2019

i am using the following code for huffman encoding-

clear all

clc

A=imread('cameraman.tif');

A1=double(A(:));

[p,symbols]=hist(A1,unique(A1));

p=p/sum(p);

[dict,avglen] = huffmandict(symbols,p);

comp = huffmanenco(A1,dict);

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Francesco Grauso (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/423105-huffman-encoding-for-image-compression#comment_635145

## Swarnali Sadhukhan (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/423105-huffman-encoding-for-image-compression#comment_701424

## Swarnali Sadhukhan (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/423105-huffman-encoding-for-image-compression#comment_701425

Sign in to comment.