0

So here is what I was trying to do. I'm absolutely new to matlab. It has only been a day or so that I've used it and here is a little something that my teacher had asked me to do. Embed statements or group of strings within an image using the LSB Algorithm. The string is to be read from a file. As of now, I've not used any file operations. I'm trying this using one character and I don't know whats wrong. The algo seems simple but my output i.e, both the cover and the steg pixels show the same value. :(

cover=imread('D:\l.jpg');
steg=cover;
l=1;
LSB=0;
height = size (cover, 1);
width = size (cover, 2);
message = 'J' ;
mdec = uint8(message);
mbin = dec2bin(mdec, 8);
mbins= mbin(:);
len=length(mbins);

for  i  = 1:height
for j = 1:width
        if(l<=len)
            LSB = mod(cover(i,j), 2);
            if(mbins(l)==LSB)
                steg(i,j) = cover(i,j);
            else if (mbins(l)~=LSB &&    LSB==1 && mbins(l)==0)
                steg(i,j) = cover(i,j)-1;
            else if (mbins(l)~=LSB &&    LSB==0 && mbins(l)==1)
                steg(i,j) = cover(i,j)+1;

                end
                end
                end
                    l=l+1;  
        end
end

end
imwrite(steg,'D:\hidden.jpg');
%imshow(steg)
cover(1, 1:8)
steg(1, 1:8)
Shai
  • 111,146
  • 38
  • 238
  • 371
Xavier
  • 27
  • 3

1 Answers1

1

Oh, nested loops... that's not the way to go.

You want to replace the least significant bits of the first l pixels with the binary ascii representation of your input string.


First thing that went wrong - converting char to binary:
Converting a character to its binary representation should be done using bitget

>> bitget( uint8('J'), 1:8 )
0    1    0    1    0    0    1    0

Gives back 1-by-8 binary array, while using dec2bin:

>> dec2bin( uint8('J'), 8 ) 
01001010

Gives back 1-by-8 string: the actual numeric values of this array are

>> uint8(dec2bin( uint8('J'), 8 ))
48   49   48   48   49   48   49   48

Can you appreciate the difference between the two methods?

If you insist on using dec2bin, consider

>> dec2bin( uint8('J'), 8 ) - '0'
0     1     0     0     1     0     1     0

Second point - nested loops:
Matlab favors vector/matrix vectorized operations rather than loops.

Here's a nice way of doing it without loops, assuming cover is a gray scale image (that is it has a single color channel rather than 3) of type uint8.

NoLsb = bitshift( bitshift( cover, -1 ), 1 ); %// use shift operation to set lsb to zero 
lsb = cover - NoLsb; %// get the lsb values of ALL pixels
lsb( 1:l ) = mbins; %// set lsb of first l pixels to bits of message
steg = NoLsb + lsb; %// this is it!
Shai
  • 111,146
  • 38
  • 238
  • 371
  • Thank you for pointing that out to me.I think I understand what went wrong in the first part. I needed to subtract the ascii value of 0 from each of the elements of the binary array to get my desired value.. that's 0 or 1. dec2bin(uint8('J')) provides me an array which in essence is a string, but it is something that I do not want. I want the precise values of each of the elements in the array so that their numerical value is 0 or 1. and not anything else.. I guess that is what you what to say? – Xavier Dec 25 '14 at 03:34
  • @Xavier - you got it right. this is what I was trying to explain at the first part of my answer. I believe making this correction to your code will make it work, but I strongly recommend that you look into the second part as well as it is a more "Matlab"-ish approach than a nested loop. – Shai Dec 25 '14 at 06:26
  • Yes. I think I understand the second part of the algorithm. It makes things a lot more easier. I had it tried out using a 3 x 3 matrix to get things cleared and I understand how it works. Thank you very much! – Xavier Dec 26 '14 at 15:31
  • but if that's so easily done.. how would the decoding function be implemented? Assuming that I'd want to do it in this approach – Xavier Dec 27 '14 at 04:55
  • @Xavier you already have the code for extracting the lsb of all the pixels in the image, just convert them to characters. – Shai Dec 27 '14 at 17:19