how to construct huffmandict for an image?
Show older comments
A = imread('lena.jpg'); B = rgb2gray(A); count = imhist(B); p = count/numel(B); [dict,avglen] = huffmandict(count,p);
Error is created Symbols must have unique...
1 Comment
Nour Alhuda Damer
on 27 Mar 2015
are you find a solution???? I have the same error
Answers (1)
Thorsten
on 27 Mar 2015
You need to count the occurrences of all unique values; hist does not do that by default, use histu instead:
A = imread('lena.jpg');
B = rgb2gray(A);
[values instances] = histu(B);
p = instances/numel(B);
[dict, avglen] = huffmandict(values,p);
with function histu
function [values instances] = histu(x)
%HISTU Count instances of unique values.
%
% [VALUES INSTANCES] = HISTU(X)
%
%
%Example:
% I = imread('cameraman.tif');
% [unique_values number_of_unique_values] = histu(I(:));
%
%Cut & past of code by Roger Stanford posted on 28 Jun, 2011 19:26:05
%http://www.mathworks.com/matlabcentral/newsreader/view_thread/309839
%
% Thorsten.Hansen@psychol.uni-giessen.de 2015-03-27
y = sort(x(:));
p = find([true;diff(y)~=0;true]);
values = y(p(1:end-1));
instances = diff(p);
Categories
Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!