【问题标题】:I have matlab code for run length encoding and I want to make code for decoding我有用于运行长度编码的 matlab 代码,我想制作用于解码的代码
【发布时间】:2017-06-19 20:56:29
【问题描述】:

我有用于运行长度编码的 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

【问题讨论】:

  • 不要在寻求解决方案的地方提问。而是询问如何获得解决方案并展示您自己的投入。
  • @advise 当您尝试使用代码进行交流时(或者至少表明您为帮助他人阅读它付出了一些努力),格式清晰的代码也是一项重要技能。注意这一点! :)

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


【解决方案1】:

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

根据您的编码函数,这是线性化形式的原始图像。

【讨论】:

  • +1 这是一个很好的答案。但我同意 Nissim 的评论,如果 OP 在提出如此巧妙的解决方案之前表现出一些努力来理解问题所在,那就太好了。
  • @TasosPapastylianou 完全同意。但是答案很简单... :-) 当有内置函数时,最好使用它而不是自己开发(当然,除非是为了练习)
  • @Luis Mendo 是的,但请你帮我把这段代码制作成灰度图像而不是二进制图像
【解决方案2】:

对于这个问题还有一个更短但更复杂的解决方案。您将灰度矩阵的灰度值和行传递给函数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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 2015-12-10
    • 1970-01-01
    • 2012-08-17
    • 2010-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多