我有运行长度编码的matlab代码,我想制作解码代码

时间:2017-06-19 20:56:30

标签: matlab image-compression decoder run-length-encoding

我有运行长度编码的Matlab代码,我想制作解码代码。请任何人帮我制作这段代码的解码器吗?

编码器如下:

function out = rle (image)
%
% RLE(IMAGE) produces a vector containing the run-length encoding of
% IMAGE, which should be a binary image. The image is set out as a long
% row, and the conde contains the number of zeros, followed by the number
% of ones, alternating.
%
% Example:
%
% rle([1 1 1 0 0;0 0 1 1 1;1 1 0 0 0])
%
% ans =
%
% 03453
%
    level = graythresh(image);
    BW    = im2bw(image, level);
    L     = prod(size(BW));
    im    = reshape(BW.', 1, L);
    x     = 1;
    out   = [];
    while L ~= 0,
        temp = min(find(im == x));
        if isempty(temp)
            out = [out, L];
            break;
        end
        out = [out, temp-1];
        x   = 1 - x;
        im  = im(temp : L);
        L   = L - temp + 1;
    end
end

2 个答案:

答案 0 :(得分:2)

Matlab有一个用于游程解码的内置函数,即repelem(从R2015a开始)。您可以使用包含原始值(在您的情况下为01)的向量以及包含游程长度的向量来提供它。

x = [0 3 4 5 3]成为输入。然后,

y = repelem(mod(0:numel(x)-1, 2), x)

给出

y =
     1     1     1     0     0     0     0     1     1     1     1     1     0     0     0

根据您的编码函数,它是线性化形式的orinal图像。

答案 1 :(得分:0)

此问题还有一个更短但更复杂的解决方案。您将灰度值矩阵的灰度值和行传递给函数RLE,它会为您提供答案。

function rle = RLE(gray_value, image_rows)
for i =1:image_rows
    diF = diff([gray_value(i,1)-1, gray_value(i,:)]) ;
    k = diff([find(diF), numel(gray_value(i,:))+1]);
    rle=[gray_value(i,find(diF));k] ;
end
end