我想在MATLAB中使用霍夫曼編碼來壓縮灰度圖像,並嘗試了下面的代碼。在MATLAB中使用霍夫曼編碼進行灰度圖像壓縮
我使用了tif
格式的大小爲512x512的灰度圖像。我的問題是,壓縮圖像的大小(壓縮碼字的長度)變得比未壓縮圖像的大小大。壓縮比率小於1.
clc;
clear all;
A1 = imread('fig1.tif');
[M N]=size(A1);
A = A1(:);
count = [0:1:255]; % Distinct data symbols appearing in sig
total=sum(count);
for i=1:1:size((count)');
p(i)=count(i)/total;
end
[dict,avglen]=huffmandict(count,p) % build the Huffman dictionary
comp= huffmanenco(A,dict); %encode your original image with the dictionary you just built
compression_ratio= (512*512*8)/length(comp) %computing the compression ratio
%% DECODING
Im = huffmandeco(comp,dict); % Decode the code
I11=uint8(Im);
decomp=reshape(I11,M,N);
imshow(decomp);
哇...差不多3年大聲笑。感謝你的接納。 – rayryeng 2017-08-18 05:38:49