Matlab-數字圖像編碼實驗-無損編碼/壓縮算法實驗
代碼鏈接:https://download.csdn.net/download/qq_43571150/12033273
問題
實現哈夫曼壓縮, 計算原圖和壓縮以後的尺寸,計算壓縮率並比較分析
結果👇
Matlab代碼👇
clear;
clear all;
A=imread('01.jpg');
I=rgb2gray(A);
[M,N] = size(I);
I1 = I(:);
P = zeros(1,256);
%獲取各符號的概率;
for i = 0:255
P(i+1) = length(find(I1 == i))/(M*N);
end
k = 0:255;
dict = huffmandict(k,P); %生成字典
enco = huffmanenco(I1,dict); %編碼
deco = huffmandeco(enco,dict); %解碼
Ide = col2im(deco,[M,N],[M,N],'distinct'); %把向量重新轉換成圖像塊;
decolength=length(deco);
encolength=length(enco);
subplot(1,2,1);imshow(I);title('原圖');
subplot(1,2,2);imshow(uint8(Ide));title('壓縮');
OB=encolength;
sumcode=decolength;
CR=sumcode/OB;
disp(['原圖 Bit: ',num2str(OB),' bit']);
disp(['壓縮圖像 Bit: ',num2str(sumcode),' bit']);
disp(['壓縮率: ',num2str(CR)]);