Search code examples
matlabsteganography

Extracted image with steganography is white


I'm trying to insert the baboon image into the lena image using the lsb embedding method. When I try to extract the baboon from the stego image, it shows just a white image.

Can anyone tell me what's wrong with my code and how to fix the problem?

Here is my baboon image.

enter image description here

Here is my lena image.

enter image description here

My embedding code

 file_name='lena.bmp';
 cover_image=imread(file_name);
 [row,col]=size(cover_image);
 %secret image 
 file_name='baboon.bmp';
 secret_image=imread(file_name);
 secret_image=imresize(secret_image,[row,col]);
 stego_image=cover_image;
 for ii=1:row
  for jj=1:col
     stego_image(ii,jj)=bitset(stego_image(ii,jj),1,secret_image(ii,jj));
  end
 end
 imwrite(uint8(stego_image),'Stegoimage.bmp') 

My extraction code

file_name='Stegoimage.bmp';
stego_image=imread(file_name);
[row,col]=size(stego_image);
for ii=1:row
  for jj=1:col
    extracted_image(ii,jj)=bitget(stego_image(ii,jj),1);
  end
end
extracted_image=uint8(255*extracted_image);

Solution

  • The reason you're getting a white image is because the command

    bitset(stego_image(ii,jj),1,secret_image(ii,jj));
    

    is saying "if secretimage(ii,jj) is nonzero set the 1st bit of stego to 1, otherwise if it's zero set it to zero". In other words, this is not doing what you think it's doing. You're encoding the image "Baboon>0" instead (which is indeed a mostly white binary image!).

    Furthermore, I think you can afford to change a few more bits rather than just the least significant, and your concealed image will not look that degraded. Here's a method below that changes the first 4 bits of the Lena image, and leaves the other 4 intact.

    %% Prepare images
    Lena = imread('lena.bmp'); Lena = Lena(:,:,1);
    Baboon = imread('baboon.bmp'); Baboon = Baboon(:,:,1);
    Baboon = imresize(Baboon, size(Lena));
    Baboon = floor(double(Baboon) / 16); % ensure valid integers up to 15 (i.e. 4 bits max)
    
    %% Conceal
    Stego = zeros(size(Baboon));
    for i = 1 : numel(Lena)
      Stego(i) = bitand(16+32+64+128, Lena(i)); % keep bits 5,6,7 and 8 only.
      Stego(i) = bitor(Stego(i), Baboon(i));    % 'add' bits 1,2,3 and 4.
    end
    figure(1); imshow(Stego, []);
    

    enter image description here

    %% Reveal
    Uncovered = zeros(size(Stego));
    for i = 1 : numel(Stego)
      Uncovered(i) = bitand(1+2+4+8, Stego(i)); % keep only the first four bits
    end
    Uncovered = Uncovered * 16; % back to usual uin8 pixel range
    figure(2); imshow(Uncovered, [])
    

    enter image description here

    You have now successfully hidden and retrieved a 4-bit image inside an 8-bit image.
    Note how the Lena image now looks a bit less smooth, as if the colours increase in steeper 'steps'. (which they are! 4bit steps + baboon noise, in fact).
    And also how the baboon image is of lower quality than your original 8-bit image, because it is now a 4-bit image (even though it has been scaled back to 8 bits).

    ALSO, IF YOU LOOK CLOSELY, YOU CAN SPOT THE BABOON'S EYE AS A FAINT SHADOW, JUST ABOVE THE ANGLE OF LENA'S HAT!!!