Difficulty in applying huffman encoding for image compression

I got the given code to apply Huffman Encoding for image compression. But i am getting the error-
Error using huffmandict (line 71)
The symbol input must be a vector
Error in new (line 5)
[dict,avglen]=huffmandict(symbols,p)
The code is:
A=imread('xyz.jpg');
[symbols,p]=hist(A,double(unique(A)))
p=p/sum(p)
[dict,avglen]=huffmandict(symbols,p)
comp=huffmanenco(A,dict)
Help me as i am new to all these concepts.

回答 (2 件)

KALYAN ACHARJYA
KALYAN ACHARJYA 2018 年 10 月 10 日
編集済み: KALYAN ACHARJYA 2018 年 10 月 10 日

0 投票

When I have tested your code with the test image, both symbols and p have different sizes (see in the attached image) Read the documentation about huffmandict Clearly mentioned that-
The length of p must equal the length of symbols.
-------------------------------------------------------------------------------------------------------------------------------------------- Updated Answer
I have suspected another issue if you consider any 1 D array, the following code is working (Note that an image is 2D)
%A=double(imread('test.jpg'));
p=[.5 .125 .125 .125 .0625 .0625];
A=randsrc(100,1,[1:6; p]);
%[symbols,p]=hist(A,double(unique(A)));
[p,symbols]=hist(A,double(unique(A)));
p=p/sum(p);
[dict,avglen]=huffmandict(symbols,p);
comp=huffmanenco(A,dict);

3 件のコメント

Nidhi Kumari
Nidhi Kumari 2018 年 10 月 10 日
編集済み: Nidhi Kumari 2018 年 10 月 10 日
Now i have converted the image to vetor ,but new error is coming -
Source symbols repeat
Also the new 'p' is 256x1 double and 'symbols' is 1x256 double. What should i do now?
KALYAN ACHARJYA
KALYAN ACHARJYA 2018 年 10 月 10 日
編集済み: KALYAN ACHARJYA 2018 年 10 月 10 日
I have updated the answer, please check.
Have you read here?
Nidhi Kumari
Nidhi Kumari 2018 年 10 月 11 日
I am using R2014a ,so histcounts() is not present. Can you suggest any other alternative?

サインインしてコメントする。

Walter Roberson
Walter Roberson 2018 年 10 月 10 日

0 投票

[symbols,p] = hist(A(:), double(unique(A)));

6 件のコメント

Nidhi Kumari
Nidhi Kumari 2018 年 10 月 10 日
Now new error is coming-
Source symbols repeat
Walter Roberson
Walter Roberson 2018 年 10 月 11 日
[symbols, idx] = unique(A(:));
counts = accumarray(idx, 1);
p = counts ./ sum(counts);
[dict,avglen]=huffmandict(symbols,p);
comp=huffmanenco(A,dict);
Nidhi Kumari
Nidhi Kumari 2018 年 10 月 11 日
Error-
The symbol and probability vector must have the same length
Walter Roberson
Walter Roberson 2018 年 10 月 11 日
[symbols, ~, idx] = unique(A(:));
counts = accumarray(idx, 1);
p = counts ./ sum(counts);
[dict,avglen]=huffmandict(symbols,p);
comp=huffmanenco(A(:),dict);
Nidhi Kumari
Nidhi Kumari 2018 年 10 月 14 日
The output image is a vertical thin line along with the message-
Warning: Image is too big to fit on screen; displaying at 0%
Walter Roberson
Walter Roberson 2018 年 10 月 14 日
The output is not an image: it is a double vector containing the values 0 and 1.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeDenoising and Compression についてさらに検索

質問済み:

2018 年 10 月 9 日

コメント済み:

2018 年 10 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by