Matlab下RLE算法的简单实现
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
游程长度编码
编码函数:
function ret_dat = rle_encode(in_dat)
% Run length encoding
% PCX is a kind of image type in which RLE is widely used.
% Compress an image by using run-length-encoding algorithm.
% BY: mayadong7349 2011-12-10
ret_dat = int32([]);
if ndims(in_dat) == 3, % RGB is given
error('Unsupported image type.');
end; % nothing to do for intensity image, indexed image
in_dat = in_dat';
in_dat = in_dat(:);
len = length(in_dat);
c = 1;
while c <= len,
pix_dat = in_dat(c);
count = 0;
while (c <= len) && (in_dat(c) == pix_dat),
count = count + 1;
c = c + 1;
end;
ret_dat = [ret_dat, pix_dat, count];
end;
end% end of function rle_encode
解码函数:
function ret_dat = rle_decode(in_dat, lines, cols, dat_type)
% Run length decoding
% Uncompress an image which is encoded by using run-length-encoding algorithm.
% BY: mayadong7349 2011-12-10
ret_dat = int32([]);
[height, width] = size(in_dat);
if height ~= 1,
error('Unsupported input data type.');
elseif mod(width, 2) ~= 0,
error('Unsupported input data type.');
end;
if ~strcmp(dat_type, 'uint8') && ~strcmp(dat_type, 'logical'), error('Cannot recognise the input data type.');
end
c = 1;
for index = 1:2:width
pix_count = in_dat(index + 1);
for n = 1:pix_count
ret_dat(c) = in_dat(index);
c = c + 1;
end
end
ret_dat = reshape(ret_dat, cols, lines);
ret_dat = ret_dat';
switch dat_type
case'uint8'
ret_dat = uint8(ret_dat);
case'logical'
ret_dat = logical(ret_dat);
end
end% end of function rle_decode
测试脚本:
% example
close all; clear; clc;
warning off all;
for count = 1:4,
if count == 1,
filename = 'bw_logical.bmp';
elseif count == 2,
filename = 'bw_gray.bmp';
elseif count == 3,
filename = 'rice_logical.bmp';
elseif count == 4,
filename = 'rice_gray.bmp';
end;
[img_data, map] = imread(filename);
[h, w] = size(img_data);
time_elapsed = cputime;
arr_encode = rle_encode(img_data);
img_data_decode = rle_decode(arr_encode, h, w, class(img_data)); time_elapsed = cputime - time_elapsed;
figure; imshow(img_data, map);
figure; imshow(img_data_decode, map);
drawnow;
disp([filename, ' 压缩比例', num2str( length(arr_encode) / length(img_data(:)) )] );
disp(['对', filename, '进行编码、解码,花费总时间为', num2str(time_elapsed), '秒']);
end;
%
a = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0; ...
0, 0, 0, 1, 1, 1, 1, 1, 1, 1; ...
1, 1, 1, 1, 1, 1, 0, 0, 0, 0];
a = logical(a)
[h, w] = size(a);
a_encode = rle_encode(a)
a_decode = rle_decode(a_encode, h, w, class(a));
disp(['压缩比例 ', num2str( length(a_encode) / length(a(:)) )] );
测试用图:
bw_gray.bmp bw_logical.bmp
rice_logical.bmp
rice_gray.bmp
输出结果:
bw_logical.bmp 压缩比例0.010403
对bw_logical.bmp进行编码、解码,花费总时间为0.4836秒
bw_gray.bmp 压缩比例0.010403
对bw_gray.bmp进行编码、解码,花费总时间为0.4836秒
rice_logical.bmp 压缩比例0.12939
对rice_logical.bmp进行编码、解码,花费总时间为 3.3228秒
rice_gray.bmp 压缩比例1.882
对rice_gray.bmp进行编码、解码,花费总时间为13.2445秒
a =
0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 1 1 1 1
1 1 1 1 1 1 0 0 0 0
a_encode =
0 13 1 13 0 4
压缩比例0.2
结论:
RLE算法作为一种无损压缩算法,对于那些图片像素值分块分布的图像的压缩比很高;而对于像素值分布并不是那么规律的图片,RLE算法不仅达不到压缩数据的目的,反而增大了数据量。
由于大多数图片都比较复杂,所以单纯使用RLE算法对图片进行压缩的图片格式并不多见(PCX文件是其中之一)。
因此RLE算法通常应用于对灰度图象和索引图像的压缩编码(在jpeg 编码过程中则使用了RLE算法)。