Saturday, May 2, 2009

Image Hider 2.0 -MATLAB Program

Hide an image file inside another image file :-high quality version


This is a MATLAB based program. Image hider 2.0 is the second part of Image Hider 1.0. Image hider 2.0 is a high quality version. This software helps you to hide one image (photo) in to another image (photo). Suppose image2 is the image to be hid on image1 and the encoded image is image3. Then all the pixel details present in the image2 were encoded in to the image1. So the quality of the image2 will not be affected. Only two bits in every pixels of the image1 is used for encoding. And so the encoded image do not show any patches of the image2. So others will never suspect that image3 as encoded or it may contain any secrets.


The bits in every pixel of the image2 is split into four and placed in four different locations in image1. So it provides some sort of encryption too. So it will be hard for others to decode the hidden image.It supports formats like JPG, BMP etc



    Example: Encoding section

    Image1.jpg :- image on which the encoding to be done

    Image2: Image to be hid

    Image3: the encoded image

    Example: Decoding section - using the above encoded image

    Decoded image1: extracted image from image3.bmp

    Decoded image2: hidden image in image3.bmp



MATLAB Program:

% Program for hiding an image inside the other

% high quality version

%By vipin.p.nair

% College of Engineering Kallooppara

% Kerala, India

% vipinpn@yahoo.com

%01-may-2009

%*****************************************************

% ---Only two bit in pixel of the image 1 is affected

% ---image2 is split & store in two diagonally opposite quadrants

% Algorithm

% the fist image is resize to double its original size

% logically the image is divided to four partitions

% the bits of the image to be hid is stored in the first image as

% shown below

% |-------------|

% |d7,d6 |d3,d2 |

% |-------------|

% |d1,d0 |d5,d4 |

% |____________|

% ****************program********************

clc; % clear the command window

clear; % clear the workspace

disp(' ');

disp(' ***** IMAGE HIDER 2.0 *****');

disp('___Program for hidimg one image inside the other image___');

disp(' ');

disp('_________________________________________________________');

task = input('---Encode :- 1 \n---Decode :- 2\n Enter your task:');

% select task

if isempty(task)

task=1;

end

if task == 1

% reads two image files

x = imread(input(' Welcome to Encoder\n Enter the first image file name: ','s'));

y = imread(input(' Enter the second image file name: ','s'));

% check compatibility

sx = size(x);

sy = size(y);

%if (sx(1) ~= sy(1))|(sx(2)~=sy(2))

x=imresize(x,[2*sy(1),2*sy(2)]);

% end

%sy=2*sy;

%

% clearing Ist files last two lsb bits & moving IInd files msb bits to lsb bits

x1 = bitand(x,uint8(252));

y1 = bitshift(y,-4);

y1_= bitand(y1,12);

y1_= bitshift(y1_,-2); % y1_ has D6 & D7

y1 = bitand(y1,3); % Y1 HAS D4,D5

% clearing II image's msb bits

y_lsb1 = bitshift(bitand(y,12),-2);

y_lsb2 = bitand(y,3);

% inserting IInd to Ist file

z=x1;

for j=1:sy(2) % y variation

for i=1:sy(1) % x variation

for k=1:3

% IInd quadrent

z(i,j,k) = bitor(x1(i,j,k), y1_(i,j,k));

% IV th quadrent

z(i+sy(1),j+sy(2),k) = bitor(x1(i+sy(1),j+sy(2),k), y1(i,j,k));

% I st quadrent

z(i+sy(1),j,k) = bitor(x1(i+sy(1),j,k), y_lsb1(i,j,k));

% IIIrd quadrent

z(i,j+sy(2),k) = bitor(x1(i,j+sy(2),k), y_lsb2(i,j,k));

end

end

end

% display the first image

figure(1)

image(x);

xlabel(' Ist Image ');

% display IInd image

figure(2);

image(y);

xlabel(' IInd Image ');

% display encoded image

figure(3);

image(z);

xlabel(' Encoded Image ');

% saving file

sav=input('Do you want to save the file y/n [y] ','s');

if isempty(sav)

sav='y';

end

if sav == 'y'

name=input('Enter a name for the encoded image: ','s');

if isempty(sav)

name='encoded_temp';

end

name=[name,'.bmp']; % concatination

imwrite(z,name,'bmp');

end

else

% Decoding encoded image

clear;

z=imread(input(' Welcome to Decoder\n Enter the image file to be decoded:','s'));

sy = size(z)/2; % take the size of input file

% xo is fist file- obtained by clearing lsb bits, yo is IInd file right

% shifting z by 4 bits

xo=bitand(z,uint8(252));

xo=imresize(xo,[sy(1),sy(2)]); % reduce the resolution to half so

%that it becoms the original image's resolution

for j=1:sy(2) % y variation

for i=1:sy(1) % x variation

for k=1:3

zout1(i,j,k) = bitshift(bitand(z(i,j,k),uint8(3)),2);

zout2(i,j,k) = bitand(z(i+sy(1),j+sy(2),k), uint8(3));

