Difficulty in applying huffman encoding for image compression
Mostrar comentarios más antiguos
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.
Respuestas (2)
KALYAN ACHARJYA
el 10 de Oct. de 2018
Editada: KALYAN ACHARJYA
el 10 de Oct. de 2018
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 comentarios
Nidhi Kumari
el 10 de Oct. de 2018
Editada: Nidhi Kumari
el 10 de Oct. de 2018
KALYAN ACHARJYA
el 10 de Oct. de 2018
Editada: KALYAN ACHARJYA
el 10 de Oct. de 2018
I have updated the answer, please check.
Nidhi Kumari
el 11 de Oct. de 2018
Walter Roberson
el 10 de Oct. de 2018
[symbols,p] = hist(A(:), double(unique(A)));
6 comentarios
Nidhi Kumari
el 10 de Oct. de 2018
Walter Roberson
el 11 de Oct. de 2018
[symbols, idx] = unique(A(:));
counts = accumarray(idx, 1);
p = counts ./ sum(counts);
[dict,avglen]=huffmandict(symbols,p);
comp=huffmanenco(A,dict);
Nidhi Kumari
el 11 de Oct. de 2018
Walter Roberson
el 11 de Oct. de 2018
[symbols, ~, idx] = unique(A(:));
counts = accumarray(idx, 1);
p = counts ./ sum(counts);
[dict,avglen]=huffmandict(symbols,p);
comp=huffmanenco(A(:),dict);
Nidhi Kumari
el 14 de Oct. de 2018
Walter Roberson
el 14 de Oct. de 2018
The output is not an image: it is a double vector containing the values 0 and 1.
Categorías
Más información sobre Denoising and Compression en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!