Hide Image inside an Image

The tutorial on hiding the text messages inside the image should have given enough knowledge on bit replacing method. Also check Bit plane slicing to learn more about bit planes.
In this tutorial, we will start in the reverse order i.e. Let’s first perform decryption and then we will learn about encryption.


Download the encrypted image and run this decryption code below to see the hidden image inside the cover image.



Encrypt_Image1.png







MATLAB CODE:

%DECRYPT IMAGE
clear all

%READ THE ENCRYPTED IMAGE
A = imread('Encrypt_Image1.png');

%RED,GREEN AND BLUE COMPONENTS
Red_ch = A(:,:,1);
Green_ch = A(:,:,2);
Blue_ch = A(:,:,3);

%INTERVAL
Dt = bitget(Red_ch(1:8),1);
Dt = double(bi2de(Dt));

%NUMBER OF COLUMNS
Ncols = bitget(Red_ch(9:16),1);
Ncols = double(bi2de(Ncols));

%NUMBER OF ROWS
Nrows = bitget(Red_ch(17:24),1);
Nrows = double(bi2de(Nrows));

%FETCH THE LEAST SIGNIFICANT BIT FROM THE ENCRYPTED IMAGE
EndPoint = (Dt*Nrows*Ncols*8)+24;
Decrypt_Red = bitget(Red_ch(25:Dt:EndPoint),1);
Decrypt_Green = bitget(Green_ch(25:Dt:EndPoint),1);
Decrypt_Blue = bitget(Blue_ch(25:Dt:EndPoint),1);

Decrypt_Red = reshape(Decrypt_Red,8,[])';
Decrypt_Red = bi2de(Decrypt_Red);
Decrypt_Red = reshape(Decrypt_Red,Ncols,Nrows);

Decrypt_Green = reshape(Decrypt_Green,8,[])';
Decrypt_Green = bi2de(Decrypt_Green);
Decrypt_Green = reshape(Decrypt_Green,Ncols,Nrows);

Decrypt_Blue = reshape(Decrypt_Blue,8,[])';
Decrypt_Blue = bi2de(Decrypt_Blue);
Decrypt_Blue = reshape(Decrypt_Blue,Ncols,Nrows);

Decrypted_Image = zeros([Ncols,Nrows,3]);
Decrypted_Image(:,:,1)=Decrypt_Red;
Decrypted_Image(:,:,2)=Decrypt_Green;
Decrypted_Image(:,:,3)=Decrypt_Blue;

Decrypted_Image = uint8(Decrypted_Image);


figure,imshow(Decrypted_Image);title('Secret Image');



ENCRYPTION:

MATLAB CODE:

%ENCRYPTION
clear all

%READ THE COVER IMAGE
I = imread('peppers.png');

%READ THE IMAGE TO HIDE
J = imread('cube.jpg');

%PREALLOCATE THE OUTPUT IMAGE
Output = zeros(size(I));
%REPRESENT THE NUMBER OF ROWS AND COLUMNS OF THE SECRET IMAGE
%IN BINARY FORMAT
Jsize = de2bi([size(J,1),size(J,2)]);

%RED,GREEN AND BLUE COMPONENTS OF THE SECRET IMAGE
Red_Ch = J(:,:,1);
Green_Ch = J(:,:,2);
Blue_Ch = J(:,:,3);

%CONVERT THE RED COMPONENT OF THE SECRET IMAGE INTO BINARY FORMAT
Encrypt_Red = de2bi(Red_Ch(:),8)';
Encrypt_Red = Encrypt_Red(:)';

%CONVERT THE GREEN COMPONENT OF THE SECRET IMAGE INTO BINARY FORMAT
Encrypt_Green = de2bi(Green_Ch(:),8)';
Encrypt_Green = Encrypt_Green(:)';

%CONVERT THE BLUE COMPONENT OF THE SECRET IMAGE INTO BINARY FORMAT
Encrypt_Blue = de2bi(Blue_Ch(:),8)';
Encrypt_Blue = Encrypt_Blue(:)';

%CALCULATE THE INTERVAL AND CONVERT IT INTO BINARY FORMAT
dt=floor((size(I,1)*size(I,2))/numel(Encrypt_Red));
Bi_dt = de2bi(dt,8);

Info_data = [Bi_dt,Jsize(1,:),Jsize(2,:)];

%RED,GREEN AND BLUE COMPONENT OF THE COVER IMAGE
Red_mat = I(:,:,1);
Green_mat = I(:,:,2);
Blue_mat = I(:,:,3);

% INTERVAL,NUMBER OF COLUMNS, NUMBER OF ROWS
Red_mat(1:24) = bitset(Red_mat(1:24),1,Info_data);
Green_mat(1:24) = bitset(Green_mat(1:24),1,Info_data);
Blue_mat(1:24) = bitset(Blue_mat(1:24),1,Info_data);

%%REPLACE THE LEAST SIGNIFICANT BIT OF THE COVER IMAGE
%WITH THE BINARY FORMAT OF THE SECRET IMAGE
EndValue = numel(Encrypt_Red)*dt+24;
Red_mat(25:dt:EndValue) = bitset(Red_mat(25:dt:EndValue),1,Encrypt_Red);
Green_mat(25:dt:EndValue) = bitset(Green_mat(25:dt:EndValue),1,Encrypt_Green);
Blue_mat(25:dt:EndValue) = bitset(Blue_mat(25:dt:EndValue),1,Encrypt_Blue);

%ENCRYPTED IMAGE
Output(:,:,1) = Red_mat;
Output(:,:,2) = Green_mat;
Output(:,:,3) = Blue_mat;
Output = uint8(Output);

figure,subplot(121),imshow(I); subplot(122),imshow(Output);
 
%DIFFERENCE BETWEEN THE ORIGINAL AND THE ENCRYPTED IMAGE
figure,imagesc(double(I)-double(Output));colormap(jet);colorbar;

%WRITE THE ENCRYPTED IMAGE IN THE PNG FORMAT
imwrite(Output,'Encrypt_Image1.png');




EXPLANATION:

Were you able to extract the encrypted image? The type of the cover image is RGB and the secret image is also RGB. The Red channel of the cover image is encrypted with the Red channel of the secret image, Green channel of cover image with the green channel of the secret image. Similarly the encryption is done on the blue channel.

NOTE: 
  • The size of the cover image after encryption should not be modified
  • Save the encrypted image with lossless compression method
  • Size of the secret image should be less than 10% of the cover image

5 comments:

  1. can you give me the images you used for encryption. ie., the original cover image and the image that you hid within the cover image?

    ReplyDelete
  2. kindly share cube.jpg and peppers.png with me. Please please please. my email id is mahnoorfatima364@gmail.com

    ReplyDelete
  3. Can you please share me the images used in the encryption process (i.e) 'peppers.png' and 'cube.jpg'?
    I'll be thankful and it'll be a great help for my project.
    Thank you.
    mail id: bnvlskashyap@gmail.com

    ReplyDelete
  4. Thanks for your code.
    I am getting Error using bitset
    ASSUMEDTYPE must be an integer type name.

    Error in trying (line 54)
    Red_mat(1:24) = bitset(Red_mat(1:24),1,Info_data);
    error
    please help me

    ReplyDelete