huffmandict error 符号和概率向量的长度必须相同

huffmandict error The symbol and probability vector must have the same length

我正在为 JPEG 压缩实现此代码,但出现错误

???在 97 使用 ==> huffmandict 时出错 符号和概率向量必须具有相同的长度

这是代码,请帮忙:

function y = mat2huff(x)
y.size = uint32(size(x));
x = round(double(x));
x = unique(x)
xmin = min(x(:));
xmax = max(x(:));
pmin = double(int16(xmin));
pmin = uint16(pmin+32768);
y.min = pmin;
x = x(:)';

h = histc(x, xmin:xmax);
if max(h) > 65535
    h = 65535 * h / max(h);
end
[map , w] =  huffmandict(x,h);   
hx = map(x(:) - xmin + 1);           % Map image
hx = char(hx)';                      % Convert to char array
hx = hx(:)';
hx(hx == ' ') = [ ];                 % Remove blanks
ysize = ceil(length(hx) / 16);       % Compute encoded size
hx16 = repmat('0', 1, ysize * 16);   % Pre-allocate modulo-16 vector
hx16(1:length(hx)) = hx;             % Make hx modulo-16 in length
hx16 = reshape(hx16, 16, ysize);     % Reshape to 16-character words
hx16 = hx16' - '0';                  % Convert binary string to decimal
twos = pow2(15 : - 1 : 0);
y.code = uint16(sum(hx16 .* twos(ones(ysize ,1), :), 2))';

为了确保您的 histc 调用确实计算了每个 unique x 值的 x 值的数量,将其称为

h=histc(x,linspace(xmin,xmax,numel(unique(x(:))));

否则,如果你的 rimage 是二进制的并且你的唯一值是 0255histc 将 return 一个 size(h)=256 大小的数组有很多的零,因为 xmin:xmax0:255=[0 1 2 3 ... 254 255]