Matlab计算错误值

Matlab computing wrong values

我正在使用MATLAB 2013a来实现本文的算法An improved method for high hiding capacity based on LSB and PVD

论文中的图3.1描述了嵌入过程,当到达右分支的第5步时,MATLAB计算变量v1v2的值错误。这是我的代码:

counter =1;
for i=1:size(stego,1)
    for j=1:3:size(stego,2)
        %% Step 1: Let the three pixels of a block be Pi, Pi+1, and Pi+2.
        p0 = stego(i,j);
        p1 = stego(i,j+1);
        p2 = stego(i,j+2);
        %% Step 2: Suppose Pi <= Pi+1 and Pi <= Pi+2.
        if (p0 <= p1 && p0 <=p2)
          % some code is here.....
        else
          % Otherwise, the reference pixel is Pi+1, and apply the steps 8 to 12
            % to produce the stego-pixels.
            bits = sm(1, counter:counter+k-1); %sm is a binary string 0011101101011.....
            counter = counter+k;
            g1 = dec2bin(p1,8);
            k_old = bin2dec(g1(1,end-k+1:end));
            g1(1,end-k+1:end) = bits;
            k_new = bin2dec(bits);
            g1 = bin2dec(g1);

        % Compute the difference value kdif = kold ?knew.
        k_diff = k_old - k_new;
        
        % Now, compute P'i+1 using Eq. (3.22).
        if (k_diff > 2^(k-1) && (g1+2^k>=0 && g1+2^k<=255))
            p_1 = g1 + 2^k;
        elseif (k_diff < -2^(k-1) && (g1 - 2^k >=0 && g1 - 2^k <= 255))
            p_1 = g1 - 2^k;
        else
            p_1 = g1;
        end

        v11 = abs(p0 - p_1); %ERROR HAPPENS HERE
        v22 = abs(p2 - p_1); %ERROR HAPPENS HERE

        % REST OF THE CODE ....

(我已经删除了左边分支的代码,因为它没有问题)

在调试我的代码时,matlab 正确地向我显示了等于 64 的 p0p_1 的值等于 66,所以等式 v11 = abs(p0 - p_1); 必须得出 2。但它给出 v11 = 0 .

Here is a screenshot of the values of the variables p0, p_1, and the result of v11 after executing the line while debugging.

那么,这是 Matlab 错误吗?还是我的代码有问题?

在您的调试中,请注意 p0 的类型为 uint8,值为 64,即一个无符号的 8 位整数,而 p_1 是一个 double.当从 uint8 中减去双精度数时,结果将是 uint8,它不能表示负值。 Matlab 无符号整数饱和,因此结果将为零。

我建议将 p0 的类型更改为有符号整数类型(也许 int8 就足够了)。