影像壓縮編碼碼matlab實現——行程編碼

fpga&matlab發表於2020-12-24

function [zipped, info] = RLEncode(vector)
[m, n] = size(vector);
%vector = vector(:)';
vector = uint8(vector(:));
L = length(vector);
c = vector(1);
e(1, 1) = c;     %e(:, 1)存放灰度
e(1, 2) = 0;     %e(:, 2)存放行程
t1 = 1;
for j = 1: L
    if((vector(j) == c))
        e(t1, 2) = double(e(t1,2)) + 1;
    else
        c = vector(j);
        t1 = t1 + 1;
        e(t1, 1) = c;
        e(t1, 2) = 1;
    end
end
zipped = e;
info.rows = m;
info.cols = n;
[m, n] = size(e);
info.ratio = m*n/(info.rows * info.cols);

 

clear all;
I = imread('cameraman.tif');
I = im2bw(I, 0.4);
[zipped, info] = RLEncode(I);
unzipped = RLEdecode(zipped, info);
%顯示原始影像和經編解碼後的影像,顯示壓縮比,並計算均方根誤差得erms=0,
%表示RLE是無失真編碼。
I2 = logical(unzipped);
subplot(121);imshow(I);
subplot(122);imshow(I2);
erms = compare(I, I2)
cr = info.ratio
whos I unzipped zipped

 

function unzipped = RLEdecode(zip, info)
zip = uint8(zip);
[m, n] = size(zip);
unzipped = [];
for i = 1: m
    section = repmat(zip(i, 1), 1, double(zip(i, 2)));
    unzipped = [unzipped section];
end
unzipped = reshape(unzipped, info.rows, info.cols);

相關文章