matlab 實現行程編碼 對二值圖像進行編解碼

 何爲行程壓縮呢?很簡單:

例如:aaabccccccddeee 可以表示爲 3a1b6c2d3e

行程壓縮對二值圖像的壓縮非常有效

close all;clear all;clc;
% matlab 行程編解碼 二值圖像
I1 = imread('lena.jpg');
I2 = I1(:);
I2length = length(I2);
I3 = im2bw(I1,0.5);  %將原圖像轉換爲二值圖像,閾值爲0.5
X = I3(:);
L = length(X);
j = 1;
I4(1) = 1;
for z=1:1:(length(X)-1)
    if X(z) == X(z+1)
        I4(j) = I4(j) + 1;
    else
        data(j) = X(z);
        j = j+1;
        I4(j) = 1;
    end
end
data(j) = X(length(X));
I4length = length(I4);
CR = I2length/I4length;  %壓縮前與壓縮後的比值
% 行程編碼解壓
l = 1;
for m=1:I4length
    for n=1:1:I4(m)
        decode_image1(l) = data(m);
        l = l+1;
    end
end
decode_image = reshape(decode_image1,300,300);
figure,
x = 1:1:length(X);
subplot(131),plot(x,X(x));
y = 1:1:I4length;
subplot(132),plot(y,I4(y));
u = 1:1:length(decode_image1);
subplot(133),plot(u,decode_image1(u));
figure,
subplot(121),imshow(I3);
subplot(122);imshow(decode_image);
disp('壓縮比:')
disp(CR);
disp('原圖像數據的長度:')
disp(L);
disp('壓縮後圖像數據的長度:')
disp(I4length);
disp('解壓後圖像數據的長度:')
disp(length(decode_image1));

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章