% RLE: Run-Length Encoding
clc, close all, clear
img_w = int32(640);
img_h = int32(480);
h_beg = int32(img_h / 8)
h_end = int32(img_h / 8 * 7)
w_beg = int32([img_w / 9, img_w / 9 * 3, img_w / 9 * 5, img_w / 9 * 7])
w_end = int32([img_w / 9 * 2, img_w / 9 * 4, img_w / 9 * 6, img_w / 9 * 8])
imgdata = uint8(zeros(img_h, img_w));
imgdata(h_beg : h_end, w_beg(1) : w_end(1)) = intmax('uint8');
imgdata(h_beg : h_end, w_beg(2) : w_end(2)) = uint8(intmax('uint8') / 4 * 3);
imgdata(h_beg : h_end, w_beg(3) : w_end(3)) = uint8(intmax('uint8') / 2);
imgdata(h_beg : h_end, w_beg(4) : w_end(4)) = uint8(intmax('uint8') / 4);
imshow(imgdata)
% encode
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
% decode
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
% 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.468秒
bw_gray.bmp 压缩比例 0.010403
对bw_gray.bmp进行编码、解码,花费总时间为 0.4212秒
rice_logical.bmp 压缩比例 0.12939
对rice_logical.bmp进行编码、解码,花费总时间为 2.73秒
rice_gray.bmp 压缩比例 1.882
对rice_gray.bmp进行编码、解码,花费总时间为 6.24秒
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