Search code examples
matlabsecuritydebuggingimage-processingsteganography

Matlab computing wrong values


I am using MATLAB 2013a to implement the algorithm of this paper An improved method for high hiding capacity based on LSB and PVD

Figure 3.1 in the paper describes the embedding procedure, when reaching the 5th step of the right branch, MATLAB computes wrong values for the variables v1 and v2. Here is my code:

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 ....

(I've removed the code of the left branch as it has no problems)

While debugging my code, matlab shows me correctly the values of p0 which is equal to 64 and p_1 which is equal to 66, so equation v11 = abs(p0 - p_1); must yield 2. But it gives 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.

So, is that a Matlab bug? Or there is a problem with my code?


Solution

  • In your debugging, notice that p0 is of type uint8 with value 64, that is, an unsigned 8-bit integer, while p_1 is a double. When subtracting a double from a uint8, the result will be a uint8, which can't represent negative values. Matlab unsigned integers saturate so the result will be zero.

    I'd recommend changing the type of p0 to a signed integer type (maybe just int8 is sufficient).