簡體   English   中英

我有用於游程編碼的matlab代碼,我想制作用於解碼的代碼

[英]I have matlab code for run length encoding and I want to make code for decoding

我有用於游程編碼的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

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

根據您的編碼功能,它是線性化的原始圖像。

對於這個問題,還有一個更簡短但更復雜的解決方案。 您將灰度值和灰度矩陣的行傳遞給函數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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM