简体   繁体   English

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

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

I'm implementing this code for JPEG compression, but I get the error 我正在为JPEG压缩实现此代码,但出现错误

??? ??? Error using ==> huffmandict at 97 The symbol and probability vector must have the same length 在97使用==> huffmandict时出错。符号和概率向量的长度必须相同

this is the code, please help: 这是代码,请帮助:

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))';

To ensure that your histc call does count the amount of x values per unique x value call it as 为了确保您的histc调用不会计算每个唯一 x值的x值数量,请将该方法称为

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

Else, if you rimage is binary and your only values are 0 and 255 , histc will return a size(h)=256 size array with lots of zeroes, because xmin:xmax is 0:255=[0 1 2 3 ... 254 255] 否则,如果您的rimage是二进制的,并且您的唯一值为0255 ,则histc将返回带有很多零的size(h)=256大小的数组,因为xmin:xmax0:255=[0 1 2 3 ... 254 255]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM