行程编码解码.m

close all;clear all;clc;	%关闭所有图形窗口,清除工作空间所有的变量,清除命令行
I1=imread('e:\role0\lena.jpg');	%读入图像
I2=I1(:);		%将原始图像写成一维的数据并设为I2
I2length=length(I2);	%计算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);	%d(j)代表相应的像素数据
        j=j+1;
        I4(j)=1;
    end
end
data(j)=X(length(X));	%最后一个像素数据赋值给data
I4length=length(I4);	%计算行程编码后的所占字节数,记为I4length
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,512,512);	%重建二位图像数组??
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));	%查看压缩后的图像数据
subplot(121);imshow(I3);	%显示原图的二值图像
subplot(122);inshow(decode_image);	%显示解压缩回复后的图像
disp('压缩比:');
disp(CR);
disp('原图像数据的长度:');
disp(L);
disp('压缩后图像数据的长度');
disp(I4length);
disp('解压缩后的数据长度');
disp(length(decode_image1));

    原文作者:游程编码问题
    原文地址: https://blog.csdn.net/whk100312/article/details/45639803
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