0

我在 Matlab 中进行运行长度编码,到目前为止,我已经实现了 zigzag 算法并得到了一个数组 RunLengthCoding:

RunLengthCoding = 
(32, 6, -1, -1, 0, -1, 0, 0, 0, -1, 0, 0, 1, 0, 0,..., 0)

现在我需要运行长度代码这样我得到:

(0,6) (0,-1) (0,-1) (1,-1) (3,-1) (2,1) (0,0)

这是 (length,value),例如 (0,6),因为没有 0,我们的值是 6,那么当我们遇到第一个 0 时,我们得到 (1,-1),因为有一个 0 并且值在它是-1之后。

我的尝试:

RunLengthCoding(1)=[]; %to remove first DC component
relMat = [];
N = 0;
for i = 1:length(RunLengthCoding)-1
     if RunLengthCoding(i)==0;
          if RunLengthCoding(i)==RunLengthCoding(i+1)
               N = N + 1;
          else
              valuecode = RunLengthCoding(i);
              lengthcode =  N;
              relMat = [relMat;  lengthcode valuecode];
              N = 1;
          end
        else
           relMat=[relMat; 0 RunLength(i)];  
    end

我知道这不会运行!但这就是我迄今为止所管理的

4

2 回答 2

1

这应该可以解决问题:

RunLengthCoding = [77   -6  -11 -18 -6  -6  2   -1  4   -1  -1  6   3   1   -1  0   0   0   2   1   2   -1  -1  0   -1  1   0   0   0   0   0   0   -1  0   0   1   0   0   0   0   0   0   0 0 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0];

RunLengthCoding(1)=[]; %to remove first DC component
relMat = [];

i = 0;
while i < length(RunLengthCoding)
    i=i+1;
    N = 0;
    while (RunLengthCoding(i)==0 && i < length(RunLengthCoding)) % keep going as long as we encounter zeros
        N=N+1;
        i=i+1;
    end
    relMat = [relMat N RunLengthCoding(i)]; % output the number of zeros we have encountered and the next value
end

if relMat(end)==0
    relMat(end-1)=0;
end
于 2014-12-03T15:24:32.463 回答
0

您可以不用任何循环来完成整个事情,而不是执行如此复杂的循环,方法如下:

% RunLengthCoding is the input row vector
rlc_m = (RunLengthCoding~=0);
relmat = [diff(find(rlc_m))-1 ; RunLengthCoding([false,rlc_m(2:end)])];

我已将输出放在两行中,但您可以使用relmat(:).'. 我认为这是获得 rlc 的一种更简单的方法。

说明:首先我创建所有非零值的掩码,存储到rlc_m. 然后诀窍如下:非零值(第二行)是输入的元素,我在其中屏蔽了第一个元素。为了获取两个数字之间的 0 的数量,我计算输入数组中非零元素的索引差,减去 1 以严格计算两个索引之间的元素数量。

于 2014-12-04T10:40:15.497 回答