MATLAB Answers

huffman encoding for image

17 views (last 30 days)
Nidhi Kumari
Nidhi Kumari on 9 Oct 2018
Commented: Walter Roberson on 26 Oct 2018
for the code-
A=imread('xyz.jpg');
A1=rgb2gray(A);
A = A1(:);
[symbols,p]=hist(A,double(unique(A)))
p=p/sum(p)
symbols=symbols/sum(symbols)
[dict,avglen]=huffmandict(symbols,p)
comp=huffmanenco(A,dict)
i am getting error-
Error using huffmandict (line 164)
Source symbols repeat
Error in new (line 7)
[dict,avglen]=huffmandict(symbols,p)
suggest necessary changes
  3 Comments
KALYAN ACHARJYA
KALYAN ACHARJYA on 10 Oct 2018
@Nidhi
I have updated the answer, please check here

Sign in to comment.

Accepted Answer

OCDER
OCDER on 10 Oct 2018
I haven't used huffmandict before, but based on the document and the error message you have, I suspect your inputs are wrong.
Double check your inputs to huffmandict. Make sure symbols are unique values, and p are the probability of each value. That means:
[symbols, p] = hist(A,double(unique(A))) should be
[p, symbols] = hist(A,double(unique(A))) because the 1st output of hist is the frequency (probability)
and the second output is the unique bin (unique symbol)
  30 Comments
Walter Roberson
Walter Roberson on 26 Oct 2018
I have a memory of having reported an inefficiency to MATLAB having to do with is_a_valid_code, that it was being called far too often. However, I do not seem to find that in my case list, so perhaps I reported it years ago on a different license.
Anyhow: upgrading to R2018a or later would probably speed up quite a bit.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by