zout3(i,j,k) = bitshift(bitand(z(i+sy(1),j,k),uint8(3)),2);

zout4(i,j,k) = bitand(z(i,j+sy(2),k),uint8(3));

end

end

end

zout = bitshift((zout1+zout2),4)+zout3+zout4;

yo = zout;

% display Ist & IInd image from encoded image

figure(4);

image(xo);

xlabel('Ist Decoded Image ');

figure(5);

image(yo);

xlabel('IInd Decoded Image');

% saving file

sav=input('Do you want to save the file y/n [y] ','s');

if isempty(sav)

sav='y';

end

if sav == 'y'

name1=input('Enter a name for the first image: ','s');

name2=input('Enter a name for the second image: ','s');

if isempty(name1)

name1 = 'Ist_temp';

end

if isempty(name2)

name2 = 'IInd_temp';

end

name1 = [name1,'.bmp'];

name2 = [name2,'.bmp'];

imwrite(xo,name1,'bmp');

imwrite(yo,name2,'bmp');

end

end

9 comments:

  1. In recent days i get request from many people to explain in detail this code. i hope following explanation would be helpful

    You have two images: Image A & Image B

    You want to store Image B in Image A.

    when you use imread function you will get 3 dimensional matrix with x,y and Z as {R, G , B}

    Each element in this matrix has 8 bits.

    We shall call this bits as D7,D6, D5,D4, D3,D2 ,D1,D0

    So the idea is to store this bits in image A. For this, as a prior understanding if D0 & D1 bits of an image is removed, it would be hard to notice the difference of the resultant image from the previous image.

    So here we shall remove all D1 & D0 bits of image A. and replace it with D1,D0 bits of Image B or D3,D2 bits of Image B or D5,D4 bits of Image B or D7,D6 bits of Image B.

    But if you want to keep this four sets of bit in Image A, then image A must have space to accommodate all. This is done by resizing the image A to 4 times Image B.


    x1 = bitand(x,uint8(252)); - binary of 252 is 11111100. so doing AND operation on X image (ie IMAGE A) will clear its D0 & D1 bits

    y1 = bitshift(y,-4); - shifting bits four time towards LSB. THis will help to simplify the operation of taking out D7 to D5 bits
    y1_= bitand(y1,12); - AND with 1100 to remove above mentioned D7 & D5
    y1_= bitshift(y1_,-2); % y1_ has D6 & D7 - shifts 2 times towards LSB to take out D7 & D6
    y1 = bitand(y1,3); % Y1 HAS D4,D5 - AND with 11 (binary) to take out D4 & D5 of image B


    below code helps to take out D0, D1, D2 & D3 bits of image B

    % clearing II image's msb bits
    y_lsb1 = bitshift(bitand(y,12),-2);
    y_lsb2 = bitand(y,3);

    Placing the separated bits of Image B into image A. it is moved in this following manner
    % |-------------|
    % |d7,d6 |d3,d2 |
    % |------------ -|
    % |d1,d0 |d5,d4 |
    % |____________|

    Maybe there is difference in quadrant, you may have a look.

    z=x1;
    for j=1:sy(2) % y variation
    for i=1:sy(1) % x variation
    for k=1:3
    % IInd quadrent
    z(i,j,k) = bitor(x1(i,j,k), y1_(i,j,k));
    % IV th quadrent
    z(i+sy(1),j+sy(2),k) = bitor(x1(i+sy(1),j+sy(2),k), y1(i,j,k));
    % I st quadrent
    z(i+sy(1),j,k) = bitor(x1(i+sy(1),j,k), y_lsb1(i,j,k));
    % IIIrd quadrent
    z(i,j+sy(2),k) = bitor(x1(i,j+sy(2),k), y_lsb2(i,j,k));
    end
    end
    end



    If you first time using MATLAB, I suggest you to use the debugger option (click on left of code in editor to put breakpoints (red circle)) and execute it line by line.
    Then you can see the values in variable in workspace

    ReplyDelete
  2. thank you for the code, it really helped. but the decoding code in the image hider 2.0 is taking so much time to decode that i am not getting the output. can you please help me out with this..

    ReplyDelete
  3. Hello,
    I have the following question: The algorithm, which you follow is a standard LSB or not ?

    ReplyDelete
  4. Hello,
    I have the following question: algorithm, which you follow in the code is a standard LSB algorithm or not ?

    ReplyDelete
  5. It's not standard algorithm. It's my own way to solve the puzzle.

    ReplyDelete
  6. Ok, that's it something like modified lsb algorithm ?
    but did you test s PSNR to see if it is sтабле?

    ReplyDelete
  7. I never checked PSNR. But change is happening only for 2 bits. So visually its hard to detect the hidden image. You can see the version 1.0 , where 4 bits where changed and distortion was clearly visible in the final image

    ReplyDelete
  8. Thanks for the reply !
    Looking at the code I don't understand why the image 3 (image totoo) is saved like a BMP file?

    ReplyDelete