2015-05-19 90 views
1

我執行JPEG壓縮這個代碼,但我得到的錯誤huffmandict錯誤符號和概率向量必須具有相同的長度

???使用錯誤==> huffmandict在97 符號和概率向量必須具有相同的長度

這是代碼,請大家幫忙:

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

是'大小(x)的大小==(H)'? –

+0

使用[MATLAB的調試器](http://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html#brqxeeu-177)遍歷代碼並檢查數據的大小。 – excaza

+0

這是問題,它們不是平等的,如果它們不相等,我該如何調用這個函數! – user3818372

回答

0

爲了確保您的通話histc也算x量每獨特x值值稱其爲

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

否則,如果你是RIMAGE二進制和你唯一的值是0255histc將返回一個size(h)=256尺寸數組與許多零,因爲xmin:xmax0:255=[0 1 2 3 ... 254 255]

+0

它的工作,但現在我得到的錯誤: ???錯誤使用==> huffmandict at 91 輸入符號的概率不能大於1 – user3818372

+0

請閱讀函數文檔並理解算法!你想要的概率,所以你需要正常化h! 'H = H /總和(H)'。 –

+0

好的我會..謝謝你:)) – user3818372

相關問題